Skip to content

Commit a14ef05

Browse files
committed
bugfix thanks to esmid, added some bounds checking
1 parent 201fcdf commit a14ef05

1 file changed

Lines changed: 42 additions & 23 deletions

File tree

checksec/plugin.cpp

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ void CheckModules(HWND hDlg) {
182182
ListInfo *list = new ListInfo;
183183
Module::GetList(list);
184184
LVITEM lvi;
185-
185+
bool is_peplus = false;
186+
186187
HWND hListView = GetDlgItem(hDlg, IDC_LIST);
187188
ZeroMemory(&lvi, sizeof(lvi));
188189
lvi.mask = LVIF_TEXT | LVIF_PARAM;
@@ -245,7 +246,12 @@ void CheckModules(HWND hDlg) {
245246
PIMAGE_FILE_HEADER pFileHeader = (PIMAGE_FILE_HEADER)&pNTHeader->FileHeader;
246247

247248
WORD DllCharacteristics;
248-
if (pFileHeader->Machine == IMAGE_FILE_MACHINE_I386) {
249+
PIMAGE_OPTIONAL_HEADER pOptionalHeader_temp = (PIMAGE_OPTIONAL_HEADER)&pNTHeader->OptionalHeader;
250+
if (pOptionalHeader_temp->Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
251+
is_peplus = true;
252+
}
253+
254+
if (!is_peplus) {
249255
PIMAGE_OPTIONAL_HEADER32 pOptionalHeader = (PIMAGE_OPTIONAL_HEADER32)&pNTHeader->OptionalHeader;
250256
DllCharacteristics = pOptionalHeader->DllCharacteristics;
251257
}
@@ -267,35 +273,48 @@ void CheckModules(HWND hDlg) {
267273
has_SAFESEH = STATUS_NO;
268274
}
269275

270-
PIMAGE_DATA_DIRECTORY pConfigDataDirectory = &pNTHeader->OptionalHeader.DataDirectory[10];
271-
if (pConfigDataDirectory->VirtualAddress != 0) {
272-
if (pFileHeader->Machine == IMAGE_FILE_MACHINE_I386) {
273-
PIMAGE_LOAD_CONFIG_DIRECTORY32 pLoadConfig = MakePtr(PIMAGE_LOAD_CONFIG_DIRECTORY32, dosHeader, pConfigDataDirectory->VirtualAddress);
276+
if (pNTHeader->OptionalHeader.NumberOfRvaAndSizes < 10){
277+
has_GS = STATUS_NO;
274278

275-
if (pLoadConfig->SecurityCookie != 0) has_GS = STATUS_YES;
276-
else has_GS = STATUS_NO;
279+
if (!is_peplus) has_SAFESEH = STATUS_NO;
280+
else has_SAFESEH = STATUS_NA;
281+
}
282+
else {
283+
PIMAGE_DATA_DIRECTORY pConfigDataDirectory = &pNTHeader->OptionalHeader.DataDirectory[10];
284+
if (pConfigDataDirectory->VirtualAddress != 0) {
285+
if (!is_peplus) {
286+
if(pConfigDataDirectory->VirtualAddress > GetFileSize(hFile, 0)){
287+
has_GS = STATUS_ERR;
288+
has_SAFESEH = STATUS_ERR;
289+
}
290+
else {
291+
PIMAGE_LOAD_CONFIG_DIRECTORY32 pLoadConfig = MakePtr(PIMAGE_LOAD_CONFIG_DIRECTORY32, dosHeader, pConfigDataDirectory->VirtualAddress);
292+
293+
if (pLoadConfig->SecurityCookie != 0) has_GS = STATUS_YES;
294+
else has_GS = STATUS_NO;
295+
296+
if (strcmp(has_SAFESEH, STATUS_ERR) == 0) {
297+
if (pLoadConfig->SEHandlerTable != 0) has_SAFESEH = STATUS_YES;
298+
else has_SAFESEH = STATUS_OFF;
299+
}
300+
}
301+
}
302+
else {
303+
PIMAGE_LOAD_CONFIG_DIRECTORY64 pLoadConfig = MakePtr(PIMAGE_LOAD_CONFIG_DIRECTORY64, dosHeader, pConfigDataDirectory->VirtualAddress);
277304

278-
if (strcmp(has_SAFESEH, STATUS_ERR) == 0) {
279-
if (pLoadConfig->SEHandlerTable != 0) has_SAFESEH = STATUS_YES;
280-
else has_SAFESEH = STATUS_OFF;
305+
if (pLoadConfig->SecurityCookie != 0) has_GS = STATUS_YES;
306+
else has_GS = STATUS_NO;
307+
308+
has_SAFESEH = STATUS_NA; //Not applicable for 64bit
281309
}
282310
}
283311
else {
284-
PIMAGE_LOAD_CONFIG_DIRECTORY64 pLoadConfig = MakePtr(PIMAGE_LOAD_CONFIG_DIRECTORY64, dosHeader, pConfigDataDirectory->VirtualAddress);
285-
286-
if (pLoadConfig->SecurityCookie != 0) has_GS = STATUS_YES;
287-
else has_GS = STATUS_NO;
312+
if (pFileHeader->Machine == IMAGE_FILE_MACHINE_I386) has_SAFESEH = STATUS_NO;
313+
else has_SAFESEH = STATUS_NA; //Not applicable for 64bit
288314

289-
//Not applicable for 64bit
290-
has_SAFESEH = STATUS_NA;
315+
has_GS = STATUS_NO;
291316
}
292317
}
293-
else {
294-
if (pFileHeader->Machine == IMAGE_FILE_MACHINE_I386) has_SAFESEH = STATUS_NO;
295-
else has_SAFESEH = STATUS_NA; //Not applicable for 64bit
296-
297-
has_GS = STATUS_NO;
298-
}
299318
}
300319
}
301320

0 commit comments

Comments
 (0)