14
14
virtualAllocEx = kernel32DLL .NewProc ("VirtualAllocEx" )
15
15
writeProcessMemory = kernel32DLL .NewProc ("WriteProcessMemory" )
16
16
createRemoteThread = kernel32DLL .NewProc ("CreateRemoteThread" )
17
+ getThreadId = kernel32DLL .NewProc ("GetThreadId" )
17
18
createToolhelp32Snapshot = kernel32DLL .NewProc ("CreateToolhelp32Snapshot" )
18
19
process32FirstW = kernel32DLL .NewProc ("Process32FirstW" )
19
20
process32NextW = kernel32DLL .NewProc ("Process32NextW" )
@@ -54,8 +55,8 @@ func injectDLL(processID uint32, processHandle windows.Handle, dllPath string) (
54
55
if remoteAlloc == 0 {
55
56
return 0 , fmt .Errorf ("VirtualAllocEx failed: %v" , err )
56
57
}
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 ))
59
60
60
61
bytesWritten := uint (0 )
61
62
_ , _ , err = writeProcessMemory .Call (
@@ -66,9 +67,9 @@ func injectDLL(processID uint32, processHandle windows.Handle, dllPath string) (
66
67
uintptr (unsafe .Pointer (& bytesWritten )),
67
68
)
68
69
if bytesWritten == 0 {
69
- return 0 , fmt .Errorf ("PID: %d WriteProcessMemory failed: %v" , processID , err )
70
+ return 0 , fmt .Errorf ("WriteProcessMemory failed: %v" , err )
70
71
}
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 ))
72
73
73
74
threadHandle , _ , err := createRemoteThread .Call (
74
75
uintptr (processHandle ),
@@ -80,40 +81,40 @@ func injectDLL(processID uint32, processHandle windows.Handle, dllPath string) (
80
81
0 ,
81
82
)
82
83
if threadHandle == 0 {
83
- return 0 , fmt .Errorf ("PID: %d - CreateRemoteThread failed: %v" , processID , err )
84
+ return 0 , fmt .Errorf ("CreateRemoteThread failed: %v" , err )
84
85
}
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 ))
87
88
defer syscall .CloseHandle (syscall .Handle (threadHandle ))
88
89
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 ))
90
91
_ , err = syscall .WaitForSingleObject (syscall .Handle (threadHandle ), syscall .INFINITE )
91
92
if err != nil {
92
- return 0 , fmt .Errorf ("PID: %d - WaitForSingleObject failed: %v" , processID , err )
93
+ return 0 , fmt .Errorf ("WaitForSingleObject failed: %v" , err )
93
94
}
94
95
95
96
// Récupérer l'adresse de la DLL chargée dans le processus distant
96
97
remoteDLLHandle , err := GetInjectedLibraryModuleHandle (processID , dllPath )
97
98
if err != nil {
98
- return 0 , fmt .Errorf ("PID: %d - GetModuleHandle failed: %v" , processID , err )
99
+ return 0 , fmt .Errorf ("GetModuleHandle failed: %v" , err )
99
100
}
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 ))
101
102
102
103
return remoteDLLHandle , nil
103
104
}
104
105
105
106
func GetInjectedLibraryModuleHandle (processID uint32 , injectedDllPath string ) (uintptr , error ) {
106
107
handle , err := syscall .OpenProcess (windows .PROCESS_QUERY_INFORMATION | windows .PROCESS_VM_READ , false , processID )
107
108
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 )
109
110
}
110
111
defer syscall .CloseHandle (syscall .Handle (handle ))
111
112
112
113
var modules [1024 ]windows.Handle
113
114
var needed uint32
114
115
err = windows .EnumProcessModules (windows .Handle (handle ), & modules [0 ], uint32 (unsafe .Sizeof (modules )), & needed )
115
116
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 )
117
118
}
118
119
119
120
numModules := needed / uint32 (unsafe .Sizeof (windows .Handle (0 )))
@@ -128,16 +129,16 @@ func GetInjectedLibraryModuleHandle(processID uint32, injectedDllPath string) (u
128
129
}
129
130
130
131
func 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 )
132
133
if err != nil {
133
- return fmt .Errorf ("PID: %d - error opening process: %w" , processID , err )
134
+ return fmt .Errorf ("error opening process: %w" , err )
134
135
}
135
- defer syscall .CloseHandle (syscall .Handle (handle ))
136
+ defer syscall .CloseHandle (syscall .Handle (processHandle ))
136
137
137
138
remoteFunctionAddress := dllBaseAddress + functionRVA
138
139
139
140
threadHandle , _ , err := createRemoteThread .Call (
140
- uintptr (handle ),
141
+ uintptr (processHandle ),
141
142
0 ,
142
143
0 ,
143
144
remoteFunctionAddress ,
@@ -146,43 +147,54 @@ func callRemoteFunction(processID uint32, dllBaseAddress uintptr, functionName s
146
147
0 ,
147
148
)
148
149
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 )
150
151
}
151
152
defer syscall .CloseHandle (syscall .Handle (threadHandle ))
152
153
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
+
153
162
return nil
154
163
}
155
164
156
165
func 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 ))
158
167
processHandle , err := syscall .OpenProcess (windows .PROCESS_CREATE_THREAD | windows .PROCESS_VM_WRITE | windows .PROCESS_VM_OPERATION , false , processID )
159
168
if err != nil {
160
- return fmt .Errorf ("PID: %d - OpenProcess failed: %v" , processID , err )
169
+ return fmt .Errorf ("OpenProcess failed: %v" , err )
161
170
}
162
171
defer syscall .CloseHandle (processHandle )
163
172
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 )))
167
176
168
177
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 )
171
183
}
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 ))
173
185
174
186
FunctionRVA , err := findSymbolRVA (dllPath , dllFunction )
175
187
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 )
177
189
}
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 ))
179
191
180
192
err = callRemoteFunction (processID , dllBaseAddress , dllFunction , uintptr (FunctionRVA ))
181
193
if err != nil {
182
194
183
- return fmt .Errorf ("PID: %d - Error calling remote function %v" , processID , err )
195
+ return fmt .Errorf ("error calling remote function %v" , err )
184
196
}
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 ))
186
198
187
199
return nil
188
200
}
0 commit comments