This repository has been archived by the owner on Jul 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
333 lines (316 loc) · 10.2 KB
/
app.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
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
// app.js
// call the packages we need
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser'); // import bodyParser to parse the body of API requests
var multer = require('multer'); // File upload support
var checksum = require('checksum'); // Does Checksum things
var fs = require('fs'); // Filesystem Object thing
var uploadDir = __dirname + "/uploads/";
var outputDir = __dirname + "/output/";
var resultDir = __dirname + "/result/";
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(multer({ dest: uploadDir})); // Set upload directory to /uploads
var port = process.env.PORT || 8080; // set our port
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router(); // get an instance of the express Router
router.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next(); // make sure we go to the next routes and don't stop here
});
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
var result = { "message": 'Steganography API!' };
res.json(result);
});
// req.params.extraThing to access :extraThing
// req.body gives key=>value pairs in JSON
router.route('/embed')
.get(function(req,res){
var result = {};
result.error = true;
result.message = "Invalid Request. Only POST is exposed for this command.";
result.arguments = "cover, embed, [password], [filetype]. [optional]";
res.status(400).json(result);
})
.post(function(req, res) {
// Has parameters cover, embed, [password], and [filetype]
// returns ID for embedded file
console.log("Time to embed!");
var coverF = req.files.cover, embedF = req.files.embed;
var hasCalled = false;
embed(coverF, embedF, req.body.password, req.body.filetype, function(result){
if(hasCalled === false){
if(typeof result.error !== 'undefined'){
// Throw an error if there's an error
res.status(500).json(result);
}
else{
res.json(result);
fs.unlink(uploadDir + coverF.name, function(err){
if (err) {
errLog(err);
}
});
fs.unlink(uploadDir + embedF.name, function(err){
if (err) {
errLog(err);
}
});
}
hasCalled = true;
}
});
})
.put(function(req,res){
var result = {};
result.error = true;
result.message = "Invalid Request. Only POST is exposed for this command.";
result.arguments = "cover, embed, [password], [filetype]. [optional]";
res.status(400).json(result);
})
.delete(function(req,res){
var result = {};
result.error = true;
result.message = "Invalid Request. Only POST is exposed for this command.";
result.arguments = "cover, embed, [password], [filetype]. [optional]";
res.status(400).json(result);
});
router.route('/info/')
.post(function(req,res){
var result = {};
result.message = "";
var coverF = req.files.cover;
var password;
if(typeof req.password == 'undefined'){
password = "";
}
var command = ["info"];
command.push(coverF.path,"-p",password);
var spawn = require('child_process').spawn;
var child = spawn('steghide', command);
child.stdout.on('data', function (data){
console.log(data.toString());
if(data.toString().indexOf("capacity") > -1){
result.message = data.toString();
res.json(result);
} else {
// Do Nothing
}
});
});
router.route('/retrieve/:id')
.get(function(req,res){
var id = req.params.id;
console.log("Retrieving: ", id);
res.sendFile(id, {root:outputDir}, function (err) {
if (err) {
errLog(err);
console.log(err);
res.status(err.status).end();
}
else {
console.log('Sent:', id);
}
});
})
.post(function(req,res){
var result = {};
result.error = true;
result.message = "Invalid Request. Only GET and DELETE are exposed for this command.";
result.arguments = "/retrieve/:id";
res.status(400).json(result);
})
.put(function(req,res){
var result = {};
result.error = true;
result.message = "Invalid Request. Only GET and DELETE are exposed for this command.";
result.arguments = "/retrieve/:id";
res.status(400).json(result);
})
.delete(function(req,res){
var id = req.params.id;
var file = outputDir + id;
fs.unlink(file, function (err) {
if (err) {
errLog(err);
res.status(err.status).end();
}
else {
res.json({"message":"File Deleted"});
console.log('Deleted:',id);
}
});
});
router.route('/extract')
.get(function(req,res){
var result = {};
result.error = true;
result.message = "Invalid Request. Only POST is exposed for this command.";
result.arguments = "embed, [password], [filetype]. [optional]";
res.status(400).json(result);
})
.post(function(req,res){
// has parameters {embed}, [password], [filetype]
// returns embedded file
console.log("time to extract");
var embed = req.files.embed;
var hasCalled = false;
extract(embed, req.body.password, req.body.filetype, function(result){
if(hasCalled === false){
if(typeof result.error !== 'undefined'){
res.status(500).json(result);
}
else{
// res.json(result);
res.sendFile(result.fileName, {root:resultDir}, function (err) {
if (err) {
errLog("kek" + err);
res.status(err.status).end();
}
else {
console.log('Sent:', result.fileName);
fs.unlink(uploadDir + embed.name, function(err){
if (err) {
errLog("unlinkerror " + err);
}
});
fs.unlink(resultDir + result.fileName,function(err){
if (err) {
errLog("unlinkerror " + err);
}
});
}
});
}
hasCalled = true;
}
});
})
.put(function(req,res){
var result = {};
result.error = true;
result.message = "Invalid Request. Only POST is exposed for this command.";
result.arguments = "embed, [password], [filetype]. [optional]";
res.status(400).json(result);
})
.delete(function(req,res){
var result = {};
result.error = true;
result.message = "Invalid Request. Only POST is exposed for this command.";
result.arguments = "embed, [password], [filetype]. [optional]";
res.status(400).json(result);
});
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /steganography
app.use('/steganography', router);
// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Magic happens on port ' + port);
// Embed/Extract functions ----------------------------
// =============================================================================
function embed(cover, embedFile, password, filetype, callback){
var result = {};
if(typeof password == 'undefined'){
password = "";
}
filetype = filetype || cover.extension;
if(/^jpe?g|au|bmp|wav$/i.test(filetype)){
// Use Steghide
var id = cover.name.split(".")[0] + ".";
var output = outputDir + id + filetype;
var command = ["embed", "-cf"];
command.push(cover.path, "-ef", embedFile.path, "-sf", output, "-p", password, "-e", "blowfish");
var spawn = require('child_process').spawn;
var child = spawn('steghide', command);
child.stderr.on('data', function (data) {
console.log('stderr: ' + data);
if(data.toString().indexOf("done") > -1){
checksum.file(output, function (err, sum) {
if(err){
errLog(err);
result.error = true;
result.message = err;
callback(result);
}
result.id = id+filetype;
result.checksum_sha1 = sum;
callback(result);
});
} else if (data.toString().indexOf("could not open file") > -1){
result.error = true;
result.message = "Could Not open File";
callback(result);
} else if (data.toString().indexOf("the cover file is too short to embed the data") > -1){
result.error = true;
result.message = "Cover file must be larger than Embed file";
callback(result);
} else if (data.toString().indexOf("steghide:") > -1){
// Do nothing
} else if (data.toString().indexOf("writing stego file") > -1){
// Do Nothing
} else {
result.error = true;
result.message = data.toString();
callback(result);
}
});
} else if(/^png$/i.test(filetype)){
result.error = true;
result.message = "PNG Images Not Supported (yet)";
callback(result);
} else {
result.error = true;
result.message = "Invalid Filetype";
callback(result);
}
}
function extract(cover, password, filetype, callback){
var result = {};
if(typeof password == 'undefined'){
password = "";
}
filetype = filetype || cover.extension;
if(/^jpe?g|au|bmp|wav$/i.test(filetype)){
// Use Steghide
var args = ['extract', '-f', '-sf'];
args.push(cover.path,'-p', password);
var spawn = require('child_process').spawn;
var child = spawn('steghide', args, {cwd: resultDir});
child.stderr.on('data', function (data) {
console.log('stderr: ' + data);
if(data.toString().indexOf("wrote extracted data to ") > -1){
result.fileName = data.toString().substring(data.toString().indexOf('"')+1,data.toString().lastIndexOf('"'));
//var move = spawn('mv',[result.fileName,resultDir],);
callback(result);
} else if (data.toString().indexOf("could not extract any data with that passphrase") > -1){
result.error = true;
result.message = "Invalid Password";
callback(result);
} else if (data.toString().indexOf("steghide:") > -1){
// Do nothing
} else {
result.error = true;
result.message = data.toString();
callback(result);
}
});
} else if(/^png$/i.test(filetype)){
result.error = true;
result.message = "Lossless Images Not Supported (yet)";
callback(result);
} else {
result.error = true;
result.message = "Invalid Filetype";
callback(result);
}
}
function errLog(item){
console.log("Error: " + item);
}