Skip to content

Commit 5c62846

Browse files
committed
handle leak fix
1 parent 60c7e35 commit 5c62846

File tree

2 files changed

+28
-31
lines changed

2 files changed

+28
-31
lines changed

src/BlackBone/Process/Process.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ NTSTATUS Process::GrantPriviledge( const std::basic_string<TCHAR>& name )
377377
return LastNtStatus();
378378
}
379379

380+
CloseHandle( hToken );
380381
return STATUS_SUCCESS;
381382
}
382383

src/BlackBone/Process/RPC/RemoteMemory.cpp

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ namespace blackbone
88
RemoteMemory::RemoteMemory( Process* process )
99
: _process( process )
1010
{
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 );
1811
}
1912

2013

@@ -184,6 +177,13 @@ NTSTATUS RemoteMemory::SetupHook( OperationType hkType )
184177
if (!pTranslated)
185178
return STATUS_INVALID_ADDRESS;
186179

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+
187187
// Listening thread
188188
if (_hThread == NULL)
189189
{
@@ -196,8 +196,7 @@ NTSTATUS RemoteMemory::SetupHook( OperationType hkType )
196196
BuildGenericHookFn( hkType );
197197

198198
// 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 ) );
201200

202201
_hooked[hkType] = true;
203202
return STATUS_SUCCESS;
@@ -243,33 +242,30 @@ bool RemoteMemory::RestoreHook( OperationType hkType )
243242
/// </summary>
244243
void RemoteMemory::reset()
245244
{
246-
if (!_mapDatabase.empty())
247-
{
248-
_active = false;
245+
_active = false;
249246

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+
}
255252

256-
if (_hPipe != NULL)
257-
{
258-
CloseHandle( _hPipe );
259-
_hPipe = NULL;
260-
}
253+
if (_hPipe != NULL)
254+
{
255+
CloseHandle( _hPipe );
256+
_hPipe = NULL;
257+
}
261258

262-
for (int i = 0; i < 4; i++)
263-
RestoreHook( (OperationType)i );
259+
for (int i = 0; i < 4; i++)
260+
RestoreHook( (OperationType)i );
264261

265-
if (!NT_SUCCESS( Unmap() ))
266-
{
267-
_mapDatabase.clear();
262+
if (!_mapDatabase.empty() && !NT_SUCCESS( Unmap() ))
263+
{
264+
_mapDatabase.clear();
268265

269-
_pSharedData = nullptr;
270-
_targetShare = 0;
271-
_targetPipe = NULL;
272-
}
266+
_pSharedData = nullptr;
267+
_targetShare = 0;
268+
_targetPipe = NULL;
273269
}
274270
}
275271

0 commit comments

Comments
 (0)