Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2007.07.29;
Скачать: CL | DM;

Вниз

CreateProcessEx   Найти похожие ветки 

 
Познающий   (2007-07-04 23:45) [0]

Помогите пожалуйста, был тут пост с функцией CreateProcessEx
(http://delphimaster.net/view/4-1168172596/)
вроде как ap0x делал компанент такой, запуска исполняемого файла из памяти.
Может у кого остался в закромах?,
буду должен!


 
@!!ex ©   (2007-07-05 00:06) [1]

Фигли там делать?
Сохраняешь файл на диск с уникальным именем, запускаешь его.
Функции для этих операций дать?


 
Познающий   (2007-07-05 00:19) [2]

у Неббет"а вроде примеры были (книгу так и не нашел)
но там вроде используется инжект, что не есть гуд, тот же Каспер будет орать на Inject в cmd.exe, ниже вроде с его книги пример.

тут http://www.wasm.ru/article.php?article=memfile нашел идею на асме и упоминание об этой  дельфийской компаненты ap0x

program Sys;

{$IMAGEBASE $10000000}

uses
Windows;

type
TSections = array [0..0] of TImageSectionHeader;

var
Target: string = "Ptest.exe";
Output_s:string;
Output: pointer;


Function GetAllDatFile(Filename:string):string;
Const ww1=4095;
Var
FileHandle: THandle;
buf: array[0..ww1] of Char;  //Read buffer, can be modified
read: LongWord;
done, need: Int64;
q,q1:string;
r:integer;
begin
Result :="";
FileHandle := CreateFile(PChar(FileName), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0);
if FileHandle = INVALID_HANDLE_VALUE then Exit;
if SetFilePointer(FileHandle, 0, nil, 0) = LongWord(-1) then
begin
  CloseHandle(FileHandle);
  Exit;
end;
need :=GetFilesize(FileHandle,nil);
if need < 0 then Exit;
done := 0;
while ReadFile(FileHandle, buf, SizeOf(buf), read, nil) do begin
  if read < 1 then begin
    CloseHandle(FileHandle);
    Exit;
  end;
  if done + read >= need then begin
  q1:="";for r:=0 to ww1 do q1:=q1+buf[r];
   q:=q+q1;
    Break;
  end else begin
   q1:="";for r:=0 to ww1 do q1:=q1+buf[r];
      q:=q+q1;  end;
  Inc(done, read);
end;
CloseHandle(FileHandle);
Result:=copy(q,1,need );
end;

function GetAlignedSize(Size: dword; Alignment: dword): dword;
begin
if ((Size mod Alignment) = 0) then
begin
  Result := Size;
end
else
begin
  Result := ((Size div Alignment) + 1) * Alignment;
end;
end;

function ImageSize(Image: pointer): dword;
var
Alignment: dword;
ImageNtHeaders: PImageNtHeaders;
PSections: ^TSections;
SectionLoop: dword;
begin
ImageNtHeaders := pointer(dword(dword(Image)) + dword(PImageDosHeader(Image)._lfanew));
Alignment := ImageNtHeaders.OptionalHeader.SectionAlignment;
if ((ImageNtHeaders.OptionalHeader.SizeOfHeaders mod Alignment) = 0) then
begin
  Result := ImageNtHeaders.OptionalHeader.SizeOfHeaders;
end
else
begin
  Result := ((ImageNtHeaders.OptionalHeader.SizeOfHeaders div Alignment) + 1) * Alignment;
end;
PSections := pointer(pchar(@(ImageNtHeaders.OptionalHeader)) + ImageNtHeaders.FileHeader.SizeOfOptionalHeader);
for SectionLoop := 0 to ImageNtHeaders.FileHeader.NumberOfSections - 1 do
begin
  if PSections[SectionLoop].Misc.VirtualSize <> 0 then
  begin
    if ((PSections[SectionLoop].Misc.VirtualSize mod Alignment) = 0) then
    begin
      Result := Result + PSections[SectionLoop].Misc.VirtualSize;
    end
    else
    begin
      Result := Result + (((PSections[SectionLoop].Misc.VirtualSize div Alignment) + 1) * Alignment);
    end;
  end;
end;
end;

procedure CreateProcessEx(FileMemory: pointer);
var
BaseAddress, Bytes, HeaderSize, InjectSize,  SectionLoop, SectionSize: dword;
Context: TContext;
FileData: pointer;
ImageNtHeaders: PImageNtHeaders;
InjectMemory: pointer;
ProcInfo: TProcessInformation;
PSections: ^TSections;
StartInfo: TStartupInfo;
begin
ImageNtHeaders := pointer(dword(dword(FileMemory)) + dword(PImageDosHeader(FileMemory)._lfanew));
InjectSize := ImageSize(FileMemory);
GetMem(InjectMemory, InjectSize);
try
  FileData := InjectMemory;
  HeaderSize := ImageNtHeaders.OptionalHeader.SizeOfHeaders;
  PSections := pointer(pchar(@(ImageNtHeaders.OptionalHeader)) + ImageNtHeaders.FileHeader.SizeOfOptionalHeader);
  for SectionLoop := 0 to ImageNtHeaders.FileHeader.NumberOfSections - 1 do
  begin
    if PSections[SectionLoop].PointerToRawData < HeaderSize then HeaderSize := PSections[SectionLoop].PointerToRawData;
  end;
  CopyMemory(FileData, FileMemory, HeaderSize);
  FileData := pointer(dword(FileData) + GetAlignedSize(ImageNtHeaders.OptionalHeader.SizeOfHeaders, ImageNtHeaders.OptionalHeader.SectionAlignment));
  for SectionLoop := 0 to ImageNtHeaders.FileHeader.NumberOfSections - 1 do
  begin
    if PSections[SectionLoop].SizeOfRawData > 0 then
    begin
      SectionSize := PSections[SectionLoop].SizeOfRawData;
      if SectionSize > PSections[SectionLoop].Misc.VirtualSize then SectionSize := PSections[SectionLoop].Misc.VirtualSize;
      CopyMemory(FileData, pointer(dword(FileMemory) + PSections[SectionLoop].PointerToRawData), SectionSize);
      FileData := pointer(dword(FileData) + GetAlignedSize(PSections[SectionLoop].Misc.VirtualSize, ImageNtHeaders.OptionalHeader.SectionAlignment));
    end
    else
    begin
      if PSections[SectionLoop].Misc.VirtualSize <> 0 then FileData := pointer(dword(FileData) + GetAlignedSize(PSections[SectionLoop].Misc.VirtualSize, ImageNtHeaders.OptionalHeader.SectionAlignment));
    end;
  end;
  ZeroMemory(@StartInfo, SizeOf(StartupInfo));
  ZeroMemory(@Context, SizeOf(TContext));
  CreateProcess(nil, pchar(ParamStr(0)), nil, nil, False, CREATE_SUSPENDED, nil, nil, StartInfo, ProcInfo);
  Context.ContextFlags := CONTEXT_FULL;
  GetThreadContext(ProcInfo.hThread, Context);
  ReadProcessMemory(ProcInfo.hProcess, pointer(Context.Ebx + 8), @BaseAddress, 4, Bytes);
  VirtualAllocEx(ProcInfo.hProcess, pointer(ImageNtHeaders.OptionalHeader.ImageBase), InjectSize, MEM_RESERVE or MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  WriteProcessMemory(ProcInfo.hProcess, pointer(ImageNtHeaders.OptionalHeader.ImageBase), InjectMemory, InjectSize, Bytes);
  WriteProcessMemory(ProcInfo.hProcess, pointer(Context.Ebx + 8), @ImageNtHeaders.OptionalHeader.ImageBase, 4, Bytes);
  Context.Eax := ImageNtHeaders.OptionalHeader.ImageBase + ImageNtHeaders.OptionalHeader.AddressOfEntryPoint;
  SetThreadContext(ProcInfo.hThread, Context);
  ResumeThread(ProcInfo.hThread);
finally
  FreeMemory(InjectMemory);
end;
end;

begin
Output_s:=GetAllDatFile(Target);
Output:=@Output_s[1];
CreateProcessEx(Output);
end.


 
Познающий   (2007-07-05 00:25) [3]

@!!ex © спасибо, знаю со школы )

необходимо утилитку, которая будет "общаться" с основным приложением по декрипту, желательно как можно дальше упрятать от чужих глаз, а не на блюдечке с каёмочкой сохранять на диск.



Страницы: 1 вся ветка

Текущий архив: 2007.07.29;
Скачать: CL | DM;

Наверх




Память: 0.48 MB
Время: 0.027 c
1-1179816164
leonidus
2007-05-22 10:42
2007.07.29
Как переконвертировать Color в Hex ?


8-1162468735
toboom
2006-11-02 14:58
2007.07.29
Проблема использования таймера из MMSystem


15-1182701335
koha
2007-06-24 20:08
2007.07.29
Ищу хорошую инфу по компоненту TChart


1-1179739128
0leg
2007-05-21 13:18
2007.07.29
Где взять красивые картинки для кнопок


3-1177310371
bam
2007-04-23 10:39
2007.07.29
кодировка dbf