Autor Beitrag
Boldar
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1555
Erhaltene Danke: 70

Win7 Enterprise 64bit, Win XP SP2
Turbo Delphi
BeitragVerfasst: Mi 14.04.10 18:29 
Hallo,
mal nur interessehalber:
Kann man beliebigen Maschinencode als prozedur ausführen?
Also quasi zur Laufzeit im Code-Segment speicher reservieren, da das entsprechend reinschreiben, ein ret dahinter und dann dorthin springen?
Und wenn ja: wie ginge das?
mfg Boldar


Moderiert von user profile iconNarses: Topic aus Windows API verschoben am Mi 14.04.2010 um 22:24
Jakob_Ullmann
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1747
Erhaltene Danke: 15

Win 7, *Ubuntu GNU/Linux*
*Anjuta* (C, C++, Python), Geany (Vala), Lazarus (Pascal), Eclipse (Java)
BeitragVerfasst: Mi 14.04.10 18:36 
Meinst du Maschinencode oder Assembler? Das geht z. B. so:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure MacheWas(a1: DWORD); assembler;
asm
  push  eax
  mov   eax, a1
  mul   eax, 2
  mov   x, eax
  pop   eax
end;
Boldar Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1555
Erhaltene Danke: 70

Win7 Enterprise 64bit, Win XP SP2
Turbo Delphi
BeitragVerfasst: Mi 14.04.10 18:38 
Nein, ich möchte nicht asm eincompilieren, sondern zur Laufzeit beliebigen Maschinencode ausführen.
Also z.B. vom user eingegebenen.
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: Mi 14.04.10 19:00 
Musst Du dir nen kleinen Assembler schreiben, oder den User fragen, dass er bitte den Code als Binary ins Edit hackt.

_________________
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.
Boldar Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1555
Erhaltene Danke: 70

Win7 Enterprise 64bit, Win XP SP2
Turbo Delphi
BeitragVerfasst: Mi 14.04.10 19:27 
Ich sprach ja auch von Maschinencode, nicht von Assembler.
Die Problematik ist nur, den Maschinencode auszuführen.
Jakob_Ullmann
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1747
Erhaltene Danke: 15

Win 7, *Ubuntu GNU/Linux*
*Anjuta* (C, C++, Python), Geany (Vala), Lazarus (Pascal), Eclipse (Java)
BeitragVerfasst: Mi 14.04.10 21:00 
Bitte Bescheid sagen, wenn ich jetzt groben Unsinn erzähle, aber prinzipiell sollte der Maschinencode doch dann als COM / EXE vorliegen und wäre nur noch an der richtigen Stelle in die EXE zu schreiben (bei EXE müsste man halt den Header entfernen).
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: Mi 14.04.10 21:06 
user profile iconJakob_Ullmann hat folgendes geschrieben Zum zitierten Posting springen:
Bitte Bescheid sagen, wenn ich jetzt groben Unsinn erzähle, aber prinzipiell sollte der Maschinencode doch dann als COM / EXE vorliegen und wäre nur noch an der richtigen Stelle in die EXE zu schreiben (bei EXE müsste man halt den Header entfernen).


und den einstiegspunkt finden, oder?

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
ALF
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1085
Erhaltene Danke: 53

WinXP, Win7, Win10
Delphi 7 Enterprise, XE
BeitragVerfasst: Mi 14.04.10 21:09 
Das machen doch auch die, die uns die Vieren an den exe'n oder com hineinschreiben! :wink:
Wie ist mhhh... ganz einfach. Nur ist die Frage ob man sowas Public macht! :?

Gruss Alf

_________________
Wenn jeder alles kann oder wüsste und keiner hätt' ne Frage mehr, omg, währe dieses Forum leer!
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: Mi 14.04.10 21:49 
@ALF: Wer Doku liest, ist klar im Vorteil. Da steht (zumindest bei M$) alles Nötige drinnen ;-) Man muss es nur zusammensetzen.

Aber gut. Angenommen, wir haben an einer Stelle im RAM den Maschinenkot bereits stehen, dann geht das Ausführen ganz einfach mit:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure ExecMem(const Entry: TProcedure);
asm
    PUSH    EAX
end;


Man kann aber auch komplett ohne ASM arbeiten und einfach:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
var
    P: Pointer;

type
    TProc = procedure;

{...}

    TProc(P);


machen. Wenn man dann zusätzlich Parameter übergeben will, geht das mit:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
var
    P: Pointer;

type
    TFunc = Function(const A; var B; const C const D): Quz;

{...}

    Result := TFunc(P)(Foo, Bar, Baz, Quo);

_________________
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.
Boldar Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1555
Erhaltene Danke: 70

Win7 Enterprise 64bit, Win XP SP2
Turbo Delphi
BeitragVerfasst: Mi 14.04.10 22:47 
Nun gut.
Aber: Wie kriegt man den Code in den Ram?
und wie springt man danach wieder zurück?
Und: geht das Üerhaupt so einfach, ohne das Windows dazwischenfunkt?
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: Mi 14.04.10 23:05 
Ich weiß nicht, was Du hast:

ausblenden Delphi-Quelltext
1:
const Code: array[0..4of Byte = ($33$C0$CC$90$C3);					


Noch ein wenig Rumpointern und man ist fertig ;-)

_________________
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.
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Mi 14.04.10 23:50 
Sobald es allerdings um etwas dynamischeren Code geht, dürfte man schnell einen GPF kassieren. Aber wie du schon sagtest, Microsoft hat das alles schön dokumentiert :D .

_________________
>λ=
ALF
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1085
Erhaltene Danke: 53

WinXP, Win7, Win10
Delphi 7 Enterprise, XE
BeitragVerfasst: Do 15.04.10 05:36 
user profile iconBenBE hat folgendes geschrieben Zum zitierten Posting springen:
@ALF: Wer Doku liest, ist klar im Vorteil. Da steht (zumindest bei M$) alles Nötige drinnen ;-) Man muss es nur zusammensetzen.

Ja ich weis, wer es drauf anlegt, macht es ja auch. Erleben wir ja leider immer wieder. :(

Gruss Alf

_________________
Wenn jeder alles kann oder wüsste und keiner hätt' ne Frage mehr, omg, währe dieses Forum leer!
Steve1024
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 141

Windows 2K, XP, 7 & Server 2003 - 2008; Linux (Ubuntu, Fedora)
D7, D05, D06, D09, DXE
BeitragVerfasst: Sa 24.04.10 23:20 
hmmm... was ist mit globalalloc umd GMEM_EXECUTABLE oder so ähnlich.
damit kann man doch einen speicherbereich reservieren, denn dann als ausführbar markiert ist. d.h. wenn da maschienencode drin steht sollte das funktionieren.

Hab da aber nicht so 100%ig ne ahnung.
aber man kann mal nach DLL aus speicher laden suchen. da passiert sowas ja auch.

Hoffe ich konnte helfen und schönen abend noch!