-
Notifications
You must be signed in to change notification settings - Fork 12
/
slopfinder.js
322 lines (274 loc) · 8.33 KB
/
slopfinder.js
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
///////////////////////////////////////////////////////////////////////////////
// Globals
///////////////////////////////////////////////////////////////////////////////
var MAX_FILE_SIZE = 10*1024; // 10K
var NUM_BYTES_TO_LOAD = 16*100;
var files;
var fileData;
var numFilesRead = 0;
var numFiles = 0;
var filesOnly = false;
///////////////////////////////////////////////////////////////////////////////
// Dialog message
///////////////////////////////////////////////////////////////////////////////
function showDialog(str, title, okBtn) {
$( "#dialog-message" ).html(str);
if (okBtn) {
$( "#dialog-message" ).dialog({
title: title,
modal: true,
disabled: false,
buttons: {
Ok: function() {
$( this ).dialog( "close" );
}
}
});
} else {
$( "#dialog-message" ).dialog({
title: title,
modal: true,
disabled: false
});
}
$( "#dialog-message" ).dialog( "enable" );
$( "#dialog-message" ).dialog( "open" );
}
function removeDialog() {
$( "#dialog-message" ).dialog( "close" );
}
function showError(str) {
$( "#dialog-message" ).html("<span class=\"ui-icon ui-icon-alert\" style=\"float: left; margin: 0 7px 50px 0;\"></span>"+str);
$( "#dialog-message" ).dialog({
title: "Error",
modal: true,
disabled: false,
buttons: {
Ok: function() {
$( this ).dialog( "close" );
}
}
});
$( "#dialog-message" ).dialog( "enable" );
$( "#dialog-message" ).dialog( "open" );
}
///////////////////////////////////////////////////////////////////////////////
// File reading
///////////////////////////////////////////////////////////////////////////////
function handleFinishedRead(evt, i) {
if(evt.target.readyState == FileReader.DONE) {
var length = evt.target.result.byteLength;
var readBlock = new Uint8Array(evt.target.result, 0, length);
doRead(readBlock, length, i);
numFilesRead++;
if (numFilesRead == numFiles) {
displayResults();
}
}
}
function displayResults() {
var output = [""];
var executables = [""];
var unknownFiles = [""];
for (var i = 0; i < fileData.length; i++) {
file = fileData[i];
if (file.type == 'exe') {
style = "";
if (file.dep != "yes" || file.aslr != "yes") {
style = "style=\"background: #FF5C5C\"";
}
executables.push("<tr><td "+style+">"+escape(file.name).replace(/%20/g, " ") + "<td align=center "+style+">"+file.dep+"<td align=center "+style+">"+file.aslr+"\n");
} else {
unknownFiles.push(escape(file.name).replace(/%20/g, " ") + "<br>\n");
}
}
output.push("<hr>");
if (executables.length>1) {
output.push(""+
"<table border=0 cellpadding=0 cellspacing=0>"+
"<tr><th width=100>Executable name<th width=100>DEP<br>protection<th width=100>ASLR<br>protection</tr>"
+ executables.join("")
+"</table>");
} else {
output.push("<b>No Windows executables found</b><br>");
}
if (unknownFiles.length>1) {
output.push("<br><b>Nonexecutables</b><br>" + unknownFiles.join(""));
}
$('#analysis').html(output.join(""));
removeDialog();
}
function doRead(readBlock, length, i) {
fileData[i].type = 'unknown';
// Check for MZ header
if (readBlock[0] == 'M'.charCodeAt(0) && readBlock[1] == 'Z'.charCodeAt(0))
{
fileData[i].type = 'exe';
fileData[i].dep = '<b>NO</b>';
fileData[i].aslr = '<b>NO</b>';
// Get to DllCharacteristics data
offset = 0x3c;
e_lfanew = ((readBlock[offset+3]<<24)>>>0) +
((readBlock[offset+2]<<16)>>>0) +
((readBlock[offset+1]<<8)>>>0) +
(readBlock[offset+0]);
sizeof_magic = 4;
offset = e_lfanew + sizeof_magic;
machine =
((readBlock[offset+1]<<8)>>>0) +
(readBlock[offset+0]);
var sizeof_IMAGE_FILE_HEADER = 20;
var offset_in_IMAGE_OPTIONAL_HEADER;
if (machine == 0x014c) {
offset_in_IMAGE_OPTIONAL_HEADER = 0x46;
} else if (machine == 0x8664) {
offset_in_IMAGE_OPTIONAL_HEADER = 0x46;
} else {
fileData[i].error = "Unknown file type";
fileData[i].dep = '<b>ERROR</b>';
fileData[i].aslr = '<b>ERROR</b>';
}
offset = e_lfanew + sizeof_magic + sizeof_IMAGE_FILE_HEADER + offset_in_IMAGE_OPTIONAL_HEADER;
DllCharacteristics = ((readBlock[offset+1]<<8)>>>0) +
(readBlock[offset+0]);
if ((DllCharacteristics & 0x100) != 0) {
fileData[i].dep = 'yes';
}
if ((DllCharacteristics & 0x40) != 0) {
fileData[i].aslr = 'yes';
}
}
}
function handleFile(file, path)
{
var fileNum = numFiles;
numFiles++;
fileData[fileNum] = {};
fileData[fileNum].name = path;
fileData[fileNum].type = '';
fileData[fileNum].error = '';
reader = new FileReader();
fileData[fileNum].reader = reader;
reader.onloadend = function(evt) { handleFinishedRead(evt, fileNum); }
readFile(reader, file);
}
function handleFileTree(entry, i) {
var directoryReader = entry.createReader();
getAllEntries(
directoryReader,
readDirectory,
appendIndentList(parentNode)
);
}
function readDirectory(entries) {
for (i = 0; i < entries.length; i++) {
(function(i) {
if (entries[i].isDirectory) {
var directoryReader = entries[i].createReader();
getAllEntries(
directoryReader,
readDirectory
);
} else {
entries[i].file(function(file) {handleFile(file, entries[i].fullPath);}, errorHandler);
}
})(i);
}
}
function errorHandler(e) {
console.log('FileSystem API error code: ' + e.code)
}
function getAllEntries(directoryReader, callback) {
var entries = [];
var readEntries = function () {
directoryReader.readEntries(function (results) {
if (!results.length) {
entries.sort();
callback(entries);
} else {
entries = entries.concat(toArray(results));
readEntries();
}
}, errorHandler);
};
readEntries();
}
function toArray(list) {
return Array.prototype.slice.call(list || [], 0);
}
function handleFileSelect(evt) {
showDialog("Loading files", "Loading");
evt.stopPropagation();
evt.preventDefault();
items = evt.dataTransfer.items;
if ($.browser.webkit && (!items || !items[0] || !items[0].webkitGetAsEntry))
{
alert("You should really upgrade your browser. This site needs at least Google Chrome 21 to handle dropped folders. You can still drop files though.");
items = evt.dataTransfer.files;
filesOnly = true;
} else if ($.browser.mozilla) {
items = evt.dataTransfer.files;
filesOnly = true;
}
numFilesRead = 0;
numFiles = 0;
fileData = [];
for (var i = 0; i < items.length; i++) {
var entry = items[i];
if (filesOnly) {
handleFile(entry, entry.name);
} else {
if (entry.getAsEntry){ //Standard HTML5 API
entry = entry.getAsEntry();
} else if (entry.webkitGetAsEntry){ //WebKit implementation of HTML5 API.
entry = entry.webkitGetAsEntry();
}
if (entry.isFile){
handleFile(evt.dataTransfer.files[i], "/" + entry.name);
} else if (entry.isDirectory){
var entries = [];
entries[0] = evt.dataTransfer.items[i].webkitGetAsEntry();
readDirectory(entries);
} else {
alert("Error, unkown type given");
}
}
}
if (numFiles == 0) {
$( "#dialog-message" ).html("No files found");
}
}
function readFile(reader, file) {
end = MAX_FILE_SIZE;
var blob;
if (file.slice) {
blob = file.slice(0, end);
} else if(file.webkitSlice) {
blob = file.webkitSlice(0, end);
} else if(file.mozSlice) {
blob = file.mozSlice(0, end);
} else {
console.log("No file slicing possible in this browser");
return;
}
reader.readAsArrayBuffer(blob);
}
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy';
}
///////////////////////////////////////////////////////////////////////////////
// Main
///////////////////////////////////////////////////////////////////////////////
if ($.browser.webkit) {
$('#drop_zone').html('Drop files and folders here');
}
$(function() {
// Handler for when the page has loaded
$( "#dialog-message" ).dialog({ autoOpen: false });
});
//Setup the dnd listeners.
var dropZone = document.getElementById('container');
dropZone.addEventListener('dragover', handleDragOver, false);
dropZone.addEventListener('drop', handleFileSelect, false);