-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
430 lines (334 loc) · 14.8 KB
/
Copy pathserver.js
File metadata and controls
430 lines (334 loc) · 14.8 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
var express = require('express');
var fs = require("fs");
const nmap = require('node-nmap');
const dns = require('dns');
var app = express();
app.use(express.static('public'));
app.use(express.static("assets"));
app.use(express.static("js"));
app.use(express.json());
//NOTE: Here you need to change if linux or windows. Make sure to point to the executable/binary,
//NOTE: Make sure that you are not pointing to the setup installer
//NOTE: Make sure that you are using \\ for windows and / for linux
nmap.nmapLocation = "C:\\Program Files (x86)\\Nmap\\Nmap.exe"; //default Windows install
// nmap.nmapLocation = "/usr/bin"; //default Linux folder
//NOTE: If poiting to the binary doesn't work, try pointing to the folder.
//NOTE: Make sure only one instance of nmap.nmapLocation is active
//TODO: We want to change this to {} so to avoid issues with race conditions on list deletion
var runningScans = [];
var finishedScans = getPersistentReports();
app.get("/index", (req, resp) => resp.sendFile('./public/index.html', { root: __dirname }));
app.get("/runningScans", (req, resp) => {
var filteredScans = getFilteredScans(req.query, runningScans, "runningScan");
resp.type = "json";
resp.status = 200;
resp.json(filteredScans);
});
app.get("/finishedScans", (req, resp) => {
var filteredScans = getFilteredScans(req.query, finishedScans, "finishedScan");
resp.type = "json";
resp.status = 200;
resp.json(filteredScans);
});
app.post("/deleteReport", (req, resp) => {
//So we need to have a valid object to search for
try {
if (JSON.stringify(Object.keys(req.body).sort()) != JSON.stringify(["scanname", "timedate", "parameters", "hostname"].sort())){
throw Error("incorrect scan descriptor variables");
}
for (var i =0; i < finishedScans.length; i++){
if (JSON.stringify(finishedScans[i]["scan descriptor"]) == JSON.stringify(Object(req.body))){
finishedScans.splice(i, 1);
}
}
writeFinishedScansToFile();
resp.json({"status": 200});
}
catch(err){
resp.status(400);
resp.json({"status": "error"});
return;
}
});
app.get("/networkScanner", (req, resp) => {
const nmapMethods = {"PageStart" : `<h3>Network Scanner</h3>
<p>There are numerous tools to help you with you're scan. Since this is a demo site, there isn't an extensive number of supported tools
provided out of the box. However, there are a few good tools included.</p>
<p><b>Unfortunately a new version of node-nmap has introduced instability to certain scan types such as -sV</b></p>
<div class="form-group ">
<label for="inputHost">Host (IP address / Hostname):</label>
<input type="text" class="form-control" id="inputHost" aria-describedby="emailHelp" placeholder="Enter Host">
<div class="valid-feedback">Looks good!</div>
<small id="emailHelp" class="form-text text-muted">Port scanning is not illegal in the UK. But make sure you abide by the rules or ToS of your network and local legislation</small>
</div><br>
`,
"FormMethods": {"Scan Technqiues" : {
"type" : "checkbox",
"required": "required",
"formItems" : ["<b>-sT</b> : TCP Connect Scan",
"<b>-sS</b> : TCP SYN Scan",
"<b>-sU</b> : UDP Connect Scan"]
},
"Port Range": {
"type" : "radio",
"required": "required",
"formItems": ["<b>-p1-1000</b> : Default Range (1-1000)",
"<b>-p-</b> : Full Range (1-65535)"]
},
"Additional Scan Technqiues": {
"type" :"checkbox",
"required": "optional",
"formItems" :["<b>-sV</b> : Service/Version Detection",
"<b>-sC</b> : Default Nmap Scripts"]
}
}
};
resp.type('json');
resp.status(200);
resp.json(nmapMethods);
});
app.post("/networkScanner", (req, resp) => {
try {
var hostname, params;
params = processParameters(req);
if ((params = validateParams(params, "nmapscan")) === false){
throw Error("Not a single valid param, so we are exiting scan");
}
hostname = req.body["host"];
resp.json({"status": 200});
}
catch(err){
resp.status(400);
resp.type('json');
resp.json({"status": "error"});
return;
}
executeNmapScan(hostname, params);
});
app.get("/dnsScanner", (req, resp) => {
const dnsMethods = {"PageStart" : `<h3>DNS Scanner</h3>
<p>Using OS resolver libraries we are able to query for many different DNS records. Unfortunately,
due to not knowing what environment our suite is running on, we chose not to implement unix tools
such as dig.</p>
<div class="form-group ">
<label for="inputHost">Host (IP address / Hostname):</label>
<input type="text" class="form-control" id="inputHost" aria-describedby="emailHelp" placeholder="Enter Host">
<small id="emailHelp" class="form-text text-muted">Please make sure to point correctly, i.e. if you mean to point to the parent domain
than do that instead of pointing to a subdomain such as "www"</div><br>`,
"FormMethods": {"Record Types" : {
"type" : "checkbox",
"required": "required",
"formItems" : ["<b>A</b> : IPv4 Address",
"<b>AAAA</b> : IPv6 Address",
"<b>CNAME</b> : Canonical Name Records",
"<b>MX</b> : Mail Exchange Records",
"<b>NS</b> : Name Server Records",
"<b>TXT</b> : Text Records",
"<b>SOA</b> : Start of Authority Records"
]
}
}
};
resp.type('json');
resp.status(200);
resp.json(dnsMethods);
});
app.post("/dnsScanner", (req, resp) => {
try {
var hostname, params;
params = processParameters(req);
if ((params = validateParams(params, "dnsscan")) == false){
throw Error("Not a single valid param, so we are exiting scan");
}
hostname = req.body["host"];
resp.status(200);
}
catch(err){
resp.status(400);
resp.type('json');
resp.json({"status": "error"});
return;
}
resp.send();
executeDnsScan(hostname, params);
});
function validateParams(params, scanname){
var definitions = { "dnsscan" : ["A", "AAAA", "CNAME", "MX", "SOA", "TXT", "NS"],
"nmapscan" : ["-sT", "-sU", "-sV", "-p-", "-p1-1000", "-sV", "-sC"]};
params = Object.values(params);
var newSet = new Set();
if (definitions[scanname] != undefined){
for(var i = 0; i< params.length; i++){
if (definitions[scanname].includes(params[i])){
newSet.add(params[i]);
}
}
}
params = Array.from(newSet);
if (params.length == 0 && scanname=="dnsscan"){
return false;
} else if (scanname == "nmapscan"){
return params;
}
return params;
}
function getFilteredScans(filterObj, arrRef, arrName){
var filteredScans = [];
var foundScan = true;
var filterKeys = Object.keys(filterObj);
var filterKey;
for (var i =0; i< arrRef.length; i++){
foundScan = true;
for (var j = 0; j < filterKeys.length; j++){
filterKey = filterKeys[j];
try {
if (arrName == "finishedScan"){
if (arrRef[i]["scan descriptor"][filterKey] != filterObj[filterKeys[j]]){
foundScan = false;
break;
}
} else if (arrName == "runningScan"){
if (arrRef[i][filterKey] != filterObj[filterKeys[j]]){
foundScan = false;
break;
}
}
} catch(err){
foundScan = false;
break;
}
}
if (foundScan == true){
filteredScans.push(arrRef[i]);
}
}
return filteredScans;
}
function processParameters(req){
var hostname = req.body["host"];
var scanType = req.body["scanType"];
if (hostname == undefined || scanType == undefined || !isValidHostname(hostname) || !isSupportedScanType(scanType)){
throw Error("incorrect_request");
}
var formKeys = Object.keys(req.body);
var params = [];
for(var i = 0; i < formKeys.length; i++){
var paramSwitch = formKeys[i];
if (paramSwitch != "host" && paramSwitch != "scanType"){
params.push(paramSwitch);
}
}
return hostname, params;
}
function isSupportedScanType(scanType){
var supportedScanTypes = ["dnsscan", "dnsScanner", "nmapscan", "networkScanner"];
if (supportedScanTypes.includes(scanType)){
return true;
}
return false;
}
//TODO: Make this function more extensive
function isValidHostname(hostname){
//In order to be a valid IP address or domain we need at least one period
if (hostname.includes(".")){
return true;
}
return false;
}
function executeNmapScan(hostname, params){
//TODO check whether nmap is in path
// nmap.nmapLocation = "nmap"; //default
var nmapscan = new nmap.NmapScan(hostname, params);
var reportObject = logScanAsRunning("nmapscan", hostname, params);
nmapscan.on('complete', data => {
transferResultsToFinished(reportObject, data[0]);
}).on('error', data => {
//TODO: Move errored out scans to some other place
transferResultsToFinished(reportObject, data[0]);
});
nmapscan.startScan();
return null;
}
// TODO: We need to update this with the neccessary code to change from arrayPosition to reportObject for transferResultsToFinished()
async function executeDnsScan(hostname, params){
var arrayPosition = logScanAsRunning("dnsscan", hostname, params);
var promises = [];
var promise, param;
var results = {};
for(var i = 0; i < params.length; i++) {
param = params[i];
promise = dns.promises.resolve(hostname, param).then((result) => {
if (result != undefined){
return result;
}
return "Scan was unable to complete successfully";
}).catch(error => {return `Scan was unable to complete successfully: ${error}`;});
promises.push(promise);
}
//We are executing multiple scans here, so we need to synchronize the async operations
for(var j = 0; j < promises.length; j++){
let result = await promises[j];
results[params[j]] = result;
}
transferResultsToFinished(arrayPosition, results);
return null;
}
//TODO: Move from array position to a map with hash IDs - avoids issues of race conditions
function transferResultsToFinished(reportObject, data){
var arrayPosition = runningScans.indexOf(reportObject);
var scanDescriptor = runningScans.splice(arrayPosition,1)[0];
var scanResult = {"scan descriptor": scanDescriptor,
"scan results": data };
finishedScans.push(scanResult);
writeScanResultsToFile(scanResult);
return null;
}
//TODO: We want to add datetime to the form
//Logs the scan as running and returns position in array
function logScanAsRunning(scanname, hostname, params){
var runningScanReport = {"scanname": scanname,
"timedate": new Date(),
"hostname" :hostname,
"parameters":[params]};
runningScans.push(runningScanReport);
return runningScanReport;
}
function getPersistentReports(){
const data = fs.readFileSync("./reports.json");
try {
var temp = JSON.parse(data);
} catch(err){
console.log(err);
temp = [];
}
return temp;
}
function writeScanResultsToFile(scanResults){
fs.readFile("reports.json", (err, data) =>{
if (err) {
console.log(err);
}
else {
//In case the JSON file is corrupted or malformed, we reset it
try{
var reports = JSON.parse(data);
} catch(err){
reports = [];
}
reports.push(scanResults);
finishedScans = reports;
}
fs.writeFile("reports.json", JSON.stringify(reports, null, 4), (err) =>{
if (err) {console.log(err);}});
});
}
function writeFinishedScansToFile(){
fs.writeFile("reports.json", JSON.stringify(finishedScans, null, 4), (err) =>{
if (err) {console.log(err);}});
}
if (!module.parent){
app.listen(4444, () => {
console.log(`GWI Toolkit listening at http://127.0.0.1:${4444}`);
});
}
module.exports = app;