-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrebellion.pl
executable file
·2027 lines (1659 loc) · 55.2 KB
/
rebellion.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 -w
# Marcin Gryszkalis <[email protected]>
# https://github.com/marcin-gryszkalis/rebellion
my $VERSION = '0.7';
use strict;
use warnings;
use POSIX qw/SIGHUP strftime/;
use Time::HiRes qw/time/;
#use WWW::Curl::Easy;
#use WWW::Curl::Form;
use Net::Curl::Easy qw(:constants);
use Net::Curl::Form qw(:constants);
use threads (
'stack_size' => 32*4096, # override with env. PERL5_ITHREADS_STACK_SIZE
'exit' => 'threads_only',
);
use threads::shared;
# use forks;
# use forks::shared;
use Thread::Queue;
# use Event::Lib;
use Data::Dumper;
use URI::Escape;
use HTML::Entities;
use File::Slurp;
my $DEBUG_NO_BURL = 0;
my $DEBUG_NO_DELAY = 0;
my $DEBUG_ONE_SHOT = 0;
my $DEBUG_ALWAYS_SAVE_VERIFICATION_LOG = 0;
# global config, defaults
my %cfg = (
logfile => './sparky.log',
sparkytee => 0,
scenario => undef,
skippages => undef,
duration => 60,
tests => 1,
sourceip => undef,
);
my @status :shared;
my @round :shared;
my $gen_dowod_prefix = 'BBD';
my $gen_dowod_seed :shared = 0;
my $global_rawpost_data = undef;
my $rebels = 0;
my @def_headers = ();
my $def_headers_h;
my %def_variables = ();
my $def_variables_per_fox;
my @def_verifynegs = ();
my %def_delays = ();
my @varfiles = ();
my @varfiles_names = ();
my $stackfiles;
my @stackfiles_names = ();
my @scenario_base = ();
my @scenario = ();
# my $skippages = '';
# my $browsers = 1;
# my @foxies = ();
# my $duration = 600;
# my $tests = undef;
# my $rate = undef;
# my @army = ();
# my @src_ip = ();
#
my $war_is_over :shared = 0;
my $devnull;
my $curl;
# Signals:
$SIG{PIPE} = 'IGNORE';
# options
use Getopt::Std;
our ($opt_c, $opt_h, $opt_V, $opt_l, $opt_d, $opt_b, $opt_p, $opt_o, $opt_a);
sub VERSION_MESSAGE() { print "$VERSION\n"; }
sub HELP_MESSAGE()
{
print "Usage: rebellion.pl [-hV] [-l log file] -c config-file
-c config file\t\tMain config file
-l log file\t\tSparky logger file
-p proto://host:port\t\tproxy defnition (proto: http, https, socks)
-d\t\tno delays
-b\t\tno static elements (b-url)
-o\t\tone shot
-a\t\talways save verification logs
-h\t\tthis help
-V\t\tversion info
";
}
getopts('c:hl:Vbdop:a');
if ($opt_h) { HELP_MESSAGE(); exit; }
if ($opt_V) { VERSION_MESSAGE(); exit; }
if ($opt_d) { $DEBUG_NO_DELAY = 1; }
if ($opt_b) { $DEBUG_NO_BURL = 1; }
if ($opt_o) { $DEBUG_ONE_SHOT = 1; }
if ($opt_a) { $DEBUG_ALWAYS_SAVE_VERIFICATION_LOG = 1; }
if ($opt_p) { $cfg{proxy} = $opt_p; }
# # === return channel for main thread
my $queue_re = Thread::Queue->new();
# hashref of queues
my $queues_varlist;
# ================ sparky logger stuff
my $queue_sparky = Thread::Queue->new();
sub now()
{
my @t = localtime time;
return sprintf("%4d-%02d-%02d %02d:%02d:%02d", $t[5]+1900, $t[4]+1, $t[3], $t[2], $t[1], $t[0]);
}
sub thread_sparky
{
print STDERR "SPARKY:Alive\n";
$cfg{logfile} = $opt_l if defined $opt_l;
open(my $l, ">$cfg{logfile}") or die("cannot create logfile ($cfg{logfile}): $!");
# my $old = select($l);
# $| = 1;
# select($old);
$l->autoflush(1);
$queue_re->enqueue("OK");
while (my $msg = $queue_sparky->dequeue())
{
chomp $msg;
my ($mode, $msg) = split/\s+/, $msg, 2;
last if $msg eq 'END';
my $date = now();
print $l "$date $msg\n";
print STDERR "$date $msg\n" if ($cfg{sparkytee} || $mode eq 'HIGH');
}
close($l);
}
sub LOG($)
{
$queue_sparky->enqueue("LOW ".shift);
}
sub MSG($)
{
$queue_sparky->enqueue("HIGH ".shift);
}
sub randxy($$)
{
my $a = shift;
my $b = shift;
return int($a + rand($b - $a + 1));
}
# === Generators
my $last_pesel = '';
sub gen_pesel()
{
# http://wipos.p.lodz.pl/zylla/ut/pesel.html
my @wagi = qw{1 3 7 9 1 3 7 9 1 3};
my $pesel = sprintf("%02d%02d%02d%03d%d",
randxy(50,90),
randxy(1,12),
randxy(1,28),
randxy(1,999),
randxy(0,4)*2 + 1); # even = female, odd = male
my @c = split //, $pesel;
my $sum = 0;
for (my $i = 0; $i < 10; $i++)
{
my $x = $c[$i] * $wagi[$i];
$sum += $x;
}
my $mod = (10 - ($sum % 10)) % 10;
$last_pesel = "$pesel$mod";
return $last_pesel;
}
sub gen_date_from_last_pesel
{
$last_pesel =~ /^(..)(..)(..)/;
return sprintf("%04d-%02d-%02d", $1 + 1900, $2 ,$3);
}
sub gen_nip()
{
my $nip;
my $mod;
while (1)
{
my @wagi = qw{6 5 7 2 3 4 5 6 7};
$nip = sprintf("%03d%03d%03d",
randxy(100,199),
randxy(100,999),
randxy(100,999));
my @c = split //, $nip;
my $sum = 0;
for (my $i = 0; $i < 9; $i++)
{
my $x = $c[$i] * $wagi[$i];
$sum += $x;
}
$mod = (10 - ($sum % 10)) % 10;
$mod = $sum % 11;
last unless $mod == 10;
}
return "$nip$mod";
}
sub gen_regon()
{
my $regon;
my $mod;
while (1)
{
my @wagi = qw{8 9 2 3 4 5 6 7};
$regon = randxy(10000000,99999999);
my @c = split //, $regon;
my $sum = 0;
for (my $i = 0; $i < 8; $i++)
{
my $x = $c[$i] * $wagi[$i];
$sum += $x;
}
# $mod = (10 - ($sum % 10)) % 10;
$mod = $sum % 11;
last unless $mod == 10;
}
return "$regon$mod";
}
sub gen_iban()
{
my $iban = "11402004"."0000".randxy(1111,9999).randxy(1111,9999).randxy(1111,9999);
my $xban = $iban."252100"; # PL
my $mod = 0;
for my $c (split//,$xban)
{
$mod = (10 * $mod + $c) % 97;
}
$mod = 98 - $mod;
$iban = sprintf "%02d%s", $mod, $iban;
return $iban;
}
sub gen_dowod()
{
# http://pl.wikipedia.org/wiki/Dow%C3%B3d_osobisty_w_Polsce
my @wagi = qw{7 3 1 0 7 3 1 7 3};
$gen_dowod_seed++;
my $do = sprintf("%s%06d", $gen_dowod_prefix, $gen_dowod_seed);
my @c = split //, $do;
my $sum =
$wagi[0] * (ord($c[0])-55) +
$wagi[1] * (ord($c[1])-55) +
$wagi[2] * (ord($c[2])-55) +
$wagi[3] * $c[3] + # ctrl, will be 0 as wagi[3]==0
$wagi[4] * $c[4] +
$wagi[5] * $c[5] +
$wagi[6] * $c[6] +
$wagi[7] * $c[7] +
$wagi[8] * $c[8];
my $mod = $sum % 10;
return sprintf("%s%d%05d", $gen_dowod_prefix, $mod, $gen_dowod_seed);
}
sub gen_random_text($)
{
my $len = shift;
my @chars=('a'..'z');
my $v = "";
foreach (1..$len)
{
$v .= $chars[int rand scalar @chars];
}
return $v;
}
sub gen_email()
{
return gen_random_text(10).'@'.gen_random_text(10).'.pl';
}
sub gen_random_number($$)
{
my $min = shift;
my $max = shift;
return sprintf("%d", rand($max - $min + 1) + $min);
}
my @first_names = qw{
Piotr Krzysztof Andrzej Jan Tomasz Marcin Marek Grzegorz Adam Zbigniew Jerzy Tadeusz Mateusz Dariusz Mariusz Wojciech Ryszard Jakub Henryk Robert Kazimierz Jacek Maciej Kamil
Anna Maria Katarzyna Agnieszka Barbara Krystyna Ewa Zofia Teresa Magdalena Joanna Janina Monika Danuta Jadwiga Aleksandra Halina Irena Beata Marta Dorota Helena Karolina Jolanta Iwona Marianna Natalia
};
sub gen_firstname
{
return $first_names[int rand scalar @first_names];
}
sub gen_lastname
{
my @names = qw{
Nowak Kowalski Lewandowski Kowalczyk Jankowski Wojciechowski Kwiatkowski Kaczmarek Mazur Krawczyk Piotrowski Grabowski Nowakowski Michalski Nowicki Adamczyk Dudek Wieczorek Majewski Olszewski Jaworski Malinowski Pawlak Witkowski
};
return $names[int rand scalar @names];
}
sub gen_company
{
my $n = $first_names[int rand scalar @first_names];
my @sufix = qw/Trans Pol ex Agra Bis Art/;
my @mod = ("Sp.z o.o.", "SA", "Sp.j.");
my $s = $sufix[int rand scalar @sufix];
my $m = $mod[int rand scalar @mod];
return "$n$s $m";
}
sub gen_city
{
my @names = qw{
Warszawa Szczecin Bydgoszcz Lublin Katowice Gdynia Radom Sosnowiec Kielce Gliwice Olsztyn Tychy Opole Koszalin Kalisz Legnica
};
return $names[int rand scalar @names];
}
sub gen_list($)
{
my $qn = shift;
my $a = $queues_varlist->{$qn}->dequeue();
if (!defined $a)
{
MSG "PROBLEM:Queue($qn) is empty";
return '0'; # oh well
}
else
{
LOG "VARLIST($qn)=($a)";
$queues_varlist->{$qn}->enqueue($a); # store it back, so it doesn't dry
}
return $a;
}
sub gen_zipcode
{
return sprintf("%02d-%03d", int rand(100), int rand(1000))
}
# http://pl.wikipedia.org/wiki/Vehicle_Identification_Number
# 17-chars
# TMB AH6NH8 D4042851
sub gen_vin
{
return sprintf("TMBAH6NH8%08d", int rand(99999999))
}
sub gen_date
{
return strftime("%F", localtime);
}
sub gen_date_add($)
{
my $add = shift;
return strftime("%F", localtime(time() + ($add * 86400)));
}
sub gen_time
{
return strftime("%T", localtime());
}
sub gen_time_add($)
{
my $add = shift;
return strftime("%T", localtime(time() + $add));
}
sub gen_datetime
{
return strftime("%F %T", localtime());
}
sub gen_datetime_add($)
{
my $add = shift;
return strftime("%F %T", localtime(time() + $add));
}
# 1990-12-31T23:59:60.123Z
# 1996-12-19T16:39:57.123-08:00
# 2020-10-30T11:33:02.077+0000 <- currently this form w/o TZ offset (ie. localtime without proper offset)
# /!\ fraction of seconds is random
sub gen_datetime_rfc3339
{
my $msec = sprintf("%.3f", time());
$msec =~ s/.*\.//;
return strftime("%FT%T.${msec}+0000", localtime());
}
sub gen_datetime_rfc3339_add($)
{
my $add = shift;
my $msec = sprintf("%.3f", time());
$msec =~ s/.*\.//;
return strftime("%FT%T.${msec}+0000", localtime(time() + $add));
}
sub process_generators
{
my $url = shift;
$url =~ s/{:TEXT\((\d+)\)}/gen_random_text($1)/ge;
$url =~ s/{:NUMBER\((\d+),(\d+)\)}/gen_random_number($1,$2)/ge;
$url =~ s/{:FIRSTNAME}/gen_firstname()/ge;
$url =~ s/{:LASTNAME}/gen_lastname()/ge;
$url =~ s/{:COMPANY}/gen_company()/ge;
$url =~ s/{:LIST\((\S+?)\)}/gen_list($1)/ge;
$url =~ s/{:PESEL}/gen_pesel()/ge;
$url =~ s/{:DATE_FROM_LAST_PESEL}/gen_date_from_last_pesel()/ge;
$url =~ s/{:NIP}/gen_nip()/ge;
$url =~ s/{:REGON}/gen_regon()/ge;
$url =~ s/{:IBAN}/gen_iban()/ge;
$url =~ s/{:VIN}/gen_vin()/ge;
$url =~ s/{:ZIPCODE}/gen_zipcode()/ge;
$url =~ s/{:CITY}/gen_city()/ge;
$url =~ s/{:EMAIL}/gen_email()/ge;
$url =~ s/{:DOWOD}/gen_dowod()/ge;
$url =~ s/{:DATE}/gen_date()/ge;
$url =~ s/{:DATE\+(\d+)}/gen_date_add($1)/ge; # add days
$url =~ s/{:TIME}/gen_time()/ge;
$url =~ s/{:TIME\+(\d+)}/gen_time_add($1)/ge; # add seconds
$url =~ s/{:DATETIME}/gen_datetime()/ge;
$url =~ s/{:DATETIME\+(\d+)}/gen_datetime($1)/ge;
$url =~ s/{:DATETIME_RFC3339}/gen_datetime_rfc3339()/ge;
$url =~ s/{:DATETIME_RFC3339\+(\d+)}/gen_datetime_rfc3339($1)/ge;
$url =~ s/{:UNIXTIME}/int(time())/ge;
return $url;
}
my $mimecache;
sub check_mime($)
{
my $b = shift;
my $mt = undef;
if (exists $mimecache->{$b})
{
$mt = $mimecache->{$b};
}
else
{
$mt = `file --mime-type --brief '$b'` || 'text/plain';
chomp $mt;
$mimecache->{$b} = $mt;
}
return $mt;
}
sub process_variables
{
my $h = shift;
my $text = shift;
my $noenc = shift // 0;
# setup variables
for my $k (keys %$h)
{
my $v = $h->{$k};
$v = uri_escape($v) unless $k =~ /^(NOENC|DECHTML):(.*)/ || $noenc;
$v = decode_entities($v) if $k =~ /^DECHTML:(.*)/;
# print STDERR "VAR($k = $v)\n";
$text =~ s/\Q{$k}\E/$v/g;
}
return $text;
}
# =============================================================================
# xxx REBELS xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# =============================================================================
my $body = undef;
my $body_h = undef; # file handle to $body
sub init_curl($)
{
my $curl = shift;
$curl->setopt(CURLOPT_NOSIGNAL, 1);
# TODO notfound $curl->setopt(CURLOPT_SSLVERSION, CURL_SSLVERSION_SSLv3);
$curl->setopt(CURLOPT_SSL_VERIFYPEER, 0);
$curl->setopt(CURLOPT_SSL_VERIFYHOST, 0);
# wywala bledy dla http kodow >= 400:
$curl->setopt(CURLOPT_FAILONERROR, 0);
# we;ll do parsing ourself (TODO: why?)
$curl->setopt(CURLOPT_HEADER, 1);
# for debug
# $curl->setopt(CURLOPT_VERBOSE, 1);
$curl->setopt(CURLOPT_FOLLOWLOCATION, 1);
$curl->setopt(CURLOPT_AUTOREFERER, 1);
$curl->setopt(CURLOPT_MAXREDIRS, 64);
# check
$curl->setopt(CURLOPT_FORBID_REUSE, 0);
# move to config
$curl->setopt(CURLOPT_CONNECTTIMEOUT, 60);
$curl->setopt(CURLOPT_POSTFIELDSIZE, -1); # curl will take care ### TODO: backport change from rebel.c
# curl->setopt(curl, CURLOPT_COOKIESESSION, 1);
$curl->setopt(CURLOPT_COOKIEFILE, "");
if (defined $cfg{proxy})
{
$curl->setopt(CURLOPT_PROXY, $cfg{proxy});
if ($cfg{proxy} =~ m{^socks})
{
$curl->setopt(CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
}
elsif ($cfg{proxy} =~ m{^https})
{
$curl->setopt(CURLOPT_PROXYTYPE, CURLPROXY_HTTPS);
}
else
{
$curl->setopt(CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
# $curl->setopt(CURLOPT_PROXYTYPE, CURLPROXY_HTTP_1_0);
}
# curl->setopt(curl, CURLOPT_HTTPPROXYTUNNEL, 1);
}
if (defined $cfg{sourceip})
{
$curl->setopt(CURLOPT_INTERFACE, $cfg{sourceip});
}
# CURLOPT_MAX_SEND_SPEED_LARGE
# CURLOPT_MAX_RECV_SPEED_LARGE
# other config?
# - CURLOPT_INTERFACE
# - http auth
return $curl;
}
sub reset_curl($)
{
my $curl = shift;
## XXX from WWW::Curl -- $curl->cleanup();
# $curl = undef;
$curl = new Net::Curl::Easy;
$curl = init_curl($curl);
return $curl
}
sub setup_fox($)
{
my $id = shift;
my $fox = undef;
$fox->{status} = 'start';
$fox->{line} = -1; # reference to source
$fox->{round} = 0;
$fox->{variables} = ();
$fox->{variables}->{variable_placeholder} = "placeholder";
$fox->{headers} = ();
$fox->{id} = $id;
# $fox->{ip} = $src_ip[$id%($#src_ip+2)] if $#src_ip >= 0;
@scenario = @scenario_base;
return $fox;
}
sub setup_stack_variables($)
{
my $fox = shift;
# get next from stackfiles
for my $variable (@stackfiles_names)
{
my $var = shift @{$stackfiles->{$variable}};
die "stackfile ($variable) too short" unless $var;
chomp $var;
redo if $var =~ /(^\s*$|^#)/;
$fox->{variables}->{$variable} = $var;
LOG "STACKFILE($fox->{id}:$variable)=($var)";
}
return $fox;
}
sub reset_scenario($)
{
my $fox = shift;
my $id = $fox->{id};
my $save_round = $fox->{round} + 1;
LOG "REBEL($fox->{id}:$fox->{line}):RESET SCENARIO";
# mostly an ugly copy :( - incomplete!!!
# assume this is dead stuff
#
# if (exists $fox->{fallback})
# {
# my $url = $fox->{fallback};
# my $post = undef;
# my $upload = undef;
# my $curlf = undef;
# if ($url =~ /^\s*(\S+)\s+POST\s+(\S+)/)
# {
# $url = $1;
# $post = $2;
# $fox->{post} = $post; # just to remember
# }
# if ($url =~ /^\s*(\S+)\s+UPLOAD\s+(.*)/)
# {
# $url = $1;
# $upload = $2;
# $curlf = Net::Curl::Form->new;
# MSG "UP($upload)";
# my @upfields = split/\s+/, $upload;
# while (@upfields)
# {
# my $a = shift @upfields;
# my $b = shift @upfields;
# MSG "XF($a)->($b)";
# if (-f $b)
# {
# my $mt = `file --mime-type '$b'` || 'text/plain';
# chomp $mt;
# $curlf->formaddfile($b, $a, $mt);
# MSG "UPLOAD($a -> $b [$mt])";
# }
# else
# {
# $curlf->formadd($a, $b);
# }
# }
# $fox->{upload} = $upload; # just to remember
# }
# $url = $fox->{base}."/$url" unless $url =~ m{^https?://};
# $url =~ s{([^:])/+}{$1/}g; # remove multiple /////
# $fox->{url} = $url;
# $curl->setopt(CURLOPT_WRITEDATA, $devnull);
# $curl->setopt(CURLOPT_HTTPGET, 1); # reset to GET, will be overwritten later
# $curl->setopt(CURLOPT_POST, 0);
# $curl->setopt(CURLOPT_URL, $url);
# # fprintf(stderr, "F%d:URL:PAGEID(%s):URL(%s)\n", id, pageid, realurl);
# if ($post)
# {
# $curl->setopt(CURLOPT_POST, 1);
# $curl->setopt(CURLOPT_COPYPOSTFIELDS, $post);
# }
# if ($upload)
# {
# $curl->setopt(CURLOPT_POST, 1);
# $curl->setopt(CURLOPT_HTTPPOST, $curlf);
# }
# $curl->setopt(CURLOPT_HTTPHEADER, \@def_headers);
# my $cpr;
# eval { $curl->perform(); };
# if ( ref $@ eq "Net::Curl::Easy::Code" )
# {
# $cpr = int($@);
# }
# else
# {
# # rethrow everyting else
# MSG "UNEXPECTED ERROR: $@";
# die $@;
# }
# LOG "REBEL($fox->{id}:$fox->{line}):FALLBACK:".
# "URL($url)".
# ($post ? ":POST($post)" : "").
# ($upload ? ":UPLOAD($upload)" : "");
# }
$fox = setup_fox($id);
$fox->{id} = $id;
$fox->{round} = $save_round;
$fox->{status} = 'start';
$round[$id] = $save_round;
$status[$id] = 'start';
if (defined $def_variables_per_fox)
{
for (keys %{$def_variables_per_fox})
{
$fox->{variables}->{$_} = $def_variables_per_fox->{$_}; # just to make them show
}
}
$fox = setup_stack_variables($fox);
if ($war_is_over || $DEBUG_ONE_SHOT)
{
LOG "REBEL($fox->{id}:0):END-OF-WAR";
$fox->{status} = 'finished';
$status[$id] = 'finished';
}
$curl = reset_curl($curl);
return $fox;
}
my $verlognum = 0;
sub verification_log($$$)
{
my $fox = shift;
my $reason = shift;
my $vn = shift;
my $orygbody = $fox->{body} // 'EMPTY-BODY';
delete $fox->{body};
my $body = '';
my $headers = ''; my $copyheaders = 1;
for my $l (split/[\r\n]+/,$orygbody)
{
if ($copyheaders)
{
if ($l =~ /^\S+:\s+\S+/ || $l =~ /^\s*$/ || $l =~ /^HTTP\/1.[01]\s+\d\d\d/)
{
$headers .= "$l\n";
}
else
{
$body .= "$l\n";
$copyheaders = 0;
}
}
else
{
$body .= "$l\n";
}
}
$verlognum++;
my $vlnumx = sprintf "%04d", $verlognum;
my $fnumx = sprintf "%04d", $fox->{id};
my $vlogf = "verification.$fnumx.$vlnumx.$vn.log.html";
mkdir("dbg") unless -d "dbg";
open(V, ">dbg/$vlogf") or die "cannot create verification log: $!";
print V "<h2>$fox->{page}</h2>\n";
print V "<h2>$fox->{url}</h2>\n";
my $vnow = strftime("%F %T", localtime);
print V "<h2>$vnow</h2><hr>\n";
print V "<h2>$reason [thread $fox->{id} at line $fox->{line}]</h2><hr>\n";
print V "<pre style='font-size: 10px;'>\n".(Dumper $fox), "\n</pre>\n<hr>\n";
print V "<pre style='font-size: 10px; white-space: pre-wrap; '>\n$headers\n</pre>\n<hr>\n";
# disable popup escape
# if(top!=self)top.location.href=location.href;
# if (top === self){window.location = '/'; }
$body =~ s/top[ !=]+self/false/g;
print V $body;
close(V);
$fox->{body} = $orygbody;
# print V
# "VERFAIL:$reason\n",
# (Dumper $fox),
# "\n\n";
}
sub writedata_callback
{
my ($curl, $chunk, $handle) = @_;
$body .= $chunk;
return length $chunk;
}
sub thread_rebel
{
my $id = shift;
$status[$id] = 'start';
$round[$id] = 0;
my $fox = setup_fox($id);
# read varfiles
for my $varfile (@varfiles)
{
my @vf = read_file($varfile) or die("cannot open varfile($varfile): $!");
@vf = grep {!/^#/ } grep {!/^\s*$/} @vf;
my $varname = shift @varfiles_names;
my $var = $vf[$id];
die("varfile ($varname) too short") unless $var;
chomp $var;
$fox->{variables}->{$varname} = $var;
LOG "VARFILE($id:$varname)=($var)";
}
my %tmph = %{$fox->{variables}};
$def_variables_per_fox = \%tmph;
$fox = setup_stack_variables($fox);
# prepare curl
$curl = new Net::Curl::Easy;
$curl = init_curl($curl);
$curl = reset_curl($curl);
$curl->setopt(CURLOPT_HEADER,1);
$curl->setopt(CURLOPT_URL, '');
$curl->setopt(CURLOPT_CUSTOMREQUEST, undef);
my $leading_delay = int(rand($cfg{leading_delay} * $cfg{browsers})+1);
LOG "REBEL($id:X) Start (delay: $leading_delay)";
$queue_re->enqueue($id);
sleep($leading_delay);
# === loop =====================================
my $skippages_active = 0;
my $skippages_skipto = undef;
my $verifyuntiltry = 0;
my $verifyuntillasturl = undef;
my $verifyuntilredo = 0;
my @verifyuntilstack = ();
while (1)
{
last if $fox->{status} eq 'finished';
my $vlvn = $cfg{verification_log_variable};
my $vlv = exists $fox->{variables}->{$vlvn} ? "$vlvn=$fox->{variables}->{$vlvn}" : "-";
my $vlvx = exists $fox->{variables}->{$vlvn} ? $fox->{variables}->{$vlvn} : "-";
my $sline = shift @scenario;
if (not defined $sline) # end of scenario
{
LOG "REBEL($fox->{id}:0):END-OF-ROUND";
$skippages_active = 0;
$fox = reset_scenario($fox);
next;
}
$fox->{line} = $sline->{line};
next if ($skippages_active and $sline->{k} ne 'page');
push(@verifyuntilstack, { %$sline });
# print "VU-STACK:", Dumper \@verifyuntilstack;
if ($sline->{k} eq 'skipto')
{
if ($sline->{v} eq 'END')
{
LOG "REBEL($fox->{id}:$fox->{line}):SKIPTO(END)";
# $fox->{lineact} = $#scenario + 10; # skip past end of scenario, it'll got hit above
# die "not implemented with new auto-burl stuff and no lineact ";
# next;
$fox = reset_scenario($fox);
next;
}
elsif ($sline->{v} =~ m/^(\S+)\s*$/)
{
my $target = $1;
$skippages_active = 1;
$skippages_skipto = $target;
LOG "REBEL($fox->{id}:$fox->{line}):SKIPTO(ALWAYS:$target)";
next;
}
elsif ($sline->{v} =~ m/^RND(\d+)\s+(\S+)/)
{
my $rnd = $1;
my $target = $2;
if (int(rand(100)) < $rnd)
{
$skippages_active = 1;
$skippages_skipto = $target;
LOG "REBEL($fox->{id}:$fox->{line}):SKIPTO(RANDOM:$rnd:$target:YES)";
}
else
{
LOG "REBEL($fox->{id}:$fox->{line}):SKIPTO(RANDOM:$rnd:$target:NO)";
}
next;
}
else
{
MSG "REBEL($fox->{id}:$fox->{line}):SKIPTO(SYNTAX ERROR:$sline->{v})";
}
}
if ($sline->{k} eq 'verify' || $sline->{k} eq 'verifyneg')
{
if (not defined $fox->{body})
{
MSG "REBEL($fox->{id}:$fox->{line}:$vlv):ERROR:Verification Failed - body empty";