Fixed multiple simultaneous process creation.

This commit is contained in:
Jason Hood 2012-06-12 14:38:00 +10:00
parent b3e905eeae
commit ef587f0dee
8 changed files with 64 additions and 33 deletions

59
ANSI.c
View File

@ -88,6 +88,9 @@
added the character/line equivalents (keaj`) of the cursor movement added the character/line equivalents (keaj`) of the cursor movement
sequences (ABCDG), as well as vertical absolute (d) and erase characters sequences (ABCDG), as well as vertical absolute (d) and erase characters
(X). (X).
v1.53, 12 June, 2012:
fixed Update_GRM when running multiple processes (e.g. "cl /MP").
*/ */
#include "ansicon.h" #include "ansicon.h"
@ -225,14 +228,19 @@ SHARED DWORD s_flag;
#define GRM_INIT 1 #define GRM_INIT 1
#define GRM_EXIT 2 #define GRM_EXIT 2
PROCESS_INFORMATION child_pi;
// Wait for the child process to finish, then update our GRM to the child's. // Wait for the child process to finish, then update our GRM to the child's.
DWORD WINAPI UpdateGRM( LPVOID lpParameter ) DWORD WINAPI UpdateGRM( LPVOID child_pi )
{ {
WaitForSingleObject( child_pi.hProcess, INFINITE ); DWORD pid = ((LPPROCESS_INFORMATION)child_pi)->dwProcessId;
if (s_flag == GRM_EXIT && s_pid == child_pi.dwProcessId) HANDLE proc = ((LPPROCESS_INFORMATION)child_pi)->hProcess;
free( child_pi );
WaitForSingleObject( proc, INFINITE );
CloseHandle( proc );
if (s_flag == GRM_EXIT && s_pid == pid)
{ {
s_flag = 0; s_flag = 0;
grm = s_grm; grm = s_grm;
@ -1171,12 +1179,13 @@ BOOL HookAPIAllMod( PHookFn Hooks, BOOL restore )
// Inject code into the target process to load our DLL. // Inject code into the target process to load our DLL.
void Inject( DWORD dwCreationFlags, LPPROCESS_INFORMATION lpi, void Inject( DWORD dwCreationFlags, LPPROCESS_INFORMATION lpi,
LPPROCESS_INFORMATION child_pi,
BOOL wide, LPCVOID lpApp, LPCVOID lpCmd ) BOOL wide, LPCVOID lpApp, LPCVOID lpCmd )
{ {
int type; int type;
BOOL gui; BOOL gui;
type = ProcessType( &child_pi, &gui ); type = ProcessType( child_pi, &gui );
if (gui) if (gui)
{ {
TCHAR app[MAX_PATH]; TCHAR app[MAX_PATH];
@ -1242,13 +1251,13 @@ void Inject( DWORD dwCreationFlags, LPPROCESS_INFORMATION lpi,
{ {
hDllNameType[0] = '3'; hDllNameType[0] = '3';
hDllNameType[1] = '2'; hDllNameType[1] = '2';
InjectDLL32( &child_pi, hDllName ); InjectDLL32( child_pi, hDllName );
} }
else else
{ {
hDllNameType[0] = '6'; hDllNameType[0] = '6';
hDllNameType[1] = '4'; hDllNameType[1] = '4';
InjectDLL64( &child_pi, hDllName ); InjectDLL64( child_pi, hDllName );
} }
#else #else
#ifdef W32ON64 #ifdef W32ON64
@ -1259,7 +1268,7 @@ void Inject( DWORD dwCreationFlags, LPPROCESS_INFORMATION lpi,
PROCESS_INFORMATION pi; PROCESS_INFORMATION pi;
wcscpy( hDllNameType, L"CON.exe" ); wcscpy( hDllNameType, L"CON.exe" );
wsprintf( args, L"ansicon -P%lu:%lu", wsprintf( args, L"ansicon -P%lu:%lu",
child_pi.dwProcessId, child_pi.dwThreadId ); child_pi->dwProcessId, child_pi->dwThreadId );
ZeroMemory( &si, sizeof(si) ); ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si); si.cb = sizeof(si);
if (CreateProcess( hDllName, args, NULL, NULL, FALSE, 0, NULL, NULL, if (CreateProcess( hDllName, args, NULL, NULL, FALSE, 0, NULL, NULL,
@ -1275,28 +1284,34 @@ void Inject( DWORD dwCreationFlags, LPPROCESS_INFORMATION lpi,
} }
else else
#endif #endif
InjectDLL32( &child_pi, hDllName ); InjectDLL32( child_pi, hDllName );
#endif #endif
if (!gui && !(dwCreationFlags & (CREATE_NEW_CONSOLE | DETACHED_PROCESS))) if (!gui && !(dwCreationFlags & (CREATE_NEW_CONSOLE | DETACHED_PROCESS)))
{ {
s_pid = child_pi.dwProcessId; LPPROCESS_INFORMATION cpi;
s_pid = child_pi->dwProcessId;
s_grm = grm; s_grm = grm;
s_flag = GRM_INIT; s_flag = GRM_INIT;
CloseHandle( CreateThread( NULL, 4096, UpdateGRM, NULL, 0, NULL ) ); cpi = malloc( sizeof(*cpi) );
cpi->dwProcessId = child_pi->dwProcessId;
DuplicateHandle( GetCurrentProcess(), child_pi->hProcess,
GetCurrentProcess(), &cpi->hProcess, 0, FALSE,
DUPLICATE_SAME_ACCESS );
CloseHandle( CreateThread( NULL, 4096, UpdateGRM, cpi, 0, NULL ) );
} }
} }
if (!(dwCreationFlags & CREATE_SUSPENDED)) if (!(dwCreationFlags & CREATE_SUSPENDED))
ResumeThread( child_pi.hThread ); ResumeThread( child_pi->hThread );
if (lpi) if (lpi)
{ {
memcpy( lpi, &child_pi, sizeof(PROCESS_INFORMATION) ); memcpy( lpi, child_pi, sizeof(PROCESS_INFORMATION) );
} }
else else
{ {
CloseHandle( child_pi.hThread ); CloseHandle( child_pi->hThread );
CloseHandle( child_pi.hProcess ); CloseHandle( child_pi->hProcess );
} }
} }
@ -1312,6 +1327,8 @@ BOOL WINAPI MyCreateProcessA( LPCSTR lpApplicationName,
LPSTARTUPINFOA lpStartupInfo, LPSTARTUPINFOA lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation ) LPPROCESS_INFORMATION lpProcessInformation )
{ {
PROCESS_INFORMATION child_pi;
if (!CreateProcessA( lpApplicationName, if (!CreateProcessA( lpApplicationName,
lpCommandLine, lpCommandLine,
lpThreadAttributes, lpThreadAttributes,
@ -1324,10 +1341,11 @@ BOOL WINAPI MyCreateProcessA( LPCSTR lpApplicationName,
&child_pi )) &child_pi ))
return FALSE; return FALSE;
DEBUGSTR( 1, L"CreateProcessA: \"%S\", \"%S\"", DEBUGSTR( 1, L"CreateProcessA: (%lu) \"%S\", \"%S\"",
child_pi.dwProcessId,
(lpApplicationName == NULL) ? "" : lpApplicationName, (lpApplicationName == NULL) ? "" : lpApplicationName,
(lpCommandLine == NULL) ? "" : lpCommandLine ); (lpCommandLine == NULL) ? "" : lpCommandLine );
Inject( dwCreationFlags, lpProcessInformation, Inject( dwCreationFlags, lpProcessInformation, &child_pi,
FALSE, lpApplicationName, lpCommandLine ); FALSE, lpApplicationName, lpCommandLine );
return TRUE; return TRUE;
@ -1345,6 +1363,8 @@ BOOL WINAPI MyCreateProcessW( LPCWSTR lpApplicationName,
LPSTARTUPINFOW lpStartupInfo, LPSTARTUPINFOW lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation ) LPPROCESS_INFORMATION lpProcessInformation )
{ {
PROCESS_INFORMATION child_pi;
if (!CreateProcessW( lpApplicationName, if (!CreateProcessW( lpApplicationName,
lpCommandLine, lpCommandLine,
lpThreadAttributes, lpThreadAttributes,
@ -1357,10 +1377,11 @@ BOOL WINAPI MyCreateProcessW( LPCWSTR lpApplicationName,
&child_pi )) &child_pi ))
return FALSE; return FALSE;
DEBUGSTR( 1, L"CreateProcessW: \"%s\", \"%s\"", DEBUGSTR( 1, L"CreateProcessW: (%lu) \"%s\", \"%s\"",
child_pi.dwProcessId,
(lpApplicationName == NULL) ? L"" : lpApplicationName, (lpApplicationName == NULL) ? L"" : lpApplicationName,
(lpCommandLine == NULL) ? L"" : lpCommandLine ); (lpCommandLine == NULL) ? L"" : lpCommandLine );
Inject( dwCreationFlags, lpProcessInformation, Inject( dwCreationFlags, lpProcessInformation, &child_pi,
TRUE, lpApplicationName, lpCommandLine ); TRUE, lpApplicationName, lpCommandLine );
return TRUE; return TRUE;

View File

@ -66,7 +66,7 @@
pass process & thread identifiers on the command line (for x86->x64). pass process & thread identifiers on the command line (for x86->x64).
*/ */
#define PDATE L"2 June, 2012" #define PDATE L"12 June, 2012"
#include "ansicon.h" #include "ansicon.h"
#include "version.h" #include "version.h"
@ -110,7 +110,7 @@ BOOL Inject( LPPROCESS_INFORMATION ppi, BOOL* gui, LPCTSTR app )
WCHAR dll[MAX_PATH]; WCHAR dll[MAX_PATH];
int type; int type;
DEBUGSTR( 1, L"%s", app ); DEBUGSTR( 1, L"%s (%lu)", app, ppi->dwProcessId );
type = ProcessType( ppi, gui ); type = ProcessType( ppi, gui );
if (type == 0) if (type == 0)
{ {

View File

@ -63,6 +63,8 @@ x86/ansicon.o: version.h
x86/ANSI.o: version.h x86/ANSI.o: version.h
x64/ansicon.o: version.h x64/ansicon.o: version.h
x64/ANSI.o: version.h x64/ANSI.o: version.h
x86/util.o: version.h
x64/util.o: version.h
x86/ansiconv.o: ansicon.rc x86/ansiconv.o: ansicon.rc
x86/ansiv.o: ansi.rc x86/ansiv.o: ansi.rc
x64/ansiconv.o: ansicon.rc x64/ansiconv.o: ansicon.rc

View File

@ -74,7 +74,7 @@ ansicon.c: ansicon.h version.h
ansicon.rc: version.h ansicon.rc: version.h
ANSI.c: ansicon.h version.h ANSI.c: ansicon.h version.h
ANSI.rc: version.h ANSI.rc: version.h
util.c: ansicon.h util.c: ansicon.h version.h
injdll32.c: ansicon.h injdll32.c: ansicon.h
injdll64.c: ansicon.h injdll64.c: ansicon.h
proctype.c: ansicon.h proctype.c: ansicon.h

View File

@ -50,8 +50,10 @@ int ProcessType( LPPROCESS_INFORMATION pinfo, BOOL* gui )
{ {
if (nt_header.FileHeader.Machine == IMAGE_FILE_MACHINE_I386) if (nt_header.FileHeader.Machine == IMAGE_FILE_MACHINE_I386)
{ {
DEBUGSTR( 1, L" 32-bit %s (base = %.8p)", // Microsoft ignores precision on %p.
(*gui) ? L"GUI" : L"console", minfo.AllocationBase ); DEBUGSTR( 1, L" 32-bit %s (base = %.8X)",
(*gui) ? L"GUI" : L"console",
(DWORD)(DWORD_PTR)minfo.AllocationBase );
return 32; return 32;
} }
#ifdef _WIN64 #ifdef _WIN64

View File

@ -3,7 +3,7 @@
Copyright 2005-2012 Jason Hood Copyright 2005-2012 Jason Hood
Version 1.52. Freeware Version 1.53. Freeware
=========== ===========
@ -274,6 +274,9 @@
Legend: + added, - bug-fixed, * changed. Legend: + added, - bug-fixed, * changed.
1.53 - 12 June, 2012:
- fix for multiple simultaneous process creation (e.g. "cl /MP ...").
1.52 - 2 June, 2012: 1.52 - 2 June, 2012:
+ 32-bit processes can inject into 64-bit processes; + 32-bit processes can inject into 64-bit processes;
+ implemented \e[39m & \e[49m (set default foreground/background color); + implemented \e[39m & \e[49m (set default foreground/background color);
@ -432,5 +435,5 @@
in the version text and a source diff is included. in the version text and a source diff is included.
========================= ==========================
Jason Hood, 2 June, 2012. Jason Hood, 12 June, 2012.

5
util.c
View File

@ -3,6 +3,7 @@
*/ */
#include "ansicon.h" #include "ansicon.h"
#include "version.h"
TCHAR prog_path[MAX_PATH]; TCHAR prog_path[MAX_PATH];
@ -59,7 +60,9 @@ void DEBUGSTR( int level, LPTSTR szFormat, ... )
{ {
SYSTEMTIME now; SYSTEMTIME now;
GetLocalTime( &now ); GetLocalTime( &now );
fprintf( file, "Logging started %d-%.2d-%.2d %d:%.2d:%.2d\n", fprintf( file, "ANSICON v" PVERSA " log (%d) started "
"%d-%.2d-%.2d %d:%.2d:%.2d\n",
log_level,
now.wYear, now.wMonth, now.wDay, now.wYear, now.wMonth, now.wDay,
now.wHour, now.wMinute, now.wSecond ); now.wHour, now.wMinute, now.wSecond );
fclose( file ); fclose( file );

View File

@ -2,8 +2,8 @@
version.h - Version defines. version.h - Version defines.
*/ */
#define PVERS L"1.52" // wide string #define PVERS L"1.53" // wide string
#define PVERSA "1.52" // ANSI string (windres 2.16.91 didn't like L) #define PVERSA "1.53" // ANSI string (windres 2.16.91 didn't like L)
#define PVERE L"152" // wide environment string #define PVERE L"153" // wide environment string
#define PVEREA "152" // ANSI environment string #define PVEREA "153" // ANSI environment string
#define PVERB 1,5,2,0 // binary (resource) #define PVERB 1,5,3,0 // binary (resource)