@@ -8,13 +8,6 @@ namespace blackbone
8
8
RemoteMemory::RemoteMemory ( Process* process )
9
9
: _process( process )
10
10
{
11
- // Generate unique pipe name
12
- _pipeName = Utils::RandomANString () +
13
- L" _" + std::to_wstring ( process->pid () ) +
14
- L" _" + std::to_wstring ( GetCurrentProcessId () );
15
-
16
- // Setup pipe
17
- _hPipe = CreateNamedPipeW ( (L" \\\\ .\\ pipe\\ " + _pipeName).c_str (), PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE, 1 , 0 , 0 , 0 , NULL );
18
11
}
19
12
20
13
@@ -184,6 +177,13 @@ NTSTATUS RemoteMemory::SetupHook( OperationType hkType )
184
177
if (!pTranslated)
185
178
return STATUS_INVALID_ADDRESS;
186
179
180
+ // IPC
181
+ if (_hPipe == NULL )
182
+ {
183
+ _pipeName = Utils::RandomANString () + L" _" + std::to_wstring ( _process->pid () ) + L" _" + std::to_wstring ( GetCurrentProcessId () );
184
+ _hPipe = CreateNamedPipeW ( (L" \\\\ .\\ pipe\\ " + _pipeName).c_str (), PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE, 1 , 0 , 0 , 0 , NULL );
185
+ }
186
+
187
187
// Listening thread
188
188
if (_hThread == NULL )
189
189
{
@@ -196,8 +196,7 @@ NTSTATUS RemoteMemory::SetupHook( OperationType hkType )
196
196
BuildGenericHookFn ( hkType );
197
197
198
198
// Copy hook
199
- memcpy ( pTranslated, (uint8_t *)_pSharedData + sizeof ( HookData ) * hkType + FIELD_OFFSET ( HookData, jump_buf),
200
- sizeof ( _pSharedData->hkVirtualAlloc .jump_buf ) );
199
+ memcpy ( pTranslated, (uint8_t *)_pSharedData + sizeof ( HookData ) * hkType + FIELD_OFFSET ( HookData, jump_buf ), sizeof ( _pSharedData->hkVirtualAlloc .jump_buf ) );
201
200
202
201
_hooked[hkType] = true ;
203
202
return STATUS_SUCCESS;
@@ -243,33 +242,30 @@ bool RemoteMemory::RestoreHook( OperationType hkType )
243
242
// / </summary>
244
243
void RemoteMemory::reset ()
245
244
{
246
- if (!_mapDatabase.empty ())
247
- {
248
- _active = false ;
245
+ _active = false ;
249
246
250
- if (_hThread != NULL )
251
- {
252
- TerminateThread ( _hThread, 0 );
253
- _hThread = NULL ;
254
- }
247
+ if (_hThread != NULL )
248
+ {
249
+ TerminateThread ( _hThread, 0 );
250
+ _hThread = NULL ;
251
+ }
255
252
256
- if (_hPipe != NULL )
257
- {
258
- CloseHandle ( _hPipe );
259
- _hPipe = NULL ;
260
- }
253
+ if (_hPipe != NULL )
254
+ {
255
+ CloseHandle ( _hPipe );
256
+ _hPipe = NULL ;
257
+ }
261
258
262
- for (int i = 0 ; i < 4 ; i++)
263
- RestoreHook ( (OperationType)i );
259
+ for (int i = 0 ; i < 4 ; i++)
260
+ RestoreHook ( (OperationType)i );
264
261
265
- if (!NT_SUCCESS ( Unmap () ))
266
- {
267
- _mapDatabase.clear ();
262
+ if (!_mapDatabase. empty () && !NT_SUCCESS ( Unmap () ))
263
+ {
264
+ _mapDatabase.clear ();
268
265
269
- _pSharedData = nullptr ;
270
- _targetShare = 0 ;
271
- _targetPipe = NULL ;
272
- }
266
+ _pSharedData = nullptr ;
267
+ _targetShare = 0 ;
268
+ _targetPipe = NULL ;
273
269
}
274
270
}
275
271
0 commit comments