-
Notifications
You must be signed in to change notification settings - Fork 45
/
urlgrep.pl
executable file
·479 lines (396 loc) · 11.3 KB
/
urlgrep.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
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
#!/usr/bin/perl
#####################################
# URLgrep v0.5.7 #
# by x0rz <[email protected]> #
# #
# http://code.google.com/p/urlgrep/ #
#####################################
########## usage example ###########
# ./urlgrep.pl -u \
# "http://www.comptechdoc.org/os/linux/howlinuxworks/linux_hlproc.html" \
# -r "\.html$" -d 1 -o file
#####################################
# debug
use strict;
use warnings;
use LWP::Simple qw($ua get);;
use HTML::LinkExtor;
use HTML::HeadParser;
use Term::ANSIColor;
use Getopt::Long;
use Smart::Comments;
use threads;
use threads::shared;
# Globals
our @crawled : shared; # list of crawled urls
our @targets : shared; # list of urls that matches the regexp
our @targets_misc : shared; # list of misc links
our %UGCONF; # URLgrep configuration
$UGCONF{'VERSION'} = "0.5.7-dev";
$UGCONF{'TIMEOUT'} = 5;
$UGCONF{'DEPTH'} = 1;
$UGCONF{'MAXTHREADS'} = 4;
$UGCONF{'NOTHREADS'} = 0;
# Options
our $entry_url = "";
our $regexp = "^.*\$";
our $verbose = 0;
our $help = 0;
our $output = "";
our $casei = 0;
our $invert = 0;
our $all = 0;
our $cookie_file = "";
# Catching Ctrl-C
$SIG{INT} = \&tsktsk;
GetOptions ('v|verbose' => \$verbose,
'depth=i' => \$UGCONF{'DEPTH'},
'url=s' => \$entry_url,
'regexp=s' => \$regexp,
'i|ignore-case' => \$casei,
'm|invert-match' => \$invert,
'output=s' => \$output,
'help' => sub { helpmessage() },
'version' => sub { helpmessage() },
'all' => \$all,
'timeout=i' => \$UGCONF{'TIMEOUT'},
'cookie=s' => \$cookie_file,
'no-threads' => \$UGCONF{'NOTHREADS'});
## $entry_url
$ua->env_proxy(); # load env proxy (*_proxy)
$ua->timeout($UGCONF{'TIMEOUT'});
$ua->agent('Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; fr; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3');
# Load cookie (if asked)
if ($cookie_file ne "") {
$ua->cookie_jar({ file => $cookie_file });
}
# Catching Ctrl-C
$SIG{INT} = \&tsktsk;
# check for mandatory options
if ($entry_url eq "") {
print_comm (&usage());
exit 1;
}
# Computing host
my $host = find_hostname($entry_url);
## $host
print_comm ("Running URLgrep on " . $entry_url ."\n");
print_comm ("Regexp: ".($invert? "!" : "")."/".$regexp."/".($casei? "i" : "")."\n");
print_comm ("Started on ".gmtime()." \n");
# Call first root URL
parseURL($entry_url, 0);
if ($verbose == 0) {
print ("\n");
}
finishing();
# Functions ##############################
# Ctrl-C catcher
sub tsktsk {
print ("\n");
print_comm ("Catching Ctrl-C!\n");
# terminating threads
if (!$UGCONF{'NOTHREADS'}) {
print_comm ("Killing threads... \n");
my @threads = threads->list();
foreach my $thr (@threads) {
$thr->detach();
print "Thread ".$thr->tid()." killed.\n"
}
}
finishing();
exit 0;
}
# show usage
sub usage {
return "usage: ./urlgrep.pl -u URL [-r -i -m -d -a -o -t -c -v -h]\n";
}
# show the help
sub helpmessage {
print_comm ("URLgrep v".$UGCONF{'VERSION'}."\n");
print_comm ("by x0rz <hourto_c\@epita.fr>\n");
print_comm ("http://code.google.com/p/urlgrep/\n");
print_comm ("\n");
print_comm (&usage());
print_comm ("-u http_url, --url http_url\n", "bold");
print_comm (" target webpage's url\n");
print_comm ("-d n, --depth n\n", "bold");
print_comm (" set the depth of the crawler (default=1)\n");
print_comm ("-a, --all\n", "bold");
print_comm (" will search outside of the specified website\n");
print_comm ("-r exp, --regexp exp\n", "bold");
print_comm (" the regular expression you want to apply\n");
print_comm ("-i, --ignore-case\n", "bold");
print_comm (" ignore case distinctions\n");
print_comm ("-m, --invert-match\n", "bold");
print_comm (" invert the sense of matching\n");
print_comm ("-o file, --output file\n", "bold");
print_comm (" specify the output file if you want to log the search\n");
print_comm ("-t n, --timeout n\n", "bold");
print_comm (" set the timeout when requesting a page (default=5s)\n");
print_comm ("-c file, --cookie file\n", "bold");
print_comm (" specify your cookie file\n");
print_comm ("-n, --no-threads\n", "bold");
print_comm (" won't use threads\n");
print_comm ("-v, --verbose\n", "bold");
print_comm (" verbose mode\n");
print_comm ("-h, --help\n", "bold");
print_comm (" show the help message\n");
exit 0;
}
# return domain
sub find_hostname {
my $url = $_[0];
$url =~ s!^https?://(?:www\.)?!!i;
$url =~ s!/.*!!;
$url =~ s/[\?\#\:].*//;
return $url;
}
# return domain and sub-domains
sub find_wwwhost {
return (URI->new($_[0])->host);
}
# grep the list with the given options and regexp
sub greplist {
my @grep = {};
if ($casei) {
if ($invert) {
@grep = grep(!/$regexp/i, @{$_[0]});
}
else {
@grep = grep(/$regexp/i, @{$_[0]});
}
} else {
if ($invert) {
@grep = grep(!/$regexp/, @{$_[0]});
} else {
@grep = grep(/$regexp/, @{$_[0]});
}
}
return @grep;
}
sub remove_duplicates {
my %seen = ();
my @unique;
foreach my $item (@{$_[0]}) {
push(@unique, $item) unless $seen{$item}++;
}
return @unique;
}
sub finishing {
# terminating threads
if (!$UGCONF{'NOTHREADS'}) {
my @threads = threads->list();
foreach my $thr (@threads) {
$thr->join();
print "Thread ".$thr->tid()." terminated.\n"
}
}
print_comm ("Finished on ".gmtime()." \n");
print_ok();
print "Crawl done [".scalar(@crawled)." URL(s) visited].\n";
# removing duplicates
@targets = remove_duplicates(\@targets);
# searching in misc links
@targets_misc = greplist(\@targets_misc);
# removing duplicates for misc links
@targets_misc = remove_duplicates(\@targets_misc);
if (scalar(@targets) == 0) {
print_ko();
print color 'red';
print "No target found.\n";
print color 'reset';
} else {
print_ok();
print color 'red';
print scalar(@targets)." URL(s) found matching /".$regexp."/".($casei? "i" : "")."\n";
foreach my $link (@targets) {
print_info();
print $link."\n";
}
if ($output ne "") {
print_comm ("Generating output...\n");
if (!open FILE, ">", $output) {
print_ko();
print "Couldn't create file.\n";
} else {
foreach my $link (@targets) {
print FILE $link."\n";
}
print_ok();
print "URLs correctly written in " .$output. "\n";
}
}
}
if (scalar(@targets_misc) != 0) {
print_comm ("Also found ".scalar(@targets_misc)." special link(s) that may interest you:\n");
}
foreach my $link (@targets_misc) {
print_info();
print $link."\n";
}
}
sub parseURL {
if ($verbose == 1) {
print_ok();
print "Trying (d:".$_[1].") " . $_[0] . "\n";
} else {
print ".";
$|++;
}
# adding url to crawled list (with mutex)
{
lock(@crawled);
push(@crawled, "$_[0]");
}
## @crawled
# Get the HTML page
my $content = get($_[0]);
## $content
if (!defined $content) {
if ($verbose) {
print_ko();
print "Couldn't reach the page.\n";
}
return;
}
# Extract header data (for <base> tag essentially)
my $head = HTML::HeadParser->new;
$head->parse($content);
# Setting up the current base (can be null)
my $base = $head->header('Content-Base');
# Extract links
my $parser = HTML::LinkExtor->new();
$parser->parse($content);
my @parse = $parser->links;
my @links : shared;
foreach my $link (@parse) {
push @links, "".constructURL($link->[2], $_[0], $base);
}
# remving empty links
@links = grep(!/^\ *$/, @links);
# Adding the grep results to the targets list
my @grep : shared = greplist(\@links);
{
lock (@targets);
@targets = (@targets, @grep);
}
if ($verbose == 1) {
print_ok();
print scalar(@links) . " link(s) found.\n";
if (scalar(@grep) != 0) {
print " > " . scalar(@grep)." matched!\n";
}
}
# Testing current depth
if ($_[1] < $UGCONF{'DEPTH'}) {
# Grabbing all urls
foreach my $link (@links) {
my $visited = 0;
# Checking if already done
# wainting lock...
{
lock (@crawled);
# cond_wait(@crawled);
foreach my $url_done (@crawled) {
if ($link eq $url_done){
$visited = 1;
}
}
}
# if not visited yet, parse it
if ($visited == 0) {
# do not browse css/js/images/etc.
if (!($link =~ m/.*\.(gif|jpe?g|png|css|js|ico|swf|axd|jsp|pdf)$/i)) {
crawl_rec($link, $_[1]);
}
}
}
}
}
sub crawl_rec {
# if we want to go through all the links (not only local to the website)
if ($all) {
parse_thread($_[0], $_[1]);
} else {
# Calculating host of the link
my $link_host = find_hostname($_[0]);
if ($link_host eq $host) {
parse_thread($_[0], $_[1]);
}
}
}
sub parse_thread {
my $thread_count = threads->list();
if ($UGCONF{'NOTHREADS'} || $thread_count >= $UGCONF{'MAXTHREADS'}) {
parseURL($_[0], $_[1] + 1);
} else {
threads->create(\&parseURL, $_[0], int($_[1] + 1));
}
}
sub constructURL {
# 0 = link
# 1 = page
# 2 = base (from <base> tag, can be null)
my $complete_url;
local $URI::ABS_REMOTE_LEADING_DOTS = 1;
local $URI::ABS_ALLOW_RELATIVE_SCHEME = 1;
# capture weird links (javascript, anchor, others protocols, etc.)
if (($_[0] =~ m!^(\w+:.+|#).*!) &&
!($_[0] =~ m!^(https?://).*!i)) {
# we keep it in our misc list but not anchor links
if (! ($_[0] =~ m!^#.*!)) {
lock (@targets_misc);
push (@targets_misc, $_[0]);
}
# return "" so it won't be part of the list
return "";
}
# building correct link
if (defined $_[2]) {
$complete_url = URI->new($_[0])->abs($_[2]);
} else {
$complete_url = URI->new($_[0])->abs($_[1]);
}
return $complete_url;
#print ("0 " . $_[0]."\n");
#print ("1 " .$_[1]."\n");
#print("==> " . $newURL."\n");
}
# Misc
sub print_ok {
print color 'bold white';
print "[";
print color 'green';
print "OK";
print color 'white';
print "] ";
print color 'reset';
}
sub print_ko {
print color 'bold white';
print "[";
print color 'red';
print "KO";
print color 'white';
print "] ";
print color 'reset';
}
sub print_info {
print color 'bold white';
print "[";
print color 'blue';
print ">>";
print color 'white';
print "] ";
print color 'reset';
}
sub print_comm {
print color 'bold red';
print "# ";
if (!((defined $_[1]) && $_[1] eq "bold")) {
print color 'reset';
}
print color 'yellow';
print $_[0];
print color 'reset';
}