Skip to content

Commit 57c3279

Browse files
committed
DBG+GUI: fixed some stuff with HandlesView
1 parent 1bff3d5 commit 57c3279

File tree

4 files changed

+38
-62
lines changed

4 files changed

+38
-62
lines changed

src/dbg/enumhandles.cpp

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -57,59 +57,46 @@ DWORD(__stdcall* NtQueryObject)(HANDLE ObjectHandle, ULONG ObjectInformationClas
5757

5858
extern "C" DLL_EXPORT long _dbg_enumhandles(duint* handles, unsigned char* typeNumbers, unsigned int* grantedAccess, unsigned int maxcount)
5959
{
60-
MYHANDLES* myhandles = (MYHANDLES*)emalloc(16384, "_dbg_enumhandles");
60+
Memory<MYHANDLES*> myhandles(16 * 1024 * 1024, "_dbg_enumhandles");
6161
DWORD size = 16384;
6262
DWORD errcode = 0xC0000004;
6363
if(NtQuerySystemInformation == nullptr)
6464
*(FARPROC*)&NtQuerySystemInformation = GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "NtQuerySystemInformation");
6565
while(errcode == 0xC0000004)
6666
{
67-
errcode = NtQuerySystemInformation(16, myhandles, size, &size);
68-
if(errcode == 0xC0000004)
69-
{
70-
myhandles = (MYHANDLES*)erealloc(myhandles, size + 16384, "_dbg_enumhandles");
71-
size += 16384;
72-
}
73-
else
74-
{
67+
errcode = NtQuerySystemInformation(16, myhandles(), size, &size);
68+
if(errcode != 0xC0000004)
7569
break;
76-
}
70+
myhandles.realloc(myhandles.size() * 2, "_dbg_enumhandles");
7771
}
7872
if(errcode != 0)
79-
{
80-
efree(myhandles, "_dbg_enumhandles");
8173
return 0;
82-
}
83-
else
74+
75+
unsigned int j = 0;
76+
for(unsigned int i = 0; i < myhandles()->HandleCount; i++)
8477
{
85-
unsigned int j = 0;
86-
for(unsigned int i = 0; i < myhandles->HandleCount; i++)
78+
DWORD pid = fdProcessInfo->dwProcessId;
79+
if(myhandles()->Handles[i].ProcessId == pid)
8780
{
88-
DWORD pid = fdProcessInfo->dwProcessId;
89-
if(myhandles->Handles[i].ProcessId == pid)
90-
{
91-
handles[j] = myhandles->Handles[j].Handle;
92-
typeNumbers[j] = myhandles->Handles[j].ObjectTypeNumber;
93-
grantedAccess[j] = myhandles->Handles[j].GrantedAccess;
94-
if(++j == maxcount) break;
95-
}
81+
handles[j] = myhandles()->Handles[j].Handle;
82+
typeNumbers[j] = myhandles()->Handles[j].ObjectTypeNumber;
83+
grantedAccess[j] = myhandles()->Handles[j].GrantedAccess;
84+
if(++j == maxcount) break;
9685
}
97-
efree(myhandles, "_dbg_enumhandles");
98-
return j;
9986
}
87+
return j;
10088
}
10189

10290
extern "C" DLL_EXPORT bool _dbg_gethandlename(char* name, char* typeName, size_t buffersize, duint remotehandle)
10391
{
10492
HANDLE hLocalHandle;
10593
if(typeName && DuplicateHandle(fdProcessInfo->hProcess, (HANDLE)remotehandle, GetCurrentProcess(), &hLocalHandle, DUPLICATE_SAME_ACCESS, FALSE, 0))
10694
{
107-
OBJECT_TYPE_INFORMATION* objectTypeInfo = (OBJECT_TYPE_INFORMATION*)emalloc(128, "_dbg_gethandlename");
95+
Memory<OBJECT_TYPE_INFORMATION*> objectTypeInfo(128, "_dbg_gethandlename");
10896
if(NtQueryObject == nullptr)
10997
*(FARPROC*)&NtQueryObject = GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "NtQueryObject");
110-
if(NtQueryObject(hLocalHandle, 2, objectTypeInfo, 128, NULL) >= 0)
111-
strcpy_s(typeName, buffersize, StringUtils::Utf16ToUtf8(objectTypeInfo->Name.Buffer).c_str());
112-
efree(objectTypeInfo, "_dbg_gethandlename");
98+
if(NtQueryObject(hLocalHandle, 2, objectTypeInfo(), 128, NULL) >= 0)
99+
strcpy_s(typeName, buffersize, StringUtils::Utf16ToUtf8(objectTypeInfo()->Name.Buffer).c_str());
113100
CloseHandle(hLocalHandle);
114101
}
115102
wchar_t* buffer;

