forked from offensive-security/masscan-web-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.php
executable file
·144 lines (141 loc) · 4.03 KB
/
import.php
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
<?php
/**
* This script is only runnable form command line
*/
if (substr(php_sapi_name(), 0, 3) != 'cli'):
die('This script can only be run from command line!');
endif;
/**
* Convert seconds to minutes, hours..
* NOTICE : Approximate, assumes months have 30 days.
*/
function seconds2human($ss)
{
$s = $ss%60;
$mins = floor(($ss%3600)/60);
$hours = floor(($ss%86400)/3600);
$days = floor(($ss%2592000)/86400);
$months = floor($ss/2592000);
$text = '';
if ($months > 0) {
$text .= $months." months,";
}
if ($days > 0) {
$text .= $days." days,";
}
if ($hours > 0) {
$text .= $hours." hours,";
}
if ($mins > 0) {
$text .= $mins." minutes,";
}
if ($s > 0) {
$text .= $s." seconds";
}
return $text;
}
/**
* Magic starts here
*/
$start_ts = time();
require dirname(__FILE__).'/config.php';
if (!isset($argv[1])):
die('Please provide a file name to import!'."\n");
endif;
$tmp = pathinfo($argv[1]);
if ($tmp['dirname'] == "."):
$filepath = dirname(__FILE__).'/'.$argv[1];
else:
$filepath = $argv[1];
endif;
if (!is_file($filepath)):
echo "File:".$filepath;
echo PHP_EOL;
echo 'File does not exist!';
echo PHP_EOL;
exit;
endif;
do {
echo PHP_EOL;
echo "Do you want to clear the database before importing (yes/no)?: ";
$handle = fopen ("php://stdin","r");
$input = fgets($handle);
} while (!in_array(trim($input), array('yes', 'no')));
if (trim(strtolower($input)) == 'yes'):
echo PHP_EOL;
echo "Clearing the db";
echo PHP_EOL;
$q = "TRUNCATE TABLE data";
DB::query($q);
endif;
echo "Reading file";
echo PHP_EOL;
$content = utf8_encode(file_get_contents($filepath));
echo "Parsing file";
echo PHP_EOL;
$xml = simplexml_load_string($content, 'SimpleXMLElement', LIBXML_COMPACT | LIBXML_PARSEHUGE);
if ($xml === false):
die('There is a problem with this xml file?!'.PHP_EOL);
endif;
$total = 0;
$inserted = 0;
echo "Processing data (This may take some time depending on file size)";
echo PHP_EOL;
foreach ($xml->host as $host):
foreach ($host->ports as $p):
$ip = sprintf('%u', ip2long($host->address['addr']));
$ts = (int) $host['endtime'];
$scanned_ts = date("Y-m-d H:i:s", $ts);
$port = (int) $p->port['portid'];
$protocol = (string) $p->port['protocol'];
if (isset($p->port->service)):
$service = (string) $p->port->service['name'];
if ($service == 'title'):
if (isset($p->port->service['banner'])):
$title = $p->port->service['banner'];
else:
$title = '';
endif;
$banner = '';
else:
if (isset($p->port->service['banner'])):
$banner = $p->port->service['banner'];
else:
$banner = '';
endif;
$title = '';
endif;
else:
$service = '';
$banner = '';
$title = '';
endif;
$state = (string) $p->port->state['state'];
$reason = (string) $p->port->state['reason'];
$reason_ttl = (string) $p->port->state['reason_ttl'];
$q = "INSERT INTO data SET id = null,
ip = ".$ip.",
port_id = ".(int) $port.",
scanned_ts = '".DB::escape($scanned_ts)."',
protocol = '".DB::escape($protocol)."',
state = '".DB::escape($state)."',
reason = '".DB::escape($reason)."',
reason_ttl = '".(int) $reason_ttl."',
service = '".DB::escape($service)."',
banner = '".DB::escape($banner)."',
title = '".DB::escape($title)."'";
$total++;
if (DB::execute($q)):
$inserted++;
endif;
endforeach;
endforeach;
$end_ts = time();
echo PHP_EOL;
echo "Summary:";
echo PHP_EOL;
echo "Total records:".$total."\n";
echo "Inserted records:".$inserted."\n";
$secs = $end_ts - $start_ts;
echo "Took about:".seconds2human($secs);
echo PHP_EOL;