-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathpastemon.pl
executable file
·1340 lines (1239 loc) · 38.7 KB
/
pastemon.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
#
# pastemon.pl
#
# This script runs in the background as a daemon and monitors pastebin.com for
# interesting content (based on regular expressions). Found information is sent
# to syslog
#
# This script is based on the Python script written by Xavier Garcia
# (http://www.shellguardians.com/2011/07/monitoring-pastebin-leaks.html)
#
# Copyright (c) 2012 Xavier Mertens
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of copyright holders nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# History
# -------
# See README file
use strict;
use threads;
use threads::shared;
use Digest::MD5 qw(md5 md5_hex md5_base64);
use File::Path;
use Getopt::Long;
use IO::Socket;
use LWP::UserAgent;
use HTML::Entities;
use Sys::Syslog;
use Encode;
use XML::XPath;
use XML::XPath::XMLParser;
use Net::SMTP;
use POSIX qw(setsid);
# Optional modules
my $haveWordPressXMLRMC = eval "use WordPress::XMLRPC; 1";
my $haveTextJaroWinkler = eval "use Text::JaroWinkler qw(strcmp95); 1";
my $haveIOCompressGzip = eval "use IO::Compress::Gzip; 1";
my $haveIOUncompressGunzip = eval "use IO::Uncompress::Gunzip; 1";
my $haveDBI = eval "use DBI; 1";
use constant PROCESS_URL => 1;
use constant PASTEBIN => 0; # Supported websites
use constant PASTIE => 1;
use constant NOPASTE => 2;
use constant PASTESITE => 3;
my @webSiteNames = ( # Self-defined names for multiple usages
"pastebin.com",
"pastie.net",
"nopaste.me",
"pastesite.com",
);
my $program = "pastemon.pl";
my $version = "v1.14";
my $debug;
my $help;
my $ignoreCase; # By default respect case in strings search
my $cefDestination; # Send CEF events to this destination:port
my $cefPort = 514;
my $cefSeverity = 3;
my $caught = 0;
my $httpTimeout = 10; # Default HTTP timeout
my @pasties;
my @seenPasties;
my $maxPasties = 1000; # TODO: Make it configurable?
my @regexList; # List of interesting regex (with the data)
my $pidFile = "/var/run/pastemon.pid";
my $configFile = "/etc/pastemon.conf"; # Main XML configuration file
my $regexFile; # Regular expressions definitions
my $wpConfigFile;
my $proxyFile;
my @proxies;
my $uaFile;
my @uas;
my $wpSite; # Wordpress settings
my $wpUser;
my $wpPass;
my $wpCategory;
my $smtpServer; # SMTP settings
my $smtpFrom;
my $smtpRecipient;
my $smtpSubject;
my @smtpRecipients;
my $distanceMin;
my $distanceMaxSize;
my $followUrls; # Follow URLs found in pastie
my $followMatching;
my $checkPastebin; # Websites to monitor
my $checkPastie;
my $checkNopaste;
my $checkPastesite;
my $delayPastebin = 300; # Delays between pasties fetches
my $delayPastie = 300;
my $delayNopaste = 300;
my $delayPastesite = 300;
my $syslogFacility = "daemon";
my $dumpDir;
my $dumpAll;
my $compressDump;
my $sampleSize;
my %matches;
my $dbFile; # SQLite3 DB file
# Process arguments
my $result = GetOptions(
"debug" => \$debug,
"help" => \$help,
"config=s" => \$configFile,
);
# TODO: Add a "--drop-sql-table" option to rebuild a fresh DB?
if ($help) {
print <<__HELP__;
Usage: $0 --config=filepath [--debug] [--help]
Where:
--config : Specify the XML configuration file
--debug : Enable debug mode (verbose - do not detach)
--help : What you're reading now.
__HELP__
exit 0;
}
parseXMLConfigFile($configFile);
($debug) && print STDERR "+++ Running in foreground.\n";
($cefDestination) && syslogOutput("Sending CEF events to $cefDestination:$cefPort (severity $cefSeverity)");
# Do not allow multiple running instances!
if (-r $pidFile) {
open(PIDH, "<$pidFile") || die "Cannot read pid file!";
my $currentpid = <PIDH>;
close(PIDH);
die "$program already running (PID $currentpid)";
}
loadRegexFromFile($regexFile) || die "Cannot load regex from file $regexFile";
loadUserAgentFromFile($uaFile) || die "Cannot load user-agents from file $uaFile";
if (!$debug) {
my $pid = fork;
die "Cannot fork" unless defined($pid);
exit(0) if $pid;
# We are the child
(POSIX::setsid != -1) or die "setsid failed";
chdir("/") || die "Cannot changed working directory to /";
close(STDOUT);
close(STDOUT);
close(STDIN);
}
syslogOutput("Running with PID $$");
open(PIDH, ">$pidFile") || die "Cannot write PID file $pidFile: $!";
print PIDH "$$";
close(PIDH);
# Notify if HTTP proxy settings detected
if ($ENV{'HTTP_PROXY'}) {
($proxyFile) && die "The HTTP_PROXY environment variable conflicts with the use of a proxies list";
syslogOutput("Using detected HTTP proxy: " . $ENV{'HTTP_PROXY'});
}
my @threads;
my @webSites;
($checkPastebin) && push(@webSites, PASTEBIN);
($checkPastie) && push(@webSites, PASTIE);
($checkNopaste) && push(@webSites, NOPASTE);
($checkPastesite) && push(@webSites, PASTESITE);
# Launch threads based on the number of webistes to monitor
for my $webSite (@webSites) {
my $t = threads->new(\&mainLoop, $webSite);
push(@threads, $t);
}
$SIG{'TERM'} = \&sigHandler;
$SIG{'INT'} = \&sigHandler;
$SIG{'KILL'} = \&sigHandler;
$SIG{'USR1'} = sub {
foreach my $t (@threads) {
$t->kill('SIGUSR1');
}
};
# Parent process just waiting for a signal
while(1) {
sleep(1);
if ($caught) {
syslogOutput("Killing my threads");
foreach my $t (@threads) {
$t->kill('SIGKILL');
}
}
}
exit 0;
# ---------
# Main loop
# ---------
sub mainLoop {
$SIG{'USR1'} = \&sigReload; # Handle config reload
$SIG{'KILL'} = \&sigHandler;
my $webSite = shift;
while(1) {
my $pastie;
if (!&fetchLastPasties($webSite)) {
foreach $pastie (@pasties) {
exit 0 if ($caught == 1);
analyzePastie($webSite, $pastie, PROCESS_URL);
}
exit 0 if ($caught == 1);
}
purgeOldPasties($maxPasties);
# Wait some seconds (depending on the website)
DELAY: {
$webSite == PASTEBIN && do {
($debug) && print STDERR "Sleeping $delayPastebin\n";
sleep($delayPastebin); last DELAY; };
$webSite == PASTIE && do {
($debug) && print STDERR "Sleeping $delayPastie\n";
sleep($delayPastie); last DELAY; };
$webSite == NOPASTE && do {
($debug) && print STDERR "Sleeping $delayNopaste\n";
sleep($delayNopaste); last DELAY; };
$webSite == PASTESITE && do {
($debug) && print STDERR "Sleeping $delayPastesite\n";
sleep($delayPastesite); last DELAY; };
}
}
}
#
# analyzePastie
#
sub analyzePastie {
my $webSite = shift;
my $pastie = shift or return;
my $processUrl = shift;
my $regex;
my $md5;
if (!grep /$pastie/, @seenPasties) {
my $content = fetchPastie($pastie);
if ($content) {
# If we receive a "slow down" message, follow Pastebin recommandation!
if ($content =~ /Please slow down/) {
($debug) && print STDERR "+++ Slow down message received. Paused 5 seconds\n";
sleep(5);
}
else {
# Compute the MD5 digest
$md5 = md5_hex(encode('UTF8',$content));
if (!dbSearchMD5($md5)) {
undef(%matches); # Reset the matches regex/counters
my $i = 0;
my $regexSearch;
my $regexInclude;
my $regexExclude;
my $regexDesc;
my $regexCount;
foreach $regex (@regexList) {
$regexSearch = @$regex[0];
$regexInclude = @$regex[1];
$regexExclude = @$regex[2];
$regexDesc = @$regex[3];
$regexCount = @$regex[4];
my $sampleData;
my ($startPos, $endPos);
my $preCount = 0;
if ($ignoreCase) {
$preCount += () = $content =~ /$regexSearch/mgi;
$startPos = $-[0];
$endPos = $+[0];
}
else {
$preCount += () = $content =~ /$regexSearch/mg;
$startPos = $-[0];
$endPos = $+[0];
}
if ($preCount >= $regexCount) {
if ($sampleSize) {
# Optional: extract a sample of the data
$startPos = (($startPos - $sampleSize) < 0) ? 0 : ($startPos - $sampleSize);
$sampleData = encode('UTF8', substr($content, $startPos, ($endPos - $startPos) + $sampleSize));
}
# Process "include" regex defined
if ($regexInclude ne "") {
my $postCount = 0;
if ($ignoreCase) {
$postCount += () = $content =~ /$regexInclude/mgi;
} else {
$postCount += () = $content =~ /$regexInclude/mg;
}
if ($postCount) {
# Matches for include $regex
$matches{$i} = [ ( $regexSearch, $preCount, $sampleData ) ];
$i++;
}
}
elsif ($regexExclude ne "") {
my $postCount = 0;
if ($ignoreCase) {
$postCount += () = $content =~ /$regexExclude/mgi;
} else {
$postCount += () = $content =~ /$regexExclude/mg;
}
if (! $postCount) {
# Matches for exclude $regex
$matches{$i} = [ ( $regexSearch, $preCount, $sampleData ) ];
$i++;
}
}
else {
$matches{$i} = [ ( $regexSearch, $preCount, $sampleData ) ];
$i++;
}
}
}
if ($followUrls && $processUrl) {
$i += processUrls($content);
}
if ($i) {
# Try to find a corresponding pastie?
if (!FuzzyMatch($webSite, $content))
{
# Generate the results based on matches
my $buffer = "Found in " . $pastie . " : ";
my $key;
for $key (keys %matches) {
$buffer = $buffer . $matches{$key}[0] . " (" . $matches{$key}[1] . " times) ";
}
if ($sampleSize) {
# Optional: Add sample of data
my $safeData = $matches{0}[2];
# Sanitize the data
$safeData =~ s///g;
$safeData =~ s/\n/\\r/g;
$safeData =~ s/\n/\\n/g;
$safeData =~ s/\t/\\t/g;
$buffer = $buffer . "| Sample: " . $safeData;
}
syslogOutput($buffer);
# Generating CEF event (if configured)
($cefDestination) && sendCEFEvent($pastie);
# Generating blog post (if configured)
($wpSite) && createBlogPost($pastie);
# Send SMTP notification (if configured)
if ($smtpServer) {
my $smtp = Net::SMTP->new($smtpServer) or die "Cannot create SMTP connection to $smtpServer: $?";
$smtp->mail($smtpFrom);
$smtp->recipient(@smtpRecipients, { SkipBad => 1});
$smtp->data();
my $subjectTags;
for $key (keys %matches) {
my $tempDesc = getRegexDesc($matches{$key}[0]);
if (length($tempDesc) > 0) {
$subjectTags = $subjectTags . '(' . getRegexDesc($matches{$key}[0]) . ') ';
}
}
my $smtpBody = "To: $smtpRecipient\nSubject: $smtpSubject $subjectTags\n\n";
for $key (keys %matches) {
$smtpBody = $smtpBody . "Matched: " . $matches{$key}[0] . " (" . $matches{$key}[1] . " time(s))\n";
}
$smtpBody = $smtpBody . "\nSource: " . $pastie . "\n\n" . $content;
$smtp->datasend($smtpBody);
$smtp->dataend();
$smtp->quit();
}
# Save pastie content in the dump directory (if configured)
if ($dumpDir) {
my $tempPastie = getPastieID($pastie);
my $tempDir = validateDumpDir($webSite, $dumpDir); # Generate and create dump directory
(-d $tempDir) or die "Cannot validate directory $dumpDir: $!";
open(DUMP, ">:encoding(UTF-8)", "$tempDir/$tempPastie.raw") or die "Cannot write to $tempDir/$tempPastie.raw : $!";
for $key (keys %matches) {
print DUMP "Matched: " . $matches{$key}[0] . " (" . $matches{$key}[1] . " time(s))\n";
}
print DUMP "\n$content";
close(DUMP);
if ($compressDump) { # Compress pastie
my $in = "$tempDir/$tempPastie.raw";
my $out = "$tempDir/$tempPastie.gz";
use IO::Compress::Gzip qw(gzip);
if (gzip $in => $out) {
unlink("$tempDir/$tempPastie.raw");
}
else {
syslogOutput("Cannot compress $tempDir/$tempPastie.raw: $!");
}
}
}
}
}
elsif ($dumpAll && $dumpDir) {
# Mirroring mode - dump the pastie in all cases
my $tempPastie = getPastieID($pastie);
my $tempDir = validateDumpDir($webSite, $dumpDir);
(-d $tempDir) or die "Cannot validate directory $tempDir: $!";
open(DUMP, ">:encoding(UTF-8)", "$tempDir/$tempPastie.raw") or die "Cannot write to $tempDir/$tempPastie.raw : $!";
print DUMP "\n$content";
close(DUMP);
if ($compressDump) { # Compress pastie
my $in = "$tempDir/$tempPastie.raw";
my $out = "$tempDir/$tempPastie.gz";
use IO::Compress::Gzip qw(gzip);
if (gzip $in => $out) {
unlink("$tempDir/$tempPastie.raw");
}
else {
syslogOutput("Cannot compress $tempDir/$tempPastie.raw: $!");
}
}
}
# Flag this pastie as "seen"
push(@seenPasties, $pastie);
# Save pastie data in SQLite
if ($dbFile) {
dbSavePastie($pastie, $md5);
}
# Wait a random number of seconds to not mess with pastebin.com webmasters
sleep(int(rand(5)));
}
else { # MD5 Exists in DB
($debug) && print "DEBUG: MD5 $md5 already found in DB!\n";
}
}
}
}
}
#
# Search for interesting data in URLs found inside the pastie
#
sub processUrls {
my $pastie = shift || return 0;
while ($pastie =~ m,(http.*?://([^\s)\"](?!ttp:))+),g) { # "
my $url = $&;
if ($url =~ /$followMatching/gi) { #Process only URLs matching our regex!
($debug) && print "+++ Following URL: $url\n";
my $ua = LWP::UserAgent->new;
$ua->agent(getRandomUA());
my $r = $ua->head("$url");
if ($r->is_success && substr($r->header('Content-Type'), 0, 5) eq "text/") { # Only process "text"
analyzePastie($url);
}
}
# Protect us against pastebin.com blacklist?
#sleep(int(rand(15)));
}
return 0;
}
#
# parseXMLConfigFile
# Load the configuration from provided XML file
# Args:
# $configFile = Main pastemon.conf XML file
#
sub parseXMLConfigFile {
my $configFile = shift;
(-r $configFile) || die "Cannot load XML file $configFile: $!";
($debug) && print STDERR "+++ Loading XML file $configFile.\n";
my $xml = XML::XPath->new(filename => "$configFile");
my $buff;
# Reset settings
undef $pidFile;
undef $sampleSize;
undef $dumpDir;
undef $dumpAll;
undef $compressDump;
undef $proxyFile;
undef $uaFile;
undef $cefDestination;
undef $cefPort;
undef $cefSeverity;
undef $smtpServer;
undef $smtpFrom;
undef $smtpRecipient;
undef $smtpSubject;
undef $wpSite;
undef $wpUser;
undef $wpPass;
undef $wpCategory;
undef $distanceMin;
undef $distanceMaxSize;
undef $checkPastebin;
undef $checkPastie;
undef $checkNopaste;
undef $checkPastesite;
undef $followUrls;
undef $followMatching;
undef $dbFile;
# Core Parameters
my $nodes = $xml->find('/pastemon/core');
foreach my $node ($nodes->get_nodelist) {
$buff = $node->find('ignore-case')->string_value;
if (lc($buff) eq "yes" || $buff eq "1") {
$ignoreCase++;
($debug) && print STDERR "+++ Non-sensitive search enabled.\n";
}
$buff = $node->find('dump-all')->string_value;
if (lc($buff) eq "yes" || $buff eq "1") {
$dumpAll++;
($debug) && print STDERR "+++ Dumping all pasties (mirror mode).\n";
}
$buff = $node->find('compress-pasties')->string_value;
if (lc($buff) eq "yes" || $buff eq "1") {
$compressDump++;
($debug) && print STDERR "+++ Compressing all pasties (mirror mode).\n";
}
$pidFile = $node->find('pid-file')->string_value;
$regexFile = $node->find('regex-file')->string_value;
$sampleSize = $node->find('sample-size')->string_value;
$dumpDir = $node->find('dump-directory')->string_value;
$proxyFile = $node->find('proxy-config')->string_value;
$uaFile = $node->find('ua-config')->string_value;
$httpTimeout = $node->find('http-timeout')->string_value;
$distanceMin = $node->find('distance-min')->string_value;
$distanceMaxSize = $node->find('distance-max-size')->string_value;
}
# Monitored websites
my $nodes = $xml->find('/pastemon/websites');
foreach my $node ($nodes->get_nodelist) {
$buff = $node->find('pastebin')->string_value;
if (lc($buff) eq "yes" || $buff eq "1") {
$checkPastebin++;
($debug) && print STDERR "+++ pastebin.com monitoring activated.\n";
}
$buff = $node->find('pastie')->string_value;
if (lc($buff) eq "yes" || $buff eq "1") {
$checkPastie++;
($debug) && print STDERR "+++ pastie.com monitoring activated.\n";
}
$buff = $node->find('nopaste')->string_value;
if (lc($buff) eq "yes" || $buff eq "1") {
$checkNopaste++;
($debug) && print STDERR "+++ nopaste.me monitoring activated.\n";
}
$buff = $node->find('pastesite')->string_value;
if (lc($buff) eq "yes" || $buff eq "1") {
$checkPastesite++;
($debug) && print STDERR "+++ pastesite.com monitoring activated.\n";
}
$delayPastebin = $node->find('pastebin-delay')->string_value;
$delayPastie = $node->find('pastie-delay')->string_value;
$delayNopaste = $node->find('nopaste-delay')->string_value;
$delayPastesite = $node->find('pastesite-delay')->string_value;
}
# Follow URLs
my $nodes = $xml->find('/pastemon/urls');
foreach my $node ($nodes->get_nodelist) {
$buff = $node->find('follow')->string_value;
if (lc($buff) eq "yes" || $buff eq "1") {
$followUrls++;
($debug) && print STDERR "+++ Follow URLs feature activated.\n";
}
$followMatching = $node->find('matching')->string_value;
}
# CEF Parameters
my $nodes = $xml->find('/pastemon/cef-output');
foreach my $node ($nodes->get_nodelist) {
$cefDestination = $node->find('destination')->string_value;
$cefPort = $node->find('port')->string_value;
$cefSeverity = $node->find('severity')->string_value;
}
# Syslog Parameters
my $nodes = $xml->find('/pastemon/syslog-output');
foreach my $node ($nodes->get_nodelist) {
$syslogFacility = $node->find('facility')->string_value;
}
# Wordpress Parameters
my $nodes = $xml->find('/pastemon/wordpress-output');
foreach my $node ($nodes->get_nodelist) {
$wpSite = $node->find('site')->string_value;
$wpUser = $node->find('user')->string_value;
$wpPass = $node->find('password')->string_value;
$wpCategory = $node->find('category')->string_value;
}
# SMTP Parameters
my $nodes = $xml->find('/pastemon/smtp-output');
foreach my $node ($nodes->get_nodelist) {
$smtpServer = $node->find('smtp-server')->string_value;
$smtpFrom = $node->find('from')->string_value;
$smtpRecipient = $node->find('recipient')->string_value;
$smtpSubject = $node->find('subject')->string_value;
}
# SQLite3 Parameters
my $nodes = $xml->find('/pastemon/db-output');
foreach my $node ($nodes->get_nodelist) {
$dbFile = $node->find('db-file')->string_value;
}
# ---------------------
# Parameters validation
# ---------------------
# Check if the provided dump directory is writable to us
if ($dumpDir) {
# (-w $dumpDir) or die "Directory $dumpDir is not writable: $!";
syslogOutput("Using $dumpDir as dump directory");
}
# Compress dumped pasties?
if ($compressDump) {
if ($haveIOCompressGzip) { # Module IO::Compress::Gzip installed?
if (!$dumpDir) {
syslogOutput("Option compress-pasties disabled: No dump directory defined");
undef $compressDump
}
if (!$haveIOUncompressGunzip) { # Module IO::Compress::Gunzp installed?
syslogOutput("Option compress-pasties disabled: IO::Uncompress:Gunzip not installed");
undef $compressDump;
}
}
else {
syslogOutput("Option compress-pasties disabled: IO::Compress:Gzip not installed");
undef $compressDump;
}
}
# Dumping all pasties requires a dump directory
if ($dumpAll && !$dumpDir) {
syslogOutput("No dump directory specified");
}
# Verifiy sampleSize format if specified
if ($sampleSize) {
die "Sample buffer length must be an integer!" if not $sampleSize =~ /\d+/;
syslogOutput("Dumping $sampleSize bytes samples");
}
# Verify the HTTP timeout if specified
if ($httpTimeout) {
die "HTTP timeout must be an integer!" if not $httpTimeout =~ /\d+/;
syslogOutput("HTTP timeout: $httpTimeout seconds");
}
# Verify Wordpress config
if ($wpSite) {
if ($haveWordPressXMLRMC) { # Module WordPress::XMLRPC installed?
(!$wpSite || !$wpUser || !$wpPass || !$wpCategory) && die "Incomplete Wordpress configuration";
($sampleSize) || die "A sample buffer length must be given with Wordpress output";
syslogOutput("Dumping data to $wpSite/xmlrpc.php");
}
else {
syslogOutput("Wordpress configuration disabled: Wordpress::XMLRPC not installed");
undef $wpSite;
}
}
# Verify SMTP config
if ($smtpServer) {
(!$smtpServer || !$smtpFrom || !$smtpRecipient || !$smtpSubject) && die "Incomplete SMTP configuration";
my $smtp = Net::SMTP->new($smtpServer) or die "Cannot use SMTP server $smtpServer: $?";
$smtp->quit();
@smtpRecipients = split(/[, ]+/, $smtpRecipient);
syslogOutput("Sending SMTP notifications to <".$smtpRecipient.">");
}
# Load proxies
if ($proxyFile) {
(-r $proxyFile) or die "Cannot read proxy configuration file $proxyFile: $!";
loadProxyFromFile($proxyFile) || die "Cannot load proxies from file $proxyFile";
}
# Distance
if ($distanceMin) {
if ($haveTextJaroWinkler) { # Module Text::JaroWinkler installed?
(!$dumpDir) && die "A dump directory must be configured to use the distance check";
($distanceMin > 0 && $distanceMin < 1) or die "Minimum distance must be between 0 and 1";
if ($distanceMaxSize) {
die "Distance max size must be an integer!" if not $distanceMaxSize =~ /\d+/;
syslogOutput("Enabled duplicate detection with distance of $distanceMin (size limit: $distanceMaxSize bytes)");
} else {
syslogOutput("Enabled duplicate detection with distance of $distanceMin");
}
}
else {
syslogOutput("Distance configuration disabled: Text::JaroWinkler not installed");
undef $distanceMin;
}
}
# SQLite3 Output
if ($dbFile) {
if ($haveDBI) { # Module DBI installed?
# Do we have to initialize the DB (first execution)
my $dbh = DBI->connect("dbi:SQLite:dbname=" . $dbFile)
or die "Cannot connect to the SQLite DB " . $dbFile . "\n";
my $sth = $dbh->prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='pasties'");
$sth->execute();
my $data = $sth->fetch();
if (!$data) { # Tables 'pasties' does not exists. Create it.
$sth = $dbh->prepare("CREATE TABLE pasties (id VARCHAR(50),
timestamp DATETIME,
url VARCHAR(128),
matched VARCHAR(256),
path VARCHAR(256),
md5 VARCHAR(32) PRIMARY KEY,
type INTEGER)");
$sth->execute() or die "Cannot create table 'pasties'";
$sth = $dbh->prepare("CREATE UNIQUE INDEX pasties_idx ON pasties(id)");
$sth->execute() or die "Cannot create index 'pasties_idx'";
($debug) && print STDERR "+++ Created database " . $dbFile . "\n";
}
$dbh->disconnect();
}
else {
syslogOutput("DB support disabled: DBI not installed");
undef $dbFile;
}
}
# Follow URL
if ($followUrls && !$followMatching) {
syslogOutput("Warning: No regex defined to match URLs");
$followMatching = ".*"; # Match everything
}
return;
}
#
# Download the latest pasties and load them in a Perl array
# (http://pastebin.com/archive)
#
sub fetchLastPasties {
my $webSite = shift;
my $tempProxy;
my $ua = LWP::UserAgent->new;
$ua->timeout($httpTimeout);
if (@proxies) {
$tempProxy = selectRandomProxy();
$ua->proxy('http', $tempProxy);
}
else {
($ENV{'HTTP_PROXY'}) && $ua->env_proxy;
}
$ua->agent(getRandomUA());
undef @pasties; # Reset the array first!
# www.pastebin.com
if ($webSite == PASTEBIN) {
($debug) && print STDERR "Loading new pasties from pastebin.com.\n";
my $response = $ua->get("http://pastebin.com/archive");
if ($response->is_success) {
# Load the pasties into an array
# @pasties = $response->decoded_content =~ /<td class=\"icon\"><a href=\"\/(\w+)\">.+<\/a><\/td>/g;
# New format (2012/02/19):
my @tempPasties = $response->decoded_content =~ /<a href=\"\/(\w{8})\">.+<\/a><\/td>/g;
# Append the complete URL
foreach my $p (@tempPasties) {
$p = 'http://pastebin.com/raw.php?i=' . $p;
}
push(@pasties, @tempPasties);
}
else {
syslogOutput("Cannot fetch www.pastebin.com: " . $response->status_line);
# If cannot fetch pastie and we use proxies, disable the current one!
(@proxies) && disableProxy($tempProxy);
return 1;
}
}
elsif ($webSite == PASTIE) {
#($debug) && print STDERR "Loading new pasties from pastie.org.\n";
my $response = $ua->get("http://pastie.org/pastes");
if ($response->is_success) {
my @tempPasties = $response->decoded_content =~ /<a href=\"(http:\/\/pastie.org\/pastes\/\d{7})\">/g;
# Append the complete URL
foreach my $p (@tempPasties) {
$p = $p . '/download';
}
push(@pasties, @tempPasties);
}
else {
syslogOutput("Cannot fetch www.pastie.org: " . $response->status_line);
# If cannot fetch pastie and we use proxies, disable the current one!
(@proxies) && disableProxy($tempProxy);
return 1;
}
}
elsif ($webSite == NOPASTE) {
#($debug) && print STDERR "Loading new pasties from nopaste.me.\n";
my $response = $ua->get("http://nopaste.me/recent");
if ($response->is_success) {
my @tempPasties = $response->decoded_content =~ /<a href=\"http:\/\/nopaste.me\/paste\/([a-z0-9]+)\">/ig;
# Append the complete URL
foreach my $p (@tempPasties) {
$p = 'http://nopaste.me/raw/' . $p . '.txt';
}
push(@pasties, @tempPasties);
}
else {
syslogOutput("Cannot fetch nopaste.me: " . $response->status_line);
# If cannot fetch pastie and we use proxies, disable the current one!
(@proxies) && disableProxy($tempProxy);
return 1;
}
}
elsif ($webSite == PASTESITE) {
($debug) && print STDERR "Loading new pasties from pastesite.com.\n";
my $response = $ua->get("http://pastesite.com/recent");
if ($response->is_success) {
my @tempPasties = $response->decoded_content =~ /<a href=\"(\d+)\" title=\"View this Paste/ig;
# Append the complete URL
foreach my $p (@tempPasties) {
$p = 'http://pastesite.com/' . $p;
}
push(@pasties, @tempPasties);
}
else {
syslogOutput("Cannot fetch pastesite.com: " . $response->status_line);
# If cannot fetch pastie and we use proxies, disable the current one!
(@proxies) && disableProxy($tempProxy);
return 1;
}
}
else {
die "Unknown website constant: $webSite";
}
# DEBUG
#foreach my $p (@pasties) {
# print "DEBUG: $p\n";
#}
return 0;
}
#
# Fetch the raw content of a pastie and return its content
#
sub fetchPastie {
my $tempProxy;
my $pastie = shift;
my $ua = LWP::UserAgent->new;
$ua->timeout($httpTimeout);
if (@proxies) {
$tempProxy = selectRandomProxy();
$ua->proxy('http', $tempProxy);
}
else {
($ENV{'HTTP_PROXY'}) && $ua->env_proxy;
}
$ua->agent(getRandomUA());
my $response = $ua->get("$pastie");
if ($response->is_success) {
# Hack for pastesite.com: Extract data from the <textarea> </textarea>
# (To bypass the <continue> button)
if ($pastie =~ /http:\/\/pastesite.com/) {
if ($response->decoded_content =~ /\<textarea .*\>(.*)\<\/textarea\>/igs) {
my $pastesiteContent = $1;
return $pastesiteContent;
}
}
else {
return $response->decoded_content;
}
}
($debug) && print STDERR "+++ Cannot fetch pastie $pastie: " . $response->status_line . "\n";
# If cannot fetch pastie and we use proxies, disable the current one!
(@proxies) && disableProxy($tempProxy);
return "";
}
#
# Load the regular expressions from the configuration file to a Perl array
#
sub loadRegexFromFile {
my $file = shift;
die "A configuration file is required" unless defined($file);
undef @regexList; # Clean up array (if reloaded via SIGUSR1
( -r "$file") || die "Cannot open file $file: $!";
my $xp = XML::XPath->new( filename => "$file");
my $ns = $xp->find('/config/regex');
foreach my $n ($ns->get_nodelist) {
my @r;
push(@r, $n->find('search')->string_value);
push(@r, $n->find('include')->string_value);
push(@r, $n->find('exclude')->string_value);
push(@r, $n->find('description')->string_value);
if ($n->find('count')->string_value ne "") {
push(@r,$n->find('count')->string_value);
} else {
push(@r, "1");
}
push(@regexList, [ @r ]);
}
syslogOutput("Loaded " . @regexList . " regular expressions from " . $file);
return(1);
}
#
# Load proxies from the configuration file
#
sub loadProxyFromFile {
my $file = shift;
return(1) unless defined($file);
open(PROXY_FD, "$file") || die "Cannot open file $file : $!";
while(<PROXY_FD>) {
chomp;
(length > 0) && push(@proxies, 'http://'.$_);
}
close(PROXY_FD);
(@proxies) || die "No proxies read from $file";
syslogOutput("Loaded " . @proxies . " proxies from " . $file);
return(1);
}
#
# Return a random proxy from the loaded list
#
sub selectRandomProxy {
my $randomIdx = rand($#proxies);
# ($debug) && print STDERR "+++ Using proxy: " . $proxies[$randomIdx] . "\n";
return $proxies[$randomIdx];
}
#
# Remove a faulty proxy from the proxies array
#
sub disableProxy {
my $badProxy = shift;
return unless defined($badProxy);
my $p;
my $i = 0;
foreach $p (@proxies) {
$i++;
if ($p eq $badProxy) { last; }
}
# delete $proxies[$i]; -- DEPRECATED
splice @proxies, $i, 1;
syslogOutput("Disabled unreliable proxy " . $badProxy . " (" . @proxies . ' active proxies)');
}
sub purgeOldPasties {
my $max = shift;
while (@seenPasties > $max) {
#delete $seenPasties[0]; -- DEPRECATED
splice @seenPasties, 0, 1;
}
return;
}
#