-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpseudo.js
308 lines (248 loc) · 8.83 KB
/
pseudo.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
var nodemailer = require('nodemailer'),
fs = require('fs'),
MailParser = require("mailparser").MailParser,
mongoose = require('mongoose'),
async = require('async'),
crypto = require('crypto'),
exec = require('child_process').exec,
watch = require('watch'),
animal = require('animal-id');
animal.useSeparator('_');
var Pseudohash = require('./models/pseudohash').Pseudohash
mongoose.connect('mongodb://localhost/pseudo', function(err) {
if(err) throw err;
})
if (process.getuid() != 0) {
throw "You must run the mail parser as root!"
}
// create reusable transporter object using SMTP transport
var transporter = nodemailer.createTransport({
host: 'localhost',
port: 25,
tls: {rejectUnauthorized: false}
});
//begin execution with this call
watch_dir()
function watch_dir(){
watch.watchTree('/var/spool/mail/relay', function (f, curr, prev) {
var mailparser = new MailParser();
mailparser.on("end", function(mail_object){
mapAES(mail_object,f)
//mapPlainText(mail_object)
});
if (typeof f == "object" && prev === null && curr === null) {
// Finished walking the tree
} else if (prev === null) {
//new mail file created
fs.createReadStream(f).pipe(mailparser);
}
})
}
//currently only supports 1 recipient, although more is totally possible to implement
function mapAES(mail_object,mailFile){
var sender = mail_object.from[0].address
var subject = mail_object.subject
var query_string = subject.split("||")[subject.split("||").length-1]
var qs = parseQS(query_string)
if(subject.indexOf("||") != -1)
subject = subject.substring(0,subject.lastIndexOf("||"))
var p = mail_object.to[0].name //pseudonym they are sending to (if none, new message)
console.log("mail received")
var message = {}
message.to = []
message.subject = subject
message.body = mail_object.text
if(!p || p == ""){
//new conversation (not replying)
if(!qs.to || qs.to == "")
return //should we notify sender that their mail didn't go through?
var recipient = qs.to
var pseudohash = new Pseudohash({});
var pseudo1 = genPseudo(); //initiator
var pseudo2 = genPseudo(); //receiver
//encrypted with pseudo1
var map1 = {
email:sender, //initiator
pseudonym: pseudo2 //receiver
}
//encrypted with pseudo2
var map2 = {
email:recipient, //receiver
pseudonym: pseudo1 //initiator
}
pseudohash.hash = SHA256(pseudo1)
pseudohash.hash2 = SHA256(pseudo2)
pseudohash.map_cipher = AESEncrypt(JSON.stringify(map1),pseudo1)
pseudohash.map_cipher2 = AESEncrypt(JSON.stringify(map2),pseudo2)
pseudohash.save(function(err,savedP){
if(err)
throw err
message.to.push(recipient)
message.from = pseudo1
send(message,mailFile)
})
}
else{
var hash = SHA256(p)
Pseudohash.findOne({$or: [{hash:hash},{hash2:hash}]}).exec(function(err,pseudohash){
if(err)
throw err
if(!pseudohash)
return //should we notify sender their email didn't go through?
var mapCipher = pseudohash.map_cipher2
var key = p
if(hash == pseudohash.hash)
mapCipher = pseudohash.map_cipher
var map
try{
map = JSON.parse(AESDecrypt(mapCipher,key))
}
catch(err){
return //invalid key
}
if(qs.destroy == 'true')
return destroy(pseudohash,sender,map.email,p,map.pseudonym)
message.to.push(map.email)
message.from = map.pseudonym
send(message,mailFile)
})
}
}
function shred(mailFile){
async.parallel({
mailbox : function(callback){
exec("shred -fuzn 5 " + mailFile, function(err, stdout, stderr){
if(err)
return callback(err)
if(stderr)
return callback(stderr)
console.log("mailbox cleaned")
callback(null)
})
},
maillogs : function(callback){
exec('find /var/log -type f -name "mail*" -exec shred -fuzn 5 {} \\;', function(err, stdout, stderr){
if(err)
return callback(err)
if(stderr)
return callback(stderr)
console.log("mail logs cleaned")
callback(null)
})
},
mongocache : function(callback){
exec('find /var/log/mongodb -type f -exec shred -zn 3 {} \\;', function(err, stdout, stderr){
if(err)
return callback(err)
if(stderr)
return callback(stderr)
console.log("mongo caches cleared")
callback(null)
})
},
bleachbit : function(callback){
async.parallel([
function(cb){
exec('bleachbit --clean system.memory', function(err, stdout, stderr){
if(err)
return cb(err)
cb(null)
})
},
function(cb){
exec('bleachbit --clean system.cache', function(err, stdout, stderr){
if(err)
return cb(err)
cb(null)
})
}
],function(err){
console.log("bleachbit garbage collection finished")
callback(err)
})
}
},function(err,results){
if(err)
console.log(err)
console.log("System securely cleaned")
})
}
//this parser is horrible right now. I guess I should fix that later?
function parseQS(query_string){
var qs = {}
var parts = query_string.split(";")
parts.forEach(function(p){
var keyval = p.split(":")
//var val
//if(keyval[1].indexOf(",") != -1){
// val = keyval[1].split(",")
//}
//else
// val = keyval[1]
qs[keyval[0]] = keyval[1]
})
return qs
}
function SHA256(message){
return crypto.createHash('sha256').update(message).digest('base64');
}
function AESEncrypt(message,key){
var cipher = crypto.createCipher("aes-256-ctr",key)
var crypted = cipher.update(message,'utf8','hex')
crypted += cipher.final('hex')
return crypted;
}
function AESDecrypt(cipher,key){
var decipher = crypto.createDecipher("aes-256-ctr",key)
var dec = decipher.update(cipher,'hex','utf8')
dec += decipher.final('utf8');
return dec;
}
function genPseudo(){
var animalName = animal.getId()
animalName = animalName + "_" + uuidGen()
return animalName
}
//destroy pseudnym lookup
function destroy(mapping,email1,email2,pseudonym1,pseudonym2){
mapping.remove(function(err){
send({from:"Ronald Rivest",to:[email1,email2],subject:"Link between " + pseudonym1 + " and " + pseudonym2 + " destroyed." })
})
}
// send mail with defined transport object
function send(message,mailFile){
var recipients = message.to
recipients.forEach(function(r){
var mailOptions = {
from: message.from + " <[email protected]>", // sender address
replyTo: message.from + ' <[email protected]>',
to: '', // list of receivers,
subject: message.subject, // Subject line
text: message.body, // plaintext body
html: "",//body,
xMailer: '6857 Mailer'
};
mailOptions.bcc = r
transporter.sendMail(mailOptions, function(error, info){
if(error){
console.log(error);
}
else{
console.log("message sent")
}
if(mailFile)
shred(mailFile)
});
})
}
function uuidGen(){ //uuid logic based off of http://stackoverflow.com/a/2117523/1459449
var uuid = 'xxxxyxxxyxxx'.replace(
/[xy]/g,
function (c) {
var r = Math.random() * 16 | 0, //(0 to 15)
v = c === 'x' ? r : (r & 0x3 | 0x8) //v = 0 to F (hex)
return v.toString(16)
}
);
return uuid
}