-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathexploitdb.class.php
More file actions
522 lines (351 loc) · 13.1 KB
/
exploitdb.class.php
File metadata and controls
522 lines (351 loc) · 13.1 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
<?php
/**
* @class: exploitdb-db
* @author: Adilson Santos da Rocha
* @copyright: Nstalker - Redesegura 2016
* @licence: GPLv3
* @date {{$date}}
* */
/**
*
* Do everything you need :)
*/
class exploitdb {
private $exploitdb_dir; //directory where exploits are stored.
protected $db; // PDO dbatabase connection object
protected $header; // Curl Header used only to debug
protected $file; // SPLFileInfo object with actual expĺoit file in proccess
protected $log_file; // file to logging all events
protected $preserv_log = 0; // If 1: store logging messages on $this->log_file
protected $fh_log; // Resource: The logfile handled by (fopen)
/**
* Construct the class and doame Confs.
*/
function __construct() {
$this->exploitdb_dir = "./exploits/platforms/";
$this->dbconnect('sqlite'); //conect to database
$this->log_file = "exploitdb.log";
// creating a File Handler to log-file
if($this->preserv_log == 1)
{
$this->fh_log = fopen($this->log_file, 'a+');
}
}
/** destruction garbage collection */
function destruct()
{
unset($this->db);
unset($this->file);
if($this->preserv_log)
{
fclose($this->log_file);
}
}
/**
* Scan the $this->exploitdb_dir files and populate de database
* use it to import from exploit-db website to database
* Note that I Prefer to scan files than read files.cvs
*/
function populate_db() {
try {
$inserted = 0;
$count = 0;
$it = new RecursiveDirectoryIterator($this->exploitdb_dir);
foreach (new RecursiveIteratorIterator($it) as $file) {
if($count ==0 ) // starting update Logging attemp
{
$this->log("Starting Syncing Exploit database, Please be Patient ! it could be slow and take long hours");
}
if(($count % 100) == 0)
{
$this->log("Dont Worry! its working very well ! its is $count files Processed and Inserted Exploits: $inserted:");
}
$x = explode('/', $file);
$y = explode('.', $x[(sizeof($x) - 1)]);
if (is_numeric($y[0])) {
if ($this->if_exists($y[0])) {
$count++;
continue;
} else {
$this->file = $file;
$exploit = $this->getExploitInfo($y[0]);
$this->insert_exploit($exploit);
$this->log("iteraction: $count Exploit id: $y[0] :Inserted ");
$inserted ++;
}
}
$count++;
}//endo of dir interctor
$this->log("update Finished: Processeds Files: $count - Inserted Exploit : $inserted - Total Exploits " . $this->CountExploit());
} catch (Exception $e) {
$this->log("General Error " . $e->getMessage() . "\n \t\t\t Debug: " .json_encode($e), "Critical:");
die("ERROR: " . $e->getMessage() . \n);
}
}
//end of function
/**
* Get information from a especic Exploit from Exploitdb website
* @param Int $id Id of Exploti to get
* @return Object a StdClass $obj
* * $obj->ID : Id of Exploit on exploit0-db.com
* $obj->title : The title of Exploit
* $obj->url : URL to Exploit om exploit-db.com
* $obj->CVE : The VCE data
* $obj->OSVDB : OSVDB-ID of exploit
* $obj->author : Exploit Author
* $obj->published : exploit published date
* $ojj->file : path to exploit
*/
public function getExploitInfo($id) {
try {
$url = "https://www.exploit-db.com/exploits/$id/";
$url = trim(rtrim($url));
$doc = new DOMDocument();
$resp = $this->request($url);
$doc->loadHTML($resp);
$tds = $doc->getElementsByTagName('td');
$title = $doc->getElementsByTagName('h1');
$obj = new stdClass();
$obj->ID = $id;
$obj->title = trim(rtrim($title[0]->textContent));
$obj->url = $url;
$obj->file = $this->file->getPathname();
foreach ($tds as $td) {
$d = explode(":", $td->textContent);
if ($d[0] == "CVE") {
$obj->CVE = trim(rtrim($d[1]));
}
if ($d[0] == "OSVDB-ID") {
$obj->OSVDB = trim(rtrim($d[1]));
}
if ($d[0] == "Published") {
$obj->published = trim(rtrim($d[1]));
}
if ($d[0] == "Author") {
$obj->author = trim(rtrim($d[1]));
}
}//endo foreach
return $obj;
} catch (Exception $e) {
$this->log($e->getMessage(), "Error:");
}
}
//end of funcion
/**
* Verify if a exploit exists on database
* @param int $id : exploitdb id to check
* @return True if exists this id on database
*/
private function if_exists($id) {
try {
$sth = $this->db->prepare("SELECT ID FROM exploitdb WHERE id = :id");
$sth->bindParam(':id', $id, PDO::PARAM_INT);
$sth->execute();
$result = $sth->fetchObject();
if (is_object($result)) {
return true;
} else {
return false;
}
} catch (PDOException $e) {
$this->log("Error checking if a exploit exists: " . $e->getMessage() );
throw new Exception("Error checking if a exploit exists: " . $e->getMessage(), 1);
}
}
/**
* Insert the exploit on database
* The $obj is returned by getExploitInfo()
* @param StdClass $obj a Object contain
* $obj->ID : Id of Exploit on exploit0-db.com
* $obj->title : The title of Exploit
* $obj->url : URL to Exploit om exploit-db.com
* $obj->CVE : The VCE data
* $obj->OSVDB : OSVDB-ID of exploit
* $obj->author : Exploit Author
* $obj->published : exploit published date
* $ojj->file : path to exploit
* @throws A Exception if fail
*/
private function insert_exploit($obj) {
try {
$sql = "INSERT INTO exploitdb(
id,
title,
url,
CVE,
OSVDB,
author,
published,
file) VALUES (
:ID,
:title,
:url,
:CVE,
:OSVDB,
:author,
:published,
:file)";
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':ID', $obj->ID, PDO::PARAM_INT);
$stmt->bindParam(':title', $obj->title, PDO::PARAM_STR);
$stmt->bindParam(':url', $obj->url, PDO::PARAM_STR);
$stmt->bindParam(':CVE', $obj->CVE, PDO::PARAM_STR);
$stmt->bindParam(':OSVDB', $obj->OSVDB, PDO::PARAM_STR);
$stmt->bindParam(':author', $obj->author, PDO::PARAM_STR);
$stmt->bindParam(':published', $obj->published, PDO::PARAM_STR);
$stmt->bindParam(':file', $obj->file, PDO::PARAM_STR);
$stmt->execute();
} catch (PDOException $e) {
$this->log("Trying to insert ". json_encode($obj) ." on DB " . $e->getMessage(), "Error:" );
} catch (Exception $e) {
$this->log("General Error " . $e->getMessage() . "\n \t\t\t Debug: " .json_encode($e), "Critical:");
}
}
/**
* Auxiliary funciton to do a HTTP GET Request to a URL
* @param String $url URL to GeT
* @return HTML string
*/
private function request($url) {
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => "$url",
CURLOPT_VERBOSE => 0,
CURLOPT_HEADER => 1,
CURLOPT_ENCODING => 1,
CURLOPT_USERAGENT => 'Nstalker'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$this->header = substr($response, 0, $header_size);
curl_close($curl);
return $resp;
}
/**
* Make a connection to a database, and set it ti $this->db
* @param String $dbtype String to select your prefered database
*/
private function dbconnect($dbtype) {
try {
if ($dbtype == 'sqlite') {
$this->db = new PDO('sqlite:exploitdb.db');
//$db = new PDO('sqlite:exploit-db.sdb');
// Set errormode to exceptions
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
} catch (PDOException $e) {
$this->log('Error connecting database: '. $e->getMessage());
die($e->getMessage() . "\n");
}
}
/**
* @session Search
*functon to search o database
* */
/**
* Search a Exploit by a CVE
* @param STRING $CVE CVE withot (CVE-) prefix only numbers like 2016-3714
* @return JSON
*/
public function byCVE($CVE)
{
try {
(string) ($CVE);
// $CVE = $this->treatCVE($CVE);
$sth = $this->db->prepare("SELECT * FROM exploitdb WHERE CVE = :CVE");
$sth->bindParam(':CVE', $CVE, PDO::PARAM_STR);
$sth->execute();
$result = $sth->fetchObject();
if (is_object($result)) {
return json_encode($result);
} else {
return ('{"Code":1, "text": "Not Found"}');
}
} catch (PDOException $e) {
$this->log("Sear" . $e->getMessage() . "\n");
}
}
/**
* Search a Exploit by a OSVDB
* @param STRING $OSVDB OSVDB without (OSVDB-ID) prefix only numbers like 29421
* @return JSON
*/
public function byOSVDB($OSVDB)
{
try {
(string) (trim(rtrim($OSVDB)));
$sth = $this->db->prepare("SELECT * FROM exploitdb WHERE OSVDB = :OSVDB");
$sth->bindParam(':OSVDB', $OSVDB, PDO::PARAM_STR);
$sth->execute();
$result = $sth->fetchObject();
if (is_object($result)) {
return json_encode($result);
} else {
return ('{"Code":1, "text": "Not Found"}');
}
} catch (PDOException $e) {
die("erro busca:" . $e->getMessage() . "\n");
}
}
/**
* Search a Exploit by Title
* @param STRING $pattern a pattern to search on title of exploit
* @return JSON
*/
public function byTitle($pattern)
{
try {
(string) (trim(rtrim($pattern)));
$sth = $this->db->prepare("SELECT * FROM exploitdb WHERE title LIKE '%$pattern%'");
$sth->execute();
$result = $sth->fetchAll();
return json_encode($result) ;
} catch (PDOException $e) {
die("erro busca:" . $e->getMessage() . "\n");
}
}
//*** END of Search Session ***/
/**
* Simle funtion to count how many exploits are on DB
* @return INT
*/
public function CountExploit()
{
try {
$this->statistics = new stdClass();
$sth = $this->db->prepare("SELECT count(id) as count FROM exploitdb");
$sth->execute();
$result = $sth->fetchObject();
if (is_object($result)) {
return $result->count;
} else {
return false;
}
} catch (PDOException $e) {
$this->log("$e->getMessage()", "Error");
die("Error: " . $e->getMessage() . "\n");
}
}
/**
* This is a simple function to log activities
* @todo integrate it with unix syslog
*/
public function log($message, $type = "info ") {
$content = date("Y-m-d H:i:s") . " - " . $type . ": - " . $message . "\n";
if($this->preserv_log == 1)
{
try {
fwrite($this->fh_log, $content);
} catch (Exception $ex) {
echo "Error: ". $ex->getMessage()."\n";
}
}
else{
echo $content;
}
}
}//end of class