; 2024.11.24 writed by fake77
; kanxue: https://bbs.kanxue.com/homepage-983513.htm
.686
.model flat, stdcall
option casemap:NONE
include windows.inc
include user32.inc
include kernel32.inc
includelib user32.lib
includelib kernel32.lib
WinMain proto :DWORD, :DWORD, :DWORD, :DWORD
.data?
hInstance HINSTANCE ?
CommandLine LPSTR ?
.data
g_szTargetProcessName db "鎵浄", 0
g_szCaption db "Err", 0
g_szErr1 db "Err 1", 0
g_szErr2 db "Err 2", 0
g_szErr3 db "Err 3", 0
g_szErr4 db "Err 4", 0
g_szErr5 db "Err 5", 0
g_szErr6 db "Err 6", 0
.code
INJECT_BEGIN:
; todo inject code
; inject data area
CODE_BEGIN:
; inject asm code area
CODE_END:
WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
; alloc local var
LOCAL hWnd:HWND ; handle of window
LOCAL dwPid:DWORD ; process id for open process
LOCAL hProc:DWORD ; process handle
LOCAL pAddr:LPVOID ; target mem pointer
LOCAL dwWrited:DWORD; writed byte num by WriteProcessMemory
; 1. get window handle
invoke FindWindow, NULL, offset g_szTargetProcessName
mov hWnd, eax
.IF !eax
invoke MessageBox, NULL, offset g_szErr1, offset g_szCaption, MB_OK
.ENDIF
; 2. find process
invoke GetWindowThreadProcessId, hWnd, addr dwPid
.IF !eax
invoke MessageBox, NULL, offset g_szErr2, offset g_szCaption, MB_OK
.ENDIF
; 3. get process handle
invoke OpenProcess, PROCESS_ALL_ACCESS, FALSE, dwPid
mov hProc, eax
.IF !eax
invoke MessageBox, NULL, offset g_szErr3, offset g_szCaption, MB_OK
.ENDIF
; 4. alloc mem in target process and prvg set read write
invoke VirtualAllocEx, hProc, NULL, 1000h, MEM_COMMIT, PAGE_EXECUTE_READWRITE
mov pAddr, eax
.IF !eax
invoke MessageBox, NULL, offset g_szErr4, offset g_szCaption, MB_OK
.ENDIF
; 5. write code to target process
invoke WriteProcessMemory, hProc, pAddr, offset INJECT_BEGIN, \
offset CODE_END - offset INJECT_BEGIN, addr dwWrited
.IF !eax
invoke MessageBox, NULL, offset g_szErr5, offset g_szCaption, MB_OK
.ENDIF
; 6. create remote thread to run code
mov eax, pAddr
add eax, offset CODE_BEGIN - offset INJECT_BEGIN ; code entry
invoke CreateRemoteThread, hProc, NULL, 0, eax, NULL, 0, NULL
.IF !eax
invoke MessageBox, NULL, offset g_szErr6, offset g_szCaption, MB_OK
.ENDIF
; makesure stack banlance
ret 10
WinMain endp
start:
invoke GetModuleHandle, NULL
mov hInstance,eax
invoke GetCommandLine
mov CommandLine,eax
invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT
invoke ExitProcess,eax
end start