1414 virtualAllocEx = kernel32DLL .NewProc ("VirtualAllocEx" )
1515 writeProcessMemory = kernel32DLL .NewProc ("WriteProcessMemory" )
1616 createRemoteThread = kernel32DLL .NewProc ("CreateRemoteThread" )
17+ getThreadId = kernel32DLL .NewProc ("GetThreadId" )
1718 createToolhelp32Snapshot = kernel32DLL .NewProc ("CreateToolhelp32Snapshot" )
1819 process32FirstW = kernel32DLL .NewProc ("Process32FirstW" )
1920 process32NextW = kernel32DLL .NewProc ("Process32NextW" )
@@ -54,8 +55,8 @@ func injectDLL(processID uint32, processHandle windows.Handle, dllPath string) (
5455 if remoteAlloc == 0 {
5556 return 0 , fmt .Errorf ("VirtualAllocEx failed: %v" , err )
5657 }
57- logMessage (LOGLEVEL_INFO , fmt .Sprintf ("PID: %d - VirtualAllocEx...\n " , processID ))
58- logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - Allocating memory at: 0x%x\n " , processID , remoteAlloc ))
58+ logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - VirtualAllocEx..." , processID ))
59+ logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - Allocating memory at: 0x%x" , processID , remoteAlloc ))
5960
6061 bytesWritten := uint (0 )
6162 _ , _ , err = writeProcessMemory .Call (
@@ -66,9 +67,9 @@ func injectDLL(processID uint32, processHandle windows.Handle, dllPath string) (
6667 uintptr (unsafe .Pointer (& bytesWritten )),
6768 )
6869 if bytesWritten == 0 {
69- return 0 , fmt .Errorf ("PID: %d WriteProcessMemory failed: %v" , processID , err )
70+ return 0 , fmt .Errorf ("WriteProcessMemory failed: %v" , err )
7071 }
71- logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - Bytes written: %d\n " , processID , bytesWritten ))
72+ logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - Bytes written: %d" , processID , bytesWritten ))
7273
7374 threadHandle , _ , err := createRemoteThread .Call (
7475 uintptr (processHandle ),
@@ -80,40 +81,40 @@ func injectDLL(processID uint32, processHandle windows.Handle, dllPath string) (
8081 0 ,
8182 )
8283 if threadHandle == 0 {
83- return 0 , fmt .Errorf ("PID: %d - CreateRemoteThread failed: %v" , processID , err )
84+ return 0 , fmt .Errorf ("CreateRemoteThread failed: %v" , err )
8485 }
85- logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - CreateRemoteThread...\n " , processID ))
86- logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - Thread Handle: %d\n " , processID , threadHandle ))
86+ logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - CreateRemoteThread..." , processID ))
87+ logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - Thread Handle: %d" , processID , threadHandle ))
8788 defer syscall .CloseHandle (syscall .Handle (threadHandle ))
8889
89- logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - Waiting for thread to finish...\n " , processID ))
90+ logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - Waiting for thread to finish..." , processID ))
9091 _ , err = syscall .WaitForSingleObject (syscall .Handle (threadHandle ), syscall .INFINITE )
9192 if err != nil {
92- return 0 , fmt .Errorf ("PID: %d - WaitForSingleObject failed: %v" , processID , err )
93+ return 0 , fmt .Errorf ("WaitForSingleObject failed: %v" , err )
9394 }
9495
9596 // Récupérer l'adresse de la DLL chargée dans le processus distant
9697 remoteDLLHandle , err := GetInjectedLibraryModuleHandle (processID , dllPath )
9798 if err != nil {
98- return 0 , fmt .Errorf ("PID: %d - GetModuleHandle failed: %v" , processID , err )
99+ return 0 , fmt .Errorf ("GetModuleHandle failed: %v" , err )
99100 }
100- logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - DLL address in the remote process: 0x%x\n " , processID , remoteDLLHandle ))
101+ logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - DLL address in the remote process: 0x%x" , processID , remoteDLLHandle ))
101102
102103 return remoteDLLHandle , nil
103104}
104105
105106func GetInjectedLibraryModuleHandle (processID uint32 , injectedDllPath string ) (uintptr , error ) {
106107 handle , err := syscall .OpenProcess (windows .PROCESS_QUERY_INFORMATION | windows .PROCESS_VM_READ , false , processID )
107108 if err != nil {
108- return 0 , fmt .Errorf ("PID: %d - error opening process: %w" , processID , err )
109+ return 0 , fmt .Errorf ("error opening process: %w" , err )
109110 }
110111 defer syscall .CloseHandle (syscall .Handle (handle ))
111112
112113 var modules [1024 ]windows.Handle
113114 var needed uint32
114115 err = windows .EnumProcessModules (windows .Handle (handle ), & modules [0 ], uint32 (unsafe .Sizeof (modules )), & needed )
115116 if err != nil {
116- return 0 , fmt .Errorf ("PID: %d - error enumerating process modules: %v" , processID , err )
117+ return 0 , fmt .Errorf ("error enumerating process modules: %v" , err )
117118 }
118119
119120 numModules := needed / uint32 (unsafe .Sizeof (windows .Handle (0 )))
@@ -128,16 +129,16 @@ func GetInjectedLibraryModuleHandle(processID uint32, injectedDllPath string) (u
128129}
129130
130131func callRemoteFunction (processID uint32 , dllBaseAddress uintptr , functionName string , functionRVA uintptr ) error {
131- handle , err := syscall .OpenProcess (windows .PROCESS_QUERY_INFORMATION | windows .PROCESS_VM_READ , false , processID )
132+ processHandle , err := syscall .OpenProcess (windows .PROCESS_QUERY_INFORMATION | windows .PROCESS_VM_READ , false , processID )
132133 if err != nil {
133- return fmt .Errorf ("PID: %d - error opening process: %w" , processID , err )
134+ return fmt .Errorf ("error opening process: %w" , err )
134135 }
135- defer syscall .CloseHandle (syscall .Handle (handle ))
136+ defer syscall .CloseHandle (syscall .Handle (processHandle ))
136137
137138 remoteFunctionAddress := dllBaseAddress + functionRVA
138139
139140 threadHandle , _ , err := createRemoteThread .Call (
140- uintptr (handle ),
141+ uintptr (processHandle ),
141142 0 ,
142143 0 ,
143144 remoteFunctionAddress ,
@@ -146,43 +147,54 @@ func callRemoteFunction(processID uint32, dllBaseAddress uintptr, functionName s
146147 0 ,
147148 )
148149 if threadHandle == 0 {
149- return fmt .Errorf ("PID: %d - CreateRemoteThread failed while calling '%s'- %v" , processID , functionName , err )
150+ return fmt .Errorf ("CreateRemoteThread failed while calling '%s'- %v" , functionName , err )
150151 }
151152 defer syscall .CloseHandle (syscall .Handle (threadHandle ))
152153
154+ threadId , _ , err := getThreadId .Call (uintptr (threadHandle ))
155+
156+ if threadId == 0 {
157+ return fmt .Errorf ("GetThreadId failed: %v" , err )
158+ }
159+
160+ logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - Remote Thread ID: %d" , processID , threadId ))
161+
153162 return nil
154163}
155164
156165func injectInProcess (processID uint32 , processName string , dllPath string , dllFunction string ) error {
157- logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - Opening process %s with 0x%x access...\n " , processID , processName , windows .PROCESS_CREATE_THREAD | windows .PROCESS_VM_WRITE | windows .PROCESS_VM_OPERATION ))
166+ logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - Opening process %s with 0x%x access..." , processID , processName , windows .PROCESS_CREATE_THREAD | windows .PROCESS_VM_WRITE | windows .PROCESS_VM_OPERATION ))
158167 processHandle , err := syscall .OpenProcess (windows .PROCESS_CREATE_THREAD | windows .PROCESS_VM_WRITE | windows .PROCESS_VM_OPERATION , false , processID )
159168 if err != nil {
160- return fmt .Errorf ("PID: %d - OpenProcess failed: %v" , processID , err )
169+ return fmt .Errorf ("OpenProcess failed: %v" , err )
161170 }
162171 defer syscall .CloseHandle (processHandle )
163172
164- logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - Process Handle: 0x%x\n " , processID , processHandle ))
165- logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - Loading DLL: %s\n " , processID , dllPath ))
166- logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - DLL Path Length: %d\n " , processID , len (dllPath )))
173+ logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - Process Handle: 0x%x" , processID , processHandle ))
174+ logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - Loading DLL: %s" , processID , dllPath ))
175+ logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - DLL Path Length: %d" , processID , len (dllPath )))
167176
168177 dllBaseAddress , err := injectDLL (processID , windows .Handle (processHandle ), dllPath )
169- if err != nil {
170- return fmt .Errorf ("PID: %d - DLL injection failed: %v" , processID , err )
178+ if err != nil || dllBaseAddress == 0 {
179+ if err == nil {
180+ err = fmt .Errorf ("DLL base address is 0" )
181+ }
182+ return fmt .Errorf ("DLL injection failed: %v" , err )
171183 }
172- logMessage (LOGLEVEL_INFO , fmt .Sprintf ("PID: %d - DLL injected successfully.\n " , processID ))
184+ logMessage (LOGLEVEL_INFO , fmt .Sprintf ("PID: %d - DLL injected successfully." , processID ))
173185
174186 FunctionRVA , err := findSymbolRVA (dllPath , dllFunction )
175187 if err != nil {
176- return fmt .Errorf ("PID: %d - Error finding symbol RVA: %v" , processID , err )
188+ return fmt .Errorf ("error finding symbol RVA: %v" , err )
177189 }
178- logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - Function '%s' RVA: 0x%x\n " , processID , dllFunction , FunctionRVA ))
190+ logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - Function '%s' RVA: 0x%x" , processID , dllFunction , FunctionRVA ))
179191
180192 err = callRemoteFunction (processID , dllBaseAddress , dllFunction , uintptr (FunctionRVA ))
181193 if err != nil {
182194
183- return fmt .Errorf ("PID: %d - Error calling remote function %v" , processID , err )
195+ return fmt .Errorf ("error calling remote function %v" , err )
184196 }
185- logMessage (LOGLEVEL_INFO , fmt .Sprintf ("PID: %d - Function '%s' successfully called.\n " , processID , dllFunction ))
197+ logMessage (LOGLEVEL_DEBUG , fmt .Sprintf ("PID: %d - Function '%s' successfully called." , processID , dllFunction ))
186198
187199 return nil
188200}
0 commit comments