Ir al contenido


Foto

Hook a la API CreateProcessInternalW en Win10


  • Por favor identifícate para responder
3 respuestas en este tema

#1 escafandra

escafandra

    Advanced Member

  • Administrador
  • 4.107 mensajes
  • LocationMadrid - España

Escrito 19 diciembre 2016 - 08:20

Me plantearon una duda sobre el Hook a la API CreateProcessInternalW en Win10, la duda era porqué no funcionaba en Win10 si habían seguido el ejemplo que en su día publiqué aquí. Así que probé ha realizar un ejemplo y efectivamente no funcionaba. La API CreateProcessInternalW se exporta en Kernell32.dll y comprobé que así seguía siendo en win10.
 
Algo había cambiado en Win10 y como es una API indocumentada M$ no tiene porqué informar nada. Busqué en la red, pero no encontré nada, de esta forma tocaba usar el debugger y comparar direcciones de memoria de las API. El resultado es que Win10 usa la API exportada en KernelBase.dll y no la clásica Kernell32.dll. Una vez conocido este detalle el hook vuelve a funcionar.
 
post-12294-0-82310800-1482200416.jpg
 
 
Voy a poner un ejemplo hecho en delphi Berlin para resarcir la duda y aclarar la definición de esa API en delphi:

delphi
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, APIHook, ShellAPI;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Button1: TButton;
  12.     procedure Button1Click(Sender: TObject);
  13.   private
  14.     { Private declarations }
  15.   public
  16.     { Public declarations }
  17.   end;
  18.  
  19. // Defino esta estructura para los que quieran probar en delphi 7
  20. PSTARTUPINFOW = ^_STARTUPINFOW;
  21. _STARTUPINFOW = record
  22.     cb: DWORD;
  23.     lpReserved: PWideChar;
  24.     lpDesktop: PWideChar;
  25.     lpTitle: PWideChar;
  26.     dwX: DWORD;
  27.     dwY: DWORD;
  28.     dwXSize: DWORD;
  29.     dwYSize: DWORD;
  30.     dwXCountChars: DWORD;
  31.     dwYCountChars: DWORD;
  32.     dwFillAttribute: DWORD;
  33.     dwFlags: DWORD;
  34.     wShowWindow: Word;
  35.     cbReserved2: Word;
  36.     lpReserved2: PByte;
  37.     hStdInput: THandle;
  38.     hStdOutput: THandle;
  39.     hStdError: THandle;
  40.   end;
  41.  
  42. PCreateProcessInternalW = function(hToken: THANDLE; pApplicationName, lpCommandLine: PWCHAR;
  43.                                    lpProcessAttributes, lpThreadAttributes:    PSECURITYATTRIBUTES; bInheritHandles: BOOL;
  44.                                    dwCreationFlags: DWORD; lpEnvironment: Pointer; lpCurrentDirectory: PWCHAR;
  45.                                    lpStartupInfo: PSTARTUPINFOW; lpProcessInformation: PPROCESSINFORMATION; hNewToken: PHANDLE): BOOL; stdcall;
  46.  
  47.  
  48. var
  49.   OldCreateProcessInternalW: PCreateProcessInternalW = nil;
  50.  
  51.  
  52. var
  53.   Form1: TForm1;
  54.  
  55. implementation
  56.  
  57. {$R *.dfm}
  58. function NewCreateProcessInternalW(hToken: THANDLE; pApplicationName, lpCommandLine: PWCHAR;
  59.                           lpProcessAttributes, lpThreadAttributes:    PSECURITYATTRIBUTES; bInheritHandles: BOOL;
  60.                           dwCreationFlags: DWORD; lpEnvironment: Pointer; lpCurrentDirectory: PWCHAR;
  61.                           lpStartupInfo: PSTARTUPINFOW; lpProcessInformation: PPROCESSINFORMATION; hNewToken: PHANDLE): BOOL; stdcall;
  62. begin
  63.   Result:= FALSE;
  64.   Winapi.Windows.Beep(500, 100);
  65.   exit;
  66. //  Result:= OldCreateProcessInternalW(hToken, pApplicationName, lpCommandLine, lpProcessAttributes, lpThreadAttributes, bInheritHandles,
  67. //                          dwCreationFlags, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation, hNewToken);
  68. end;
  69. //----------------------------------------------------------------------------
  70. // Instalando los Hooks a las API
  71. procedure InstallHooks;
  72. begin
  73.   InstallHook(@NewCreateProcessInternalW, @OldCreateProcessInternalW, 'Kernelbase.dll', 'CreateProcessInternalW', true);
  74. end;
  75.  
  76. procedure UnInstallHooks;
  77. begin
  78.   UnInstallHook(@OldCreateProcessInternalW, 'KernelBase.dll', 'CreateProcessInternalW');
  79. end;
  80.  
  81. procedure Execute(ProcessName: AnsiString);
  82. var
  83.   si: TStartupInfoA;
  84.   pi: TProcessInformation;
  85.  
  86. begin
  87.   ZeroMemory(@si, SizeOf(TStartupInfoA));
  88.   si.cb := SizeOf(TStartupInfoA);
  89.   si.dwFlags:= STARTF_USESHOWWINDOW;
  90.   si.wShowWindow:= SW_SHOW;
  91.  
  92.   CreateProcessA(nil, PAnsiChar(ProcessName), nil, nil, false, 0, nil, nil, si, pi);
  93. end;
  94.  
  95.  
  96.  
  97. procedure TForm1.Button1Click(Sender: TObject);
  98. begin
  99.   InstallHooks;
  100.   Execute('cmd.exe');
  101.   ShellExecute(0,'open','cmd.exe', nil, nil, SW_SHOW);
  102.   UnInstallHooks;
  103. end;
  104.  
  105. end.
  106.  
  107.  
  108.  
  109. end.

El ejemplo muestra como un hook a la API CreateProcessInternalW es capaz de interceptar distintas formas de ejecutar un proceso.
 
El misterio queda resuelto.
 
 
Saludos.

Archivos adjuntos


  • 5

#2 sir.dev.a.lot

sir.dev.a.lot

    Advanced Member

  • Miembros
  • PipPipPip
  • 545 mensajes
  • Location127.0.0.1

Escrito 19 diciembre 2016 - 09:16

+1 ... Esto de API no documentados son siempre temas Interesantes...

 

Saludos!


  • 1

#3 escafandra

escafandra

    Advanced Member

  • Administrador
  • 4.107 mensajes
  • LocationMadrid - España

Escrito 20 diciembre 2016 - 05:09

El ejemplo es una prueba de concepto. En un caso real debería escribirse una dll e inyectarse el el proceso que queremos controlar o a todos los procesos si es el caso.

Un uso típico es comparar el hash del proceso cuyo nombre se pasa a CreateProcessInternalW, con una lista negra para permitir o no lea ejecución, cambiar atributos de seguridad o ejecutar un proceso distinto.

 

Saludos.


  • 2

#4 escafandra

escafandra

    Advanced Member

  • Administrador
  • 4.107 mensajes
  • LocationMadrid - España

Escrito 25 diciembre 2016 - 01:34

Para los interesados en el tema, aquí dejo un tutorial para extender el Hook a la API de 32 a 64 bits HOOK a la API64. Uno de los ejemplos el ejemplos realiza un Hook a CreateProcessInternalW en Win10 64 bits.

 

 

Saludos.


  • 0




IP.Board spam blocked by CleanTalk.