1 |
/* |
2 |
* Copyright (C) 2002 Erik Fears |
3 |
* |
4 |
* This program is free software; you can redistribute it and/or |
5 |
* modify it under the terms of the GNU General Public License |
6 |
* as published by the Free Software Foundation; either version 2 |
7 |
* of the License, or (at your option) any later version. |
8 |
* |
9 |
* This program is distributed in the hope that it will be useful, |
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
* GNU General Public License for more details. |
13 |
* |
14 |
* You should have received a copy of the GNU General Public License |
15 |
* along with this program; if not, write to |
16 |
* |
17 |
* The Free Software Foundation, Inc. |
18 |
* 59 Temple Place - Suite 330 |
19 |
* Boston, MA 02111-1307, USA. |
20 |
* |
21 |
* |
22 |
*/ |
23 |
|
24 |
#include "setup.h" |
25 |
|
26 |
#include <stdio.h> |
27 |
#include "opm.h" |
28 |
#include "opm_error.h" |
29 |
#include "opm_types.h" |
30 |
#include "compat.h" |
31 |
#ifdef HAVE_UNISTD_H |
32 |
# include <unistd.h> |
33 |
#endif |
34 |
|
35 |
RCSID("$Id: test.c,v 1.36 2003/06/20 04:55:14 andy Exp $"); |
36 |
|
37 |
#define ARRAY_SIZEOF(x) (sizeof(x) / sizeof((x)[0])) |
38 |
|
39 |
void open_proxy(OPM_T *, OPM_REMOTE_T *, int, void *); |
40 |
void negotiation_failed(OPM_T *, OPM_REMOTE_T *, int, void *); |
41 |
void timeout(OPM_T *, OPM_REMOTE_T *, int, void *); |
42 |
void end(OPM_T *, OPM_REMOTE_T *, int, void *); |
43 |
void handle_error(OPM_T *, OPM_REMOTE_T *, int, void *); |
44 |
|
45 |
int complete = 0; |
46 |
|
47 |
int main(int argc, char **argv) |
48 |
{ |
49 |
OPM_ERR_T err; |
50 |
int fdlimit = 1024; |
51 |
int scan_port = 6667; |
52 |
int max_read = 4096; |
53 |
int scantimeout = 10; |
54 |
unsigned int i, s; |
55 |
|
56 |
unsigned short http_ports[] = { |
57 |
8000, 8080, 3128, 80 |
58 |
}; |
59 |
|
60 |
unsigned short wingate_ports[] = { |
61 |
23 |
62 |
}; |
63 |
|
64 |
unsigned short router_ports[] = { |
65 |
23 |
66 |
}; |
67 |
|
68 |
unsigned short socks4_ports[] = { |
69 |
1080 |
70 |
}; |
71 |
|
72 |
unsigned short socks5_ports[] = { |
73 |
1080 |
74 |
}; |
75 |
|
76 |
unsigned short httppost_ports[] = { |
77 |
80, 8090, 3128 |
78 |
}; |
79 |
|
80 |
OPM_T *scanner; |
81 |
OPM_REMOTE_T *remote; |
82 |
|
83 |
scanner = opm_create(); |
84 |
|
85 |
if(argc >= 2) |
86 |
remote = opm_remote_create(argv[1]); |
87 |
else |
88 |
remote = opm_remote_create("127.0.0.1"); |
89 |
|
90 |
/* Setup callbacks */ |
91 |
opm_callback(scanner, OPM_CALLBACK_OPENPROXY, &open_proxy, NULL); |
92 |
opm_callback(scanner, OPM_CALLBACK_NEGFAIL, &negotiation_failed, NULL); |
93 |
opm_callback(scanner, OPM_CALLBACK_TIMEOUT, &timeout, NULL); |
94 |
opm_callback(scanner, OPM_CALLBACK_END, &end, NULL); |
95 |
opm_callback(scanner, OPM_CALLBACK_ERROR, &handle_error, NULL); |
96 |
|
97 |
|
98 |
/* Setup the scanner configuration */ |
99 |
opm_config(scanner, OPM_CONFIG_FD_LIMIT, &fdlimit); |
100 |
opm_config(scanner, OPM_CONFIG_SCAN_IP, "216.175.104.202"); |
101 |
opm_config(scanner, OPM_CONFIG_SCAN_PORT, &scan_port); |
102 |
opm_config(scanner, OPM_CONFIG_TARGET_STRING, "*** Looking up your hostname..."); |
103 |
opm_config(scanner, OPM_CONFIG_TARGET_STRING, "ERROR :Trying to reconnect too fast."); |
104 |
opm_config(scanner, OPM_CONFIG_TIMEOUT, &scantimeout); |
105 |
opm_config(scanner, OPM_CONFIG_MAX_READ, &max_read); |
106 |
|
107 |
/* Setup the protocol configuration */ |
108 |
for (s = ARRAY_SIZEOF(http_ports), i = 0; i < s; i++) { |
109 |
opm_addtype(scanner, OPM_TYPE_HTTP, http_ports[i]); |
110 |
} |
111 |
|
112 |
for (s = ARRAY_SIZEOF(wingate_ports), i = 0; i < s; i++) { |
113 |
opm_addtype(scanner, OPM_TYPE_WINGATE, wingate_ports[i]); |
114 |
} |
115 |
|
116 |
for (s = ARRAY_SIZEOF(router_ports), i = 0; i < s; i++) { |
117 |
opm_addtype(scanner, OPM_TYPE_ROUTER, router_ports[i]); |
118 |
} |
119 |
|
120 |
for (s = ARRAY_SIZEOF(socks4_ports), i = 0; i < s; i++) { |
121 |
opm_addtype(scanner, OPM_TYPE_SOCKS4, socks4_ports[i]); |
122 |
} |
123 |
|
124 |
for (s = ARRAY_SIZEOF(socks5_ports), i = 0; i < s; i++) { |
125 |
opm_addtype(scanner, OPM_TYPE_SOCKS5, socks5_ports[i]); |
126 |
} |
127 |
|
128 |
for (s = ARRAY_SIZEOF(httppost_ports), i = 0; i < s; i++) { |
129 |
opm_addtype(scanner, OPM_TYPE_HTTPPOST, httppost_ports[i]); |
130 |
} |
131 |
|
132 |
/* Remote structs can also have their own extended protocol configurations. For instance |
133 |
if the target hostname contains strings such as 'proxy' or 'www', extended ports could |
134 |
be scanned. */ |
135 |
opm_remote_addtype(remote, OPM_TYPE_HTTP, 8001); |
136 |
opm_remote_addtype(remote, OPM_TYPE_HTTP, 8002); |
137 |
|
138 |
switch(err = opm_scan(scanner, remote)) |
139 |
{ |
140 |
case OPM_SUCCESS: |
141 |
break; |
142 |
case OPM_ERR_BADADDR: |
143 |
printf("Bad address\n"); |
144 |
opm_free(scanner); |
145 |
opm_remote_free(remote); |
146 |
return 0; |
147 |
default: |
148 |
printf("Unknown Error %d\n", err); |
149 |
return 0; |
150 |
} |
151 |
|
152 |
|
153 |
while(!complete) |
154 |
opm_cycle(scanner); |
155 |
|
156 |
opm_free(scanner); |
157 |
|
158 |
return 0; |
159 |
} |
160 |
|
161 |
void open_proxy(OPM_T *scanner, OPM_REMOTE_T *remote, int notused, |
162 |
void *data) |
163 |
{ |
164 |
USE_VAR(notused); |
165 |
USE_VAR(data); |
166 |
|
167 |
printf("Open proxy on %s:%d [%d bytes read]\n", remote->ip, |
168 |
remote->port, remote->bytes_read); |
169 |
opm_end(scanner, remote); |
170 |
} |
171 |
|
172 |
void negotiation_failed(OPM_T *scanner, OPM_REMOTE_T *remote, int notused, |
173 |
void *data) |
174 |
{ |
175 |
USE_VAR(scanner); |
176 |
USE_VAR(notused); |
177 |
USE_VAR(data); |
178 |
|
179 |
printf("Negotiation on %s:%d failed [%d bytes read]\n", remote->ip, |
180 |
remote->port, remote->bytes_read); |
181 |
} |
182 |
|
183 |
void timeout(OPM_T *scanner, OPM_REMOTE_T *remote, int notused, void *data) |
184 |
{ |
185 |
USE_VAR(scanner); |
186 |
USE_VAR(notused); |
187 |
USE_VAR(data); |
188 |
|
189 |
printf("Negotiation timed out on %s:%d\n", remote->ip, remote->port); |
190 |
} |
191 |
|
192 |
void end(OPM_T *scanner, OPM_REMOTE_T *remote, int notused, void *data) |
193 |
{ |
194 |
USE_VAR(scanner); |
195 |
USE_VAR(notused); |
196 |
USE_VAR(data); |
197 |
|
198 |
printf("Scan on %s has ended\n", remote->ip); |
199 |
opm_remote_free(remote); |
200 |
complete = 1; |
201 |
} |
202 |
|
203 |
void handle_error(OPM_T *scanner, OPM_REMOTE_T *remote, int err, void *data) |
204 |
{ |
205 |
USE_VAR(scanner); |
206 |
USE_VAR(data); |
207 |
|
208 |
switch(err) |
209 |
{ |
210 |
case OPM_ERR_MAX_READ: |
211 |
printf("Reached MAX READ on %s:%d\n", remote->ip, remote->port); |
212 |
break; |
213 |
case OPM_ERR_BIND: |
214 |
printf("Unable to bind for %s:%d\n", remote->ip, remote->port); |
215 |
break; |
216 |
case OPM_ERR_NOFD: |
217 |
printf("Unable to allocate file descriptor for %s:%d\n", |
218 |
remote->ip, remote->port); |
219 |
break; |
220 |
default: |
221 |
printf("Unknown error on %s:%d, err = %d\n", remote->ip, |
222 |
remote->port, err); |
223 |
} |
224 |
} |