src/dbg/instruction.cpp

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2392,47 +2392,37 @@ CMDRESULT cbInstrMnemonicbrief(int argc, char* argv[])
23922392
return STATUS_CONTINUE;
23932393
}
23942394

2395-
23962395
CMDRESULT cbGetPrivilegeState(int argc, char* argv[])
23972396
{
2398-
TOKEN_PRIVILEGES* Privileges;
23992397
DWORD returnLength;
24002398
LUID luid;
24012399
if(LookupPrivilegeValueW(nullptr, StringUtils::Utf8ToUtf16(argv[1]).c_str(), &luid) == 0)
24022400
{
24032401
varset("$result", (duint)0, false);
24042402
return CMDRESULT::STATUS_CONTINUE;
24052403
}
2406-
Privileges = (TOKEN_PRIVILEGES*)emalloc(64 * 16 + 8, "_dbg_getprivilegestate");
2407-
if(GetTokenInformation(hProcessToken, TokenPrivileges, Privileges, 64 * 16 + 8, &returnLength) == 0)
2404+
Memory <TOKEN_PRIVILEGES*> Privileges(64 * 16 + 8, "_dbg_getprivilegestate");
2405+
if(GetTokenInformation(hProcessToken, TokenPrivileges, Privileges(), 64 * 16 + 8, &returnLength) == 0)
24082406
{
24092407
if(returnLength > 4 * 1024 * 1024)
24102408
{
24112409
varset("$result", (duint)0, false);
24122410
return CMDRESULT::STATUS_CONTINUE;
24132411
}
2414-
Privileges = (TOKEN_PRIVILEGES*)erealloc(Privileges, returnLength, "_dbg_getprivilegestate");
2415-
if(GetTokenInformation(hProcessToken, TokenPrivileges, Privileges, returnLength, &returnLength) == 0)
2416-
{
2417-
efree(Privileges, "_dbg_getprivilegestate");
2412+
Privileges.realloc(returnLength, "_dbg_getprivilegestate");
2413+
if(GetTokenInformation(hProcessToken, TokenPrivileges, Privileges(), returnLength, &returnLength) == 0)
24182414
return STATUS_ERROR;
2419-
}
24202415
}
2421-
for(unsigned int i = 0; i < Privileges->PrivilegeCount; i++)
2416+
for(unsigned int i = 0; i < Privileges()->PrivilegeCount; i++)
24222417
{
24232418
if(4 + sizeof(LUID_AND_ATTRIBUTES) * i > returnLength)
2424-
{
2425-
efree(Privileges, "_dbg_getprivilegestate");
24262419
return STATUS_ERROR;
2427-
}
2428-
if(memcmp(&Privileges->Privileges[i].Luid, &luid, sizeof(LUID)) == 0)
2420+
if(memcmp(&Privileges()->Privileges[i].Luid, &luid, sizeof(LUID)) == 0)
24292421
{
2430-
efree(Privileges, "_dbg_getprivilegestate");
2431-
varset("$result", (duint)(Privileges->Privileges[i].Attributes + 1), false); // 2=enabled, 3=default, 1=disabled
2422+
varset("$result", (duint)(Privileges()->Privileges[i].Attributes + 1), false); // 2=enabled, 3=default, 1=disabled
24322423
return STATUS_CONTINUE;
24332424
}
24342425
}
2435-
efree(Privileges, "_dbg_getprivilegestate");
24362426
varset("$result", (duint)0, false);
24372427
return STATUS_CONTINUE;
24382428
}
@@ -2445,13 +2435,11 @@ CMDRESULT cbEnablePrivilege(int argc, char* argv[])
24452435
dprintf("Could not find the specified privilege: %s\n", argv[1]);
24462436
return CMDRESULT::STATUS_ERROR;
24472437
}
2448-
TOKEN_PRIVILEGES* Privilege;
2449-
Privilege = (TOKEN_PRIVILEGES*)emalloc(sizeof(LUID_AND_ATTRIBUTES) + 4, "_dbg_enableprivilege");
2450-
Privilege->PrivilegeCount = 1;
2451-
Privilege->Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
2452-
Privilege->Privileges[0].Luid = luid;
2453-
bool ret = AdjustTokenPrivileges(hProcessToken, FALSE, Privilege, sizeof(LUID_AND_ATTRIBUTES) + 4, nullptr, nullptr) != NO_ERROR;
2454-
efree(Privilege, "_dbg_enableprivilege");
2438+
Memory<TOKEN_PRIVILEGES*> Privilege(sizeof(LUID_AND_ATTRIBUTES), "_dbg_enableprivilege");
2439+
Privilege()->PrivilegeCount = 1;
2440+
Privilege()->Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
2441+
Privilege()->Privileges[0].Luid = luid;
2442+
bool ret = AdjustTokenPrivileges(hProcessToken, FALSE, Privilege(), sizeof(LUID_AND_ATTRIBUTES) + 4, nullptr, nullptr) != NO_ERROR;
24552443
return ret ? CMDRESULT::STATUS_CONTINUE : CMDRESULT::STATUS_CONTINUE;
24562444
}
24572445

