Skip to content

Commit 7560ee9

Browse files
committed
JIT fixes and more improvements (admin check etc.)
1 parent 4458ca9 commit 7560ee9

File tree

7 files changed

+66
-3
lines changed

7 files changed

+66
-3
lines changed

x64_dbg_dbg/_dbgfunctions.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ static bool _getjitauto(bool* jit_auto)
112112
return dbggetjitauto(jit_auto, notfound, NULL, NULL);
113113
}
114114

115+
static bool _isprocesselevated(void)
116+
{
117+
return IsProcessElevated();
118+
}
119+
115120
static bool _getpagerights(uint* addr, char* rights)
116121
{
117122
return dbggetpagerights(addr, rights);
@@ -198,4 +203,5 @@ void dbgfunctionsinit()
198203
_dbgfunctions.GetPageRights = _getpagerights;
199204
_dbgfunctions.SetPageRights = _setpagerights;
200205
_dbgfunctions.PageRightsToString = _pagerightstostring;
206+
_dbgfunctions.IsProcessElevated = _isprocesselevated;
201207
}

x64_dbg_dbg/_dbgfunctions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ typedef bool (*GETPROCESSLIST)(DBGPROCESSINFO** entries, int* count);
6060
typedef bool (*GETPAGERIGHTS)(duint*, char*);
6161
typedef bool (*SETPAGERIGHTS)(duint*, char*);
6262
typedef bool (*PAGERIGHTSTOSTRING)(DWORD, char*);
63+
typedef bool (*ISPROCESSELEVATED)(void);
6364

6465
typedef struct DBGFUNCTIONS_
6566
{
@@ -90,6 +91,7 @@ typedef struct DBGFUNCTIONS_
9091
GETPAGERIGHTS GetPageRights;
9192
SETPAGERIGHTS SetPageRights;
9293
PAGERIGHTSTOSTRING PageRightsToString;
94+
ISPROCESSELEVATED IsProcessElevated;
9395
} DBGFUNCTIONS;
9496

9597
#ifdef BUILD_DBG

x64_dbg_dbg/debugger.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,21 @@ void cbDetach()
14801480
return;
14811481
}
14821482

