Skip to content

Commit fb96fab

Browse files
committed
Added first version of the API
1 parent 62fa48b commit fb96fab

File tree

5 files changed

+173
-0
lines changed

5 files changed

+173
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/*
2+
exploitdb/*

create.sql

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CREATE DATABASE IF NOT EXISTS exploitdb;
2+
3+
# id,file,description,date,author,platform,type,port
4+
CREATE TABLE IF NOT EXISTS exploitdb.exploits (
5+
id INT NOT NULL AUTO_INCREMENT,
6+
file VARCHAR(255) NOT NULL,
7+
description VARCHAR(255) NOT NULL,
8+
_date DATE NOT NULL,
9+
author VARCHAR(255) NOT NULL,
10+
platform VARCHAR(255) NOT NULL,
11+
type VARCHAR(255) NOT NULL,
12+
port INT NOT NULL,
13+
PRIMARY KEY (id)
14+
);

model.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var mysql = require('mysql');
2+
var connection = mysql.createConnection({
3+
host : 'localhost',
4+
user : 'root', // change line to fit your requirements
5+
password : '', // change line to fit your requirements
6+
database : 'exploitdb'
7+
});
8+
9+
connection.connect(function(err) {
10+
if (err) {
11+
console.error('error connecting: ' + err.stack);
12+
return;
13+
}
14+
console.log('connected as id ' + connection.threadId);
15+
});
16+
17+
exports.getExploits = function(id, options, callback) {
18+
var sql = 'SELECT * from exploits';
19+
var tmp = '';
20+
for (var key in options) {
21+
if (key == 'before' || key == 'after') {
22+
tmp += ' AND _date';
23+
if (key == 'before') {
24+
tmp += ' <= ';
25+
} else {
26+
tmp += ' >= ';
27+
}
28+
tmp += connection.escape(options[key]);
29+
} else {
30+
tmp += " AND " + key + " LIKE " + connection.escape('%' +options[key] + '%');
31+
}
32+
}
33+
if (id != undefined) {
34+
sql += ' WHERE id = ' + connection.escape(id) + tmp;
35+
} else {
36+
// replace the first occurence of 'AND' with a 'WHERE'
37+
sql += tmp.replace('AND', 'WHERE');
38+
}
39+
40+
connection.query(sql, function(err, results) {
41+
if (err) throw err;
42+
callback(results);
43+
});
44+
}

package.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "exploitdb-json-api",
3+
"version": "0.0.1",
4+
"description": "ExploitDB JSON API",
5+
"main": "server.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"start": "node server.js"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git://github.com/PaulSec/exploitdb-json-api.git"
13+
},
14+
"author": "PaulSec",
15+
"license": "MIT",
16+
"bugs": {
17+
"url": "https://github.com/PaulSec/exploitdb-json-api/issues"
18+
},
19+
"homepage": "https://github.com/PaulSec/exploitdb-json-api",
20+
"dependencies": {
21+
"express": "^4.10.7",
22+
"mysql": "^2.5.4",
23+
"mime": "^1.2.11"
24+
}
25+
}

server.js

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
var express = require('express'); // call express
2+
var app = express(); // define our app using express
3+
var fs = require('fs');
4+
var model = require('./model.js');
5+
var path = require('path');
6+
var mime = require('mime');
7+
8+
var port = process.env.PORT || 8080; // set our port
9+
10+
var router = express.Router(); // get an instance of the express Router
11+
12+
// router.get('/', function(req, res) {
13+
// res.send("test");
14+
// });
15+
16+
function parseParameters(req, callback) {
17+
options = {}
18+
if (req.query.author != undefined) {
19+
options['author'] = req.query.author;
20+
}
21+
if (req.query.platform != undefined) {
22+
options['platform'] = req.query.platform;
23+
}
24+
if (req.query.port != undefined) {
25+
options['port'] = req.query.port;
26+
}
27+
if (req.query.description != undefined) {
28+
options['description'] = req.query.description;
29+
}
30+
if (req.query.type != undefined) {
31+
options['type'] = req.query.type;
32+
}
33+
if (req.query.before != undefined) {
34+
options['before'] = req.query.before;
35+
}
36+
if (req.query.after != undefined) {
37+
options['after'] = req.query.after;
38+
}
39+
callback(options);
40+
}
41+
42+
// retrieves all exploit
43+
router.get('/exploits', function (req, res) {
44+
res.setHeader('Content-Type', 'application/json');
45+
46+
parseParameters(req, function (options) {
47+
model.getExploits(undefined, options, function (results){
48+
res.status((results.length > 0) ? 200 : 404).end(JSON.stringify(results));
49+
});
50+
});
51+
});
52+
53+
// retrieves a specific exploit
54+
router.get('/exploits/:id', function (req, res) {
55+
res.setHeader('Content-Type', 'application/json');
56+
model.getExploits(req.params.id, undefined, function (results){
57+
res.status((results.length > 0) ? 200 : 404).end(JSON.stringify(results));
58+
});
59+
});
60+
61+
// retrieves the attachment of an exploit
62+
router.get('/exploits/:id/attachment', function (req, res) {
63+
model.getExploits(req.params.id, undefined, function (results){
64+
if (results[0] && results[0]['file']) {
65+
var file = __dirname + '/exploitdb/' + results[0]['file'];
66+
67+
var filename = path.basename(file);
68+
var mimetype = mime.lookup(file);
69+
70+
res.setHeader('Content-disposition', 'attachment; filename=' + filename);
71+
res.setHeader('Content-type', mimetype);
72+
73+
var filestream = fs.createReadStream(file);
74+
filestream.pipe(res);
75+
} else {
76+
res.status(404).end();
77+
}
78+
});
79+
});
80+
81+
// manages 404
82+
router.get('*', function(req, res){
83+
res.redirect('/exploits');
84+
});
85+
86+
app.use('/', router);
87+
app.listen(port);
88+
console.log("Open browser at: http://127.0.0.1:" + port);

0 commit comments

Comments
 (0)