Advertisement
finalpatch

fixpopen

Aug 31st, 2014
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.14 KB | None | 0 0
  1. library fixpopen;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes, lua, lauxlib, lualib, windows;
  7.  
  8. type
  9.   {$PACKRECORDS 1}
  10.   TThunk = record
  11.     jmp   : byte;
  12.     offset: longword;
  13.   end;
  14.   {$PACKRECORDS DEFAULT}
  15.   TCreateProcess = function (lpApplicationName:LPCSTR; lpCommandLine:LPSTR;
  16.                          lpProcessAttributes:LPSECURITY_ATTRIBUTES;
  17.                          lpThreadAttributes:LPSECURITY_ATTRIBUTES;
  18.                          bInheritHandles:WINBOOL;dwCreationFlags:DWORD;
  19.                          lpEnvironment:LPVOID;lpCurrentDirectory:LPCSTR;
  20.                          lpStartupInfo:LPSTARTUPINFO;
  21.                          lpProcessInformation:LPPROCESS_INFORMATION):WINBOOL;stdcall;
  22.  
  23. var
  24.   thunk : TThunk = (jmp:$e9; offset:$0);
  25.   save  : TThunk = (jmp:$0;  offset:$0);
  26.   w32CreateProcess : TCreateProcess = nil;
  27.   protect : DWORD = 0;
  28.  
  29. function patch(L:Plua_State):integer;cdecl;
  30. var
  31.   bret: BOOL;
  32. begin
  33.   //OutputDebugString('patching');
  34.   bret:=VirtualProtect(w32CreateProcess, sizeof(TThunk), PAGE_EXECUTE_READWRITE, @protect);
  35.   CopyMemory(w32CreateProcess, @thunk, sizeof(TThunk));
  36.   VirtualProtect(w32CreateProcess, sizeof(TThunk), protect, nil);
  37.   //OutputDebugString('done');
  38.   result:=1;
  39. end;
  40.  
  41. function unpatch(L:Plua_State):integer;cdecl;
  42. var
  43.   bret: BOOL;
  44. begin
  45.   //OutputDebugString('unpatching');
  46.   VirtualProtect(w32CreateProcess, sizeof(TThunk), PAGE_EXECUTE_READWRITE, @protect);
  47.   CopyMemory(w32CreateProcess, @save, sizeof(TThunk));
  48.   VirtualProtect(w32CreateProcess, sizeof(TThunk), protect, nil);
  49.   //OutputDebugString('done');
  50.   result:=1;
  51. end;
  52.  
  53. function libinit(L:Plua_State):integer;cdecl;export;
  54. begin
  55.   lua_register(L, 'fix_popen_patch', @patch);
  56.   lua_register(L, 'fix_popen_unpatch', @unpatch);
  57.   //OutputDebugString('registered');
  58.   result:=0;
  59. end;
  60.  
  61. function myCreateProcess(lpApplicationName:LPCSTR; lpCommandLine:LPSTR;
  62.                          lpProcessAttributes:LPSECURITY_ATTRIBUTES;
  63.                          lpThreadAttributes:LPSECURITY_ATTRIBUTES;
  64.                          bInheritHandles:WINBOOL;dwCreationFlags:DWORD;
  65.                          lpEnvironment:LPVOID;lpCurrentDirectory:LPCSTR;
  66.                          lpStartupInfo:LPSTARTUPINFO;
  67.                          lpProcessInformation:LPPROCESS_INFORMATION):WINBOOL;stdcall;
  68. begin
  69.   //OutputDebugString('myCreateProcess');
  70.   unpatch(nil);
  71.   lpStartupInfo^.dwFlags:=lpStartupInfo^.dwFlags or STARTF_USESHOWWINDOW;
  72.   lpStartupInfo^.wShowWindow:=SW_HIDE;
  73.   result := w32CreateProcess(lpApplicationName,lpCommandLine,lpProcessAttributes,
  74.          lpThreadAttributes,bInheritHandles,dwCreationFlags,lpEnvironment,
  75.          lpCurrentDirectory,lpStartupInfo,lpProcessInformation);
  76.   patch(nil);
  77. end;
  78.  
  79. exports
  80.   libinit;
  81.  
  82. initialization
  83.   if w32CreateProcess = nil then
  84.   begin
  85.      // save API call address
  86.      w32CreateProcess := TCreateProcess(GetProcAddress(GetModuleHandle('kernel32.dll'), 'CreateProcessA'));
  87.      // save API function prelude
  88.      CopyMemory(@save, w32CreateProcess, sizeof(TThunk));
  89.      // fill in the thunk
  90.      thunk.offset:= pointer(@myCreateProcess) - pointer(w32CreateProcess) - 5;
  91.   end
  92. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement