-
Notifications
You must be signed in to change notification settings - Fork 0
/
kismetsim.php
executable file
·241 lines (205 loc) · 4.67 KB
/
kismetsim.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
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
#!/usr/bin/php -q
<?php
/**
* Listens for requests and forks on each connection
*/
include 'settings.php';
$__server_listening = true;
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
declare(ticks = 1);
become_daemon();
/* nobody/nogroup, change to your host's uid/gid of the non-priv user */
change_identity(65534, 65534);
/* handle signals */
pcntl_signal(SIGTERM, 'sig_handler');
pcntl_signal(SIGINT, 'sig_handler');
pcntl_signal(SIGCHLD, 'sig_handler');
/* change this to your own host / port */
server_loop($server, $port);
/**
* Change the identity to a non-priv user
*/
function change_identity($uid, $gid)
{
if( !posix_setgid( $gid ) )
{
print "Unable to setgid to " . $gid . "!\n";
exit;
}
if( !posix_setuid( $uid ) )
{
print "Unable to setuid to " . $uid . "!\n";
exit;
}
}
/**
* Creates a server socket and listens for incoming client connections
* @param string $address The address to listen on
* @param int $port The port to listen on
*/
function server_loop($address, $port)
{
GLOBAL $__server_listening;
if(($sock = socket_create(AF_INET, SOCK_STREAM, 0)) < 0)
{
echo "failed to create socket: ".socket_strerror($sock)."\n";
exit();
}
if(($ret = socket_bind($sock, $address, $port)) < 0)
{
echo "failed to bind socket: ".socket_strerror($ret)."\n";
exit();
}
if( ( $ret = socket_listen( $sock, 0 ) ) < 0 )
{
echo "failed to listen to socket: ".socket_strerror($ret)."\n";
exit();
}
socket_set_nonblock($sock);
echo "waiting for clients to connect\n";
while ($__server_listening)
{
$connection = @socket_accept($sock);
if ($connection === false)
{
usleep(100);
}elseif ($connection > 0)
{
handle_client($sock, $connection);
}else
{
echo "error: ".socket_strerror($connection);
die;
}
}
}
/**
* Signal handler
*/
function sig_handler($sig)
{
switch($sig)
{
case SIGTERM:
case SIGINT:
exit();
break;
case SIGCHLD:
pcntl_waitpid(-1, $status);
break;
}
}
/**
* Handle a new client connection
*/
function handle_client($ssock, $csock)
{
GLOBAL $__server_listening;
$pid = pcntl_fork();
if ($pid == -1)
{
/* fork failed */
echo "fork failure!\n";
die;
}elseif ($pid == 0)
{
/* child process */
$__server_listening = false;
socket_close($ssock);
interact($csock);
socket_close($csock);
}else
{
socket_close($csock);
}
}
function interact($socket)
{
showWelcome($socket);
$command = '';
$isRunning = true;
$networks = 0;
$starttime = time();
$endtime = $starttime + 1200;
$currtime = time();
$nettime = time();
$errorcode = '';
while(time() < $endtime && $errorcode == '' && $isRunning)
{
/* TALK TO YOUR CLIENT */
if(time() > $currtime + 5)
{
@socket_write($socket, '*INFO: ' . $networks . "\n");
$currtime = time();
}
// if(time() > $nettime + 3)
// {
outputNetworkInfo($socket, $networks);
$networks++;
$nettime = time();
// }
$errorcode = socket_last_error($socket);
}
}
/**
* Become a daemon by forking and closing the parent
*/
function become_daemon()
{
$pid = pcntl_fork();
if ($pid == -1)
{
/* fork failed */
echo "fork failure!\n";
exit();
}elseif ($pid)
{
/* close the parent */
exit();
}else
{
/* child becomes our daemon */
posix_setsid();
chdir('/');
umask(0);
return posix_getpid();
}
}
/**
* Functions to support kismetsim processing
*/
function process_command($socket, $command)
{
switch($command)
{
case 'quit':
return false;
break;
}
return true;
}
function showWelcome($socket)
{
@socket_write($socket, '*KISMET: 0.0.0 1311505204 Kismet 20050815211952 1 2007.01.R1
*PROTOCOLS: KISMET,ERROR,ACK,PROTOCOLS,CAPABILITY,TERMINATE,TIME,ALERT,NETWORK,CLIENT,GPS,INFO,REMOVE,STATUS,PACKET,STRING,WEPKEY,CARD' . "\n");
}
function outputNetworkInfo($socket, $networks)
{
$network = '*NETWORK: ' . generateMACAddress() . ' 0.0.0.0 0 2 11.0 0 979.770020 -81.571976 41.404366 979.770020 0.000000 41.404499 -81.571899 1018.049988 0.000000 WAP Demo ' . $networks . "\n";
echo $network . "\n";
@socket_write($socket, $network);
}
function generateMACAddress()
{
$mac = '';
for($a = 0; $a < 6; $a++)
{
if($a > 0)
$mac .= ':';
$mac .= sprintf('%02d', rand(0, 255));
}
return $mac;
}
?>