-
Notifications
You must be signed in to change notification settings - Fork 90
/
CheckSec.c
285 lines (230 loc) · 8 KB
/
CheckSec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* Poor version of checksec.sh script for PE (checks for ASLR, NX, Integrity, SEH flags and more).
*
* Compile with:
* c:\> cl CheckSec.c
* or
* $ x86_64-w64-mingw32-gcc -municode -Wall -o ~/tmp/CheckSec.exe CheckSec.c -lwintrust
*
* Collect files with:
* c:\> dir /s /b C:\*.dll > DllList.txt
* c:\> dir /s /b C:\*.exe > ExeList.txt
*
* Run with:
* c:\> CheckSec.exe -f DllList.txt > DllList_CheckSec.txt
* c:\> CheckSec.exe -f ExeList.txt > ExeList_CheckSec.txt
*
* @_hugsy_
*
*/
#define _UNICODE 1
#define UNICODE 1
#include <windows.h>
#include <softpub.h>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <wincrypt.h>
#include <wintrust.h>
#if defined(_MSC_VER)
#pragma comment (lib, "wintrust")
#endif
WORD CheckForFlags[] =
{
IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE,
IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY,
IMAGE_DLLCHARACTERISTICS_NX_COMPAT,
IMAGE_DLLCHARACTERISTICS_NO_ISOLATION,
IMAGE_DLLCHARACTERISTICS_NO_SEH,
0x0000
};
BOOL StrictRead(HANDLE hdl, LPVOID dst, DWORD len)
{
DWORD dwNumReadBytes;
if (! ReadFile(hdl, dst, len, &dwNumReadBytes, NULL)) {
wprintf(L"ReadFile failed, error %lu.\n", GetLastError());
return FALSE;
}
if (len != dwNumReadBytes) {
wprintf(L"[-] len != dwNumReadBytes\n", len, dwNumReadBytes);
return FALSE;
}
return TRUE;
}
int CheckFileSigned(LPCWSTR pwszSourceFile)
{
LONG lStatus;
DWORD dwLastError;
WINTRUST_FILE_INFO FileData;
GUID WVTPolicyGUID = WINTRUST_ACTION_GENERIC_VERIFY_V2;
WINTRUST_DATA WinTrustData;
memset(&FileData, 0, sizeof(FileData));
FileData.cbStruct = sizeof(WINTRUST_FILE_INFO);
FileData.pcwszFilePath = pwszSourceFile;
FileData.hFile = NULL;
FileData.pgKnownSubject = NULL;
memset(&WinTrustData, 0, sizeof(WinTrustData));
WinTrustData.cbStruct = sizeof(WinTrustData);
WinTrustData.pPolicyCallbackData = NULL;
WinTrustData.pSIPClientData = NULL;
WinTrustData.dwUIChoice = WTD_UI_NONE;
WinTrustData.fdwRevocationChecks = WTD_REVOKE_NONE;
WinTrustData.dwUnionChoice = WTD_CHOICE_FILE;
WinTrustData.dwStateAction = WTD_STATEACTION_VERIFY;
WinTrustData.hWVTStateData = NULL;
WinTrustData.pwszURLReference = NULL;
WinTrustData.dwUIContext = 0;
WinTrustData.pFile = &FileData;
lStatus = WinVerifyTrust(NULL, &WVTPolicyGUID, &WinTrustData);
switch (lStatus)
{
case ERROR_SUCCESS:
wprintf(L"OK");
break;
case TRUST_E_NOSIGNATURE:
dwLastError = GetLastError();
if (TRUST_E_NOSIGNATURE == dwLastError ||
TRUST_E_SUBJECT_FORM_UNKNOWN == dwLastError ||
TRUST_E_PROVIDER_UNKNOWN == dwLastError) {
wprintf(L"NotSigned");
} else {
wprintf(L"VerifyFailed");
}
break;
case TRUST_E_EXPLICIT_DISTRUST:
wprintf(L"SigNotAllowed");
break;
case TRUST_E_SUBJECT_NOT_TRUSTED:
case CRYPT_E_SECURITY_SETTINGS:
wprintf(L"SigNotTrusted");
break;
default:
wprintf(L"Error: 0x%x", lStatus);
break;
}
WinTrustData.dwStateAction = WTD_STATEACTION_CLOSE;
lStatus = WinVerifyTrust(NULL, &WVTPolicyGUID, &WinTrustData);
return 0;
}
VOID CheckSecFile( LPCWSTR sFileName )
{
HANDLE hPeFile;
DWORD off;
DWORD MoreDosHeader[16];
ULONG ntSignature;
IMAGE_DOS_HEADER bImageDosHeader;
IMAGE_FILE_HEADER bImageFileHeader;
IMAGE_OPTIONAL_HEADER bImageOptionalHeader;
WORD *flag;
hPeFile = CreateFile(sFileName, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
NULL);
if (hPeFile == INVALID_HANDLE_VALUE) {
wprintf(L"[-] Failed to open '%s', error %lu\n", sFileName, GetLastError());
return;
}
if (!StrictRead(hPeFile, &bImageDosHeader, sizeof(IMAGE_DOS_HEADER))) {
goto end;
}
if (bImageDosHeader.e_magic != IMAGE_DOS_SIGNATURE) {
wprintf(L"[-] Invalid DOS signature.\n");
goto end;
}
if (!StrictRead(hPeFile, MoreDosHeader, sizeof(MoreDosHeader))) {
goto end;
}
off = SetFilePointer(hPeFile, bImageDosHeader.e_lfanew, NULL, FILE_BEGIN);
if (off == INVALID_SET_FILE_POINTER) {
wprintf(L"SetFilePointer failed, error %lu.\n", GetLastError());
goto end;
}
off+= sizeof(ULONG);
if (!StrictRead(hPeFile, &ntSignature, sizeof(ULONG))) {
goto end;
}
if (ntSignature != IMAGE_NT_SIGNATURE) {
wprintf(L"[-] Missing NT signature (got %x, expected %x)\n", ntSignature, IMAGE_NT_SIGNATURE);
goto end;
}
if (!StrictRead(hPeFile, &bImageFileHeader, IMAGE_SIZEOF_FILE_HEADER)) {
goto end;
}
if (!StrictRead(hPeFile, &bImageOptionalHeader, sizeof(IMAGE_OPTIONAL_HEADER))) {
goto end;
}
wprintf(L"%s ; ", sFileName);
for(flag = &CheckForFlags[0]; *flag; flag++) {
wprintf(L"%s ; ", (bImageOptionalHeader.DllCharacteristics & *flag) ? L"Yes" : L"No");
}
CheckFileSigned( sFileName );
wprintf(L"; ");
wprintf(L" \n");
end:
if (! CloseHandle(hPeFile) ) {
wprintf(L"[-] Error while closing %s: %s\n", sFileName, GetLastError());
}
return;
}
int CheckSecList( LPCWSTR sList )
{
HANDLE hDllList;
hDllList = CreateFile(sList, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hDllList == INVALID_HANDLE_VALUE) {
wprintf(L"[-] Cannot open %s, error %lu\n", sList, GetLastError());
return 1;
}
while( 1 ) {
_TCHAR line[MAX_PATH+1] = {0, };
DWORD dwNumReadBytes = 0;
int i = 0;
_TCHAR c = 0;
while( 1 ) {
if (! ReadFile(hDllList, &c, 1, &dwNumReadBytes, NULL) )
break;
if (dwNumReadBytes == 0)
break;
if (c != '\r' && c != '\n')
line[i++] = c;
else
break;
if (i==MAX_PATH)
break;
}
if(!dwNumReadBytes)
break;
if (wcslen(line)){
CheckSecFile( line );
}
}
if (! CloseHandle(hDllList) ) {
wprintf(L"[-] Error while closing %s: %s\n", sList, GetLastError());
}
return 0;
}
int wmain(int argc, wchar_t** argv, wchar_t** envp)
{
UNREFERENCED_PARAMETER(envp);
wchar_t **wsArgs;
wprintf(L"[+] %s: check DLL/EXE for DEP/ASLR flag\n", argv[0]);
if(argc < 2) {
wprintf(L"[-] Missing EXE/DLL path\nSyntax:\n\t");
wprintf(L"%s \\path\\to\\exe [\\moar\\exe\\here]\n", argv[0]);
wprintf(L"or\n\t");
wprintf(L"%s -f \\path\\to\\dlllist.txt\n", argv[0]);
return 1;
}
wprintf(L"Filename ; ASLR ; ForceIntegrity ; DEP ; NoIsolation ; NoSEH ; IsSigned ;\n");
if (argc==3){
if (wcscmp(argv[1], L"-f")==0) {
CheckSecList( argv[2] );
return 0;
}
wprintf(L"Incorrect syntax: %s -f \\Path\\To\\DllList.txt\n", argv[0]);
return 1;
}
for (wsArgs = argv+1; *wsArgs; wsArgs++){
CheckSecFile( *wsArgs );
}
return 0;
}