@@ -2463,12 +2451,10 @@ CMDRESULT cbDisablePrivilege(int argc, char* argv[])
24632451
dprintf("Could not find the specified privilege: %s\n", argv[1]);
24642452
return CMDRESULT::STATUS_ERROR;
24652453
}
2466-
TOKEN_PRIVILEGES* Privilege;
2467-
Privilege = (TOKEN_PRIVILEGES*)emalloc(sizeof(LUID_AND_ATTRIBUTES) + 4, "_dbg_disableprivilege");
2468-
Privilege->PrivilegeCount = 1;
2469-
Privilege->Privileges[0].Attributes = 0;
2470-
Privilege->Privileges[0].Luid = luid;
2471-
bool ret = AdjustTokenPrivileges(hProcessToken, FALSE, Privilege, sizeof(LUID_AND_ATTRIBUTES) + 4, nullptr, nullptr) != NO_ERROR;
2472-
efree(Privilege, "_dbg_disableprivilege");
2454+
Memory<TOKEN_PRIVILEGES*> Privilege(sizeof(LUID_AND_ATTRIBUTES), "_dbg_disableprivilege");
2455+
Privilege()->PrivilegeCount = 1;
2456+
Privilege()->Privileges[0].Attributes = 0;
2457+
Privilege()->Privileges[0].Luid = luid;
2458+
bool ret = AdjustTokenPrivileges(hProcessToken, FALSE, Privilege(), sizeof(LUID_AND_ATTRIBUTES) + 4, nullptr, nullptr) != NO_ERROR;
24732459
return ret ? CMDRESULT::STATUS_CONTINUE : CMDRESULT::STATUS_CONTINUE;
24742460
}

src/dbg/x64_dbg.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ static void registercommands()
268268
dbgcmdnew("scriptdll\1dllscript", cbScriptDll, false); //execute a script DLL
269269
dbgcmdnew("mnemonichelp", cbInstrMnemonichelp, false); //mnemonic help
270270
dbgcmdnew("mnemonicbrief", cbInstrMnemonicbrief, false); //mnemonic brief
271+
dbgcmdnew("GetPrivilegeState", cbGetPrivilegeState, true); //get priv state
272+
dbgcmdnew("EnablePrivilege", cbEnablePrivilege, true); //enable priv
273+
dbgcmdnew("DisablePrivilege", cbDisablePrivilege, true); //disable priv
271274
}
272275

273276
static bool cbCommandProvider(char* cmd, int maxlen)

src/gui/Src/Gui/HandlesView.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ void HandlesView::enumPrivileges()
275275

276276
void HandlesView::AppendPrivilege(int row, const char* PrivilegeString)
277277
{
278-
DbgCmdExec(QString("GetPrivilegeState \"%1\"").arg(PrivilegeString).toUtf8().constData());
278+
DbgCmdExecDirect(QString("GetPrivilegeState \"%1\"").arg(PrivilegeString).toUtf8().constData());
279279
mPrivilegesTable->setCellContent(row, 0, QString(PrivilegeString));
280280
switch(DbgValFromString("$result"))
281281
{

0 commit comments

Comments
 (0)