-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathmiddler.js
266 lines (220 loc) · 6.99 KB
/
middler.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
//
// snarf - SMB man-in-the-middle tool
// Copyright (C) 2015 Josh Stone ([email protected])
// Victor Mata ([email protected])
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// ------------------------------------------------------------------------
var net = require("net");
var smb = require("./smb.js");
var out = require ("./out.js");
var moment = require("moment");
function Middler(id) {
var client;
var server;
var clientIP;
var clientPort;
var serverIP;
var reqPackets = [];
var respPackets = [];
var hackercount = 0;
var broker = undefined;
var myself = this;
var collectPackets = false;
var hostname = "unknown";
var freshness = moment();
var active = true;
var mature; // this variable tells when the client has released the client socket
this.attributes = new Object;
this.attributes.logon_failed = false;
this.expired = false;
// This may seem like a waste to define all of these accessors,
// but this is in anticipation of this functionality either
// getting more cmoplicated in the future or a potential need to
// subclass the Middler class for other protocols. As such, we
// don't want the idea that these items are simply variables to
// get too firmly coupled to the rest of the Snarf system.
this.setActive = function(state) {
active = state;
}
this.getActive = function() {
return active;
}
this.freshen = function() {
freshness = moment();
}
this.getFreshness = function() {
return freshness;
}
this.shutdown = function() {
out.red("Can't shutdown session that hasn't been setup yet");
}
this.getID = function() {
return id;
}
this.getRespPackets = function() {
return respPackets;
}
this.getReqPackets = function() {
return reqPackets;
}
this.getClient = function() {
return client;
}
this.getServer = function() {
return server;
}
this.getClientAddr = function() {
return clientIP;
}
this.getMature = function() {
return mature;
}
this.setMature = function(val) {
mature = val;
}
this.getServerAddr = function() {
return serverIP;
}
this.getClientPort = function() {
return clientPort;
}
this.setID = function(i) {
id = i;
}
this.setClientInfo = function(addr, port, dest) {
clientIP = addr;
clientPort = port;
serverIP = dest;
}
this.setBroker = function(b) {
broker = b;
}
this.setClient = function(s) {
client = s;
}
this.terminate = function() {
if(server) server.end();
}
this.expire = function() {
this.shutdown();
}
// The server component is actually a client socket connected to
// the distant server. I know, using a client socket to represent
// a server is awkward, but it's not a simple arrangement anyway.
// This code primarily shuffles responses from the server back to
// the appropriate client process.
this.setServer = function(val) {
var myself = this;
server = val;
server.on('data', function(x) {
var packet = broker.parsePacket(x);
if(myself.collectPackets) {
respPackets.push(packet);
}
broker.reviewServerPacket(packet, client, myself);
if(myself.attributes.logon_failed == true) {
myself.rewire(server, client);
}
});
server.on('end', function(data) {
out.red("Encountered 'end' event from server");
broker.deactivateMiddler(id);
});
server.on('error', function(data) {
server.pause();
out.red("Encountered error from server");
broker.deactivateMiddler(myself);
});
}
// We need to trap some authenticated session. This is the
// "originalClient". About the only thing that makes this
// different from a port redirector is that when the connection
// dies, we find a hacker tool to substitute.
this.setOriginalClient = function(val) {
var myself = this;
client = val;
this.shutdown = function() {
myself.expired = true;
out.red("Activating middler");
broker.activateMiddler(myself);
client.end();
if(client) {
out.red("Destroying client socket!");
client.destroy();
}
client = undefined;
server.resume();
}
client.on('data', function(x) {
var packet = broker.parsePacket(x);
if(myself.collectPackets) {
reqPackets.push(packet);
}
out.green("[" + myself.getID() + "] Client: " + packet.describe());
broker.reviewClientPacket(packet, server, myself);
});
client.on("error", function() {
server.pause();
myself.shutdown();
});
client.on('end', function() {
server.pause();
myself.shutdown();
});
}
this.rewire = function(server, client) {
out.red("Rewiring middler as dumb relay until it dies");
server.on('data', function(x) {
client.write(x);
});
client.on('data', function(x) {
server.write(x);
});
server.on('end', function() {
myself.terminate();
});
client.on('end', function() {
myself.terminate();
});
server.on('error', function() {
myself.terminate();
});
client.on('error', function() {
myself.terminate();
});
}
}
module.exports.NullMiddler = function(x) {
x.on('data', function(x) {
out.red("Shouldn't get data");
});
x.on('error', function() {
out.red("Shouldn't get errors");
});
x.on('end', function() {
out.red("Closing down socket\n");
});
}
module.exports.Relay = function(client, server) {
client.on('data', function(data) { server.write(data); });
server.on('data', function(data) { client.write(data); });
client.on('end', function(data) { });
server.on('end', function(data) { });
client.on('error', function(data) { });
server.on('error', function(data) { });
}
module.exports.Middler = Middler;