1483+
1484+
bool IsProcessElevated()
1485+
{
1486+
1487+
HANDLE hToken;
1488+
DWORD tkInfoLen;
1489+
TOKEN_ELEVATION tkElevation;
1490+
1491+
OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &hToken);
1492+
1493+
GetTokenInformation(hToken, TokenElevation, &tkElevation, sizeof(tkElevation), &tkInfoLen);
1494+
1495+
return (tkElevation.TokenIsElevated != 0);
1496+
}
1497+
14831498
bool _readwritejitkey(char* jit_key_value, DWORD* jit_key_vale_size, char* key, arch arch_in, arch* arch_out, readwritejitkey_error_t* error, bool write)
14841499
{
14851500
DWORD key_flags;
@@ -1491,7 +1506,15 @@ bool _readwritejitkey(char* jit_key_value, DWORD* jit_key_vale_size, char* key,
14911506
* error = ERROR_RW;
14921507

14931508
if(write)
1509+
{
1510+
if(!IsProcessElevated())
1511+
{
1512+
if(error != NULL)
1513+
* error = ERROR_RW_NOTADMIN;
1514+
return false;
1515+
}
14941516
key_flags = KEY_WRITE;
1517+
}
14951518
else
14961519
key_flags = KEY_READ;
14971520

@@ -1512,7 +1535,7 @@ bool _readwritejitkey(char* jit_key_value, DWORD* jit_key_vale_size, char* key,
15121535

15131536
if(arch_in == x64)
15141537
{
1515-
#ifdef _WIN32
1538+
#ifndef _WIN64
15161539
if(!IsWow64())
15171540
{
15181541
if(error != NULL)

x64_dbg_dbg/debugger.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ typedef enum
1616
{
1717
ERROR_RW = 0,
1818
ERROR_RW_FILE_NOT_FOUND,
19-
ERROR_RW_NOTWOW64
19+
ERROR_RW_NOTWOW64,
20+
ERROR_RW_NOTADMIN
2021
} readwritejitkey_error_t;
2122

2223
//structures
@@ -74,6 +75,7 @@ bool _readwritejitkey(char*, DWORD*, char*, arch, arch*, readwritejitkey_error_t
7475
bool dbggetjitauto(bool*, arch, arch*, readwritejitkey_error_t*);
7576
bool dbgsetjitauto(bool, arch, arch*, readwritejitkey_error_t*);
7677
bool dbglistprocesses(std::vector<PROCESSENTRY32>* list);
78+
bool IsProcessElevated();
7779

7880
void cbStep();
7981
void cbRtrStep();

x64_dbg_dbg/debugger_commands.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,11 @@ CMDRESULT cbDebugSetJITAuto(int argc, char* argv[])
14371437
{
14381438
arch actual_arch;
14391439
bool set_jit_auto;
1440+
if(!IsProcessElevated())
1441+
{
1442+
dprintf("Error run the debugger as Admin to setjitauto\n");
1443+
return STATUS_ERROR;
1444+
}
14401445
if(argc < 2)
14411446
{
14421447
dprintf("Error setting JIT Auto. Use ON:1 or OFF:0 arg or x64/x32, ON:1 or OFF:0.\n");
@@ -1512,6 +1517,11 @@ CMDRESULT cbDebugSetJIT(int argc, char* argv[])
15121517
arch actual_arch;
15131518
char* jit_debugger_cmd;
15141519
char oldjit[MAX_SETTING_SIZE] = "";
1520+
if(!IsProcessElevated())
1521+
{
1522+
dprintf("Error run the debugger as Admin to setjit\n");
1523+
return STATUS_ERROR;
1524+
}
15151525
if(argc < 2)
15161526
{
15171527
char path[JIT_ENTRY_DEF_SIZE];
@@ -1619,7 +1629,7 @@ CMDRESULT cbDebugSetJIT(int argc, char* argv[])
16191629
if(rw_error == ERROR_RW_NOTWOW64)
16201630
dprintf("Error using x64 arg. The debugger is not a WOW64 process\n");
16211631
else
1622-
dprintf("Error getting JIT %s\n", (actual_arch == x64) ? "x64" : "x32");
1632+
dprintf("Error setting JIT %s\n", (actual_arch == x64) ? "x64" : "x32");
16231633
return STATUS_ERROR;
16241634
}
16251635
}

x64_dbg_gui/Project/Src/Gui/SettingsDialog.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@ void SettingsDialog::LoadSettings()
191191

192192
ui->chkConfirmBeforeAtt->setCheckState(bool2check(settings.miscSetJITAuto));
193193
}
194+
195+
if(!DbgFunctions()->IsProcessElevated())
196+
{
197+
ui->chkSetJIT->setDisabled(true);
198+
ui->chkConfirmBeforeAtt->setDisabled(true);
199+
ui->lbladminwarning->setText(QString("Warning: Run the debugger as Admin to enable JIT."));
200+
}
194201
}
195202
}
196203
bJitOld = settings.miscSetJIT;

x64_dbg_gui/Project/Src/Gui/SettingsDialog.ui

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,19 @@
516516
<string>Confirm before attaching</string>
517517
</property>
518518
</widget>
519+
<widget class="QLabel" name="lbladminwarning">
520+
<property name="geometry">
521+
<rect>
522+
<x>10</x>
523+
<y>80</y>
524+
<width>271</width>
525+
<height>31</height>
526+
</rect>
527+
</property>
528+
<property name="text">
529+
<string/>
530+
</property>
531+
</widget>
519532
</widget>
520533
</widget>
521534
<widget class="QPushButton" name="btnSave">

0 commit comments

Comments
 (0)