Как можно запустить программу из памяти?

Discussion in 'С/С++, C#, Rust, Swift, Go, Java, Perl, Ruby' started by _nic, 20 Jul 2008.

  1. _nic

    _nic Elder - Старейшина

    Joined:
    5 May 2006
    Messages:
    651
    Likes Received:
    54
    Reputations:
    3
    Допустим есть выделеное место в куче куда был прочитан в бинарном режиме Pe файл.Можно как то его выполнить использовав системный Pe загрузчик?
     
  2. GALIAFF

    GALIAFF Elder - Старейшина

    Joined:
    28 Sep 2007
    Messages:
    45
    Likes Received:
    28
    Reputations:
    5
    пиши или свой загрузчик, или инжекть полностью в процесс
    _http://www.wasm.ru/article.php?article=memfile
     
  3. _nic

    _nic Elder - Старейшина

    Joined:
    5 May 2006
    Messages:
    651
    Likes Received:
    54
    Reputations:
    3
    Что то никогда о таком неслышал :( Можно поподробней?
     
  4. GALIAFF

    GALIAFF Elder - Старейшина

    Joined:
    28 Sep 2007
    Messages:
    45
    Likes Received:
    28
    Reputations:
    5
    _nic, обьясни, те что надо, запустить ре файл из своей программы НЕ сохраняя на диск? если да то я тебе уже предложил..
     
  5. _nic

    _nic Elder - Старейшина

    Joined:
    5 May 2006
    Messages:
    651
    Likes Received:
    54
    Reputations:
    3
    А нету примера на С? а то я в асме не бэ ни мэ
     
  6. GALIAFF

    GALIAFF Elder - Старейшина

    Joined:
    28 Sep 2007
    Messages:
    45
    Likes Received:
    28
    Reputations:
    5
    а по принципу самому написать? глянь в коментах к статье, но там инжект.
     
  7. _nic

    _nic Elder - Старейшина

    Joined:
    5 May 2006
    Messages:
    651
    Likes Received:
    54
    Reputations:
    3
    Немогу вьехать в заполнение таблицы импорта :confused: асм :(
     
  8. GALIAFF

    GALIAFF Elder - Старейшина

    Joined:
    28 Sep 2007
    Messages:
    45
    Likes Received:
    28
    Reputations:
    5
    глянь в коментах код
     
  9. JuliaSmit

    JuliaSmit New Member

    Joined:
    21 Dec 2007
    Messages:
    10
    Likes Received:
    2
    Reputations:
    0
    Code:
    unit MemProcess;
    
    interface
    
    uses
    Windows;
    
    
    type
      TImportItem = record
        Name: string;
        PProcVar: ^Pointer;
      end;
    
      TwordArr = array [0..0] of word;
      PwordArr = ^TwordArr;
      TdwordArr = array [0..0] of dword;
      PdwordArr = ^TdwordArr;
    
      PImageImportDescriptor = ^TImageImportDescriptor;
      TImageImportDescriptor = packed record
        OriginalFirstThunk: dword;
        TimeDateStamp: dword;
        ForwarderChain: dword;
        Name: dword;
        FirstThunk: dword;
      end;
    
      PImageBaseRelocation= ^TImageBaseRelocation;
      TImageBaseRelocation = packed record
        VirtualAddress: cardinal;
        SizeOfBlock: cardinal;
      end;
    
      TDllEntryProc = function(hinstDLL: HMODULE; dwReason: dword; lpvReserved: Pointer): Boolean; stdcall;
    
      TStringArray = array of string;
    
      TLibInfo = record
        ImageBase: Pointer;
        DllProc: TDllEntryProc;
        LibsUsed: TStringArray;
      end;
    
      PLibInfo = ^TLibInfo;
      PPointer = ^Pointer;
    
      TSections = array [0..100000] of TImageSectionHeader;
    
    const
      IMPORTED_NAME_OFFSET = $00000002;
      IMAGE_ORDINAL_FLAG32 = $80000000;
      IMAGE_ORDINAL_MASK32 = $0000FFFF;
    
    procedure CreateProcessEx(FileMemory: pointer);
    
    implementation
    
    
    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;
    
    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;
    
    
    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;
    
    
    
    end.
    Вот пример запуска файла из памяти...
     
    #9 JuliaSmit, 24 Jul 2008
    Last edited: 24 Jul 2008
    1 person likes this.
  10. _nic

    _nic Elder - Старейшина

    Joined:
    5 May 2006
    Messages:
    651
    Likes Received:
    54
    Reputations:
    3
    А можно источник ,из которого этот исходник?А то без описаний и коментариев слишком туманно выглядит.
     
Loading...
Similar Threads - запустить программу памяти
  1. Peja
    Replies:
    0
    Views:
    2,647