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:
| program test;
uses windows, sysutils;
procedure haha; begin messagebox(0,'normaler Export aufgerufen',nil,0); end;
var dllhandle: integer; procedure dllloadproc; stdcall; begin messagebox(0,pchar('Dll geladen bei: '+inttohex(dllhandle,8)),nil,0); end;
procedure dllunloadproc; stdcall; begin messagebox(0,pchar('Dll entladen'),nil,0); end;
procedure DllLoad; stdcall; asm pusha call dllLoadproc popa end;
procedure DllUnload; stdcall; asm pusha call dllUnLoadproc popa end;
procedure init; asm push eax mov eax, ebx shr eax, 16 shl eax, 16 mov [dllhandle], eax mov eax, esp shl eax, 24 cmp eax, $a8000000 pop eax jne @_unload call dllload jmp @_exit @_unload: call dllunload @_exit: end;
exports haha;
procedure SaveMeAsDllAndLoadMeAndExecuteExport; var IDH: PImageDosHeader; INH: PImageNtHeaders; h: integer; filesize, read, dllh: cardinal; filemem: pointer; hahaproc: procedure; begin copyfile(pchar(paramstr(0)),'measdll.dll',false); h := CreateFileA('measdll.dll',GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); if h <> -1 then begin filesize := getfilesize(h,nil); getmem(filemem,filesize); readfile(h,filemem^,filesize,read,nil);
IDH := filemem; if IDH^.e_magic = IMAGE_DOS_SIGNATURE then begin INH := pointer(cardinal(filemem)+cardinal(IDH^._lfanew)); if INH^.Signature = IMAGE_NT_SIGNATURE then begin INH^.FileHeader.Characteristics := INH^.FileHeader.Characteristics + IMAGE_FILE_DLL; INH^.OptionalHeader.AddressOfEntryPoint := cardinal(@init)-getmodulehandle(nil); setfilepointer(h,0,nil,FILE_BEGIN); writefile(h,filemem^,filesize,read,nil); end; end; closehandle(h); end; dllh := loadlibrary('measdll.dll'); @hahaproc := getprocaddress(dllh,'haha'); hahaproc; freelibrary(dllh); end;
begin messagebox(0,'bin eine exe',nil,0); SaveMeAsDllAndLoadMeAndExecuteExport; end. |