-
Notifications
You must be signed in to change notification settings - Fork 43
/
104fx_import_shavar_cryptominers.pl
executable file
·425 lines (372 loc) · 10.9 KB
/
104fx_import_shavar_cryptominers.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
#!/usr/bin/perl -s
# This descends from huepl and TTYtter, and is therefore under the Floodgap
# Free Software License. It is not required to build or run Firefox or
# TenFourFox.
BEGIN {
$VERSION = "v0.5";
if ($] >= 5.014000 && $ENV{'PERL_SIGNALS'} ne 'unsafe') {
$signals_use_posix = 1;
} else {
$ENV{'PERL_SIGNALS'} = 'unsafe';
}
}
$lastexception = 0;
@wend = ('-L', '-s', '-m', '20', '-A', '', '-H', 'Expect:');
eval "use POSIX;";
$URL = "https://raw.githubusercontent.com/mozilla-services/shavar-prod-lists/master/disconnect-blacklist.json";
$exit_mode = 0;
$stringify_args = sub {
my $basecom = shift;
my $resource = shift;
my $data = shift;
my $p;
my $l = '';
foreach $p (@_) {
if ($p =~ /^-/) {
$l .= "\n" if (length($l));
$l .= "$p ";
next;
}
$l .= $p;
}
$l .= "\n";
# if resource is an arrayref, then it's a GET with URL
# and args
$resource = join('?', @{ $resource })
if (ref($resource) eq 'ARRAY');
$l .= "url = \"$resource\"\n";
$data =~ s/"/\\"/g;
$l .= "data = \"$data\"\n" if length($data);
return ("$basecom -K -", $l, undef);
};
sub sigify {
# this routine abstracts setting signals to a subroutine reference.
# check and see if we have to use POSIX.pm (Perl 5.14+) or we can
# still use $SIG for proper signalling. We prefer the latter, but
# must support the former.
my $subref = shift;
my $k;
if ($signals_use_posix) {
my @w;
my $sigaction = POSIX::SigAction->new($subref);
while ($k = shift) {
my $e = &posix_signal_of($k);
# some signals may not exist on all systems.
next if (!(0+$e));
POSIX::sigaction($e, $sigaction)
|| die("sigaction failure: $! $@\n");
}
} else {
while ($k = shift) { $SIG{$k} = $subref; }
}
}
sub posix_signal_of {
die("never call posix_signal_of if signals_use_posix is false\n")
if (!$signals_use_posix);
# this assumes that POSIX::SIG* returns a scalar int value.
# not all signals exist on all systems. this ensures zeroes are
# returned for locally bogus ones.
return 0+(eval("return POSIX::SIG".shift));
}
sub parsejson {
my $data = shift;
my $my_json_ref = undef; # durrr hat go on foot
my $i;
my $tdata;
my $seed;
my $bbqqmask;
my $ddqqmask;
my $ssqqmask;
# test for single logicals
return {
'ok' => 1,
'result' => (($1 eq 'true') ? 1 : 0),
'literal' => $1,
} if ($data =~ /^['"]?(true|false)['"]?$/);
# first isolate escaped backslashes with a unique sequence.
$bbqqmask = "BBQQ";
$seed = 0;
$seed++ while ($data =~ /$bbqqmask$seed/);
$bbqqmask .= $seed;
$data =~ s/\\\\/$bbqqmask/g;
# next isolate escaped quotes with another unique sequence.
$ddqqmask = "DDQQ";
$seed = 0;
$seed++ while ($data =~ /$ddqqmask$seed/);
$ddqqmask .= $seed;
$data =~ s/\\\"/$ddqqmask/g;
# then turn literal ' into another unique sequence. you'll see
# why momentarily.
$ssqqmask = "SSQQ";
$seed = 0;
$seed++ while ($data =~ /$ssqqmask$seed/);
$ssqqmask .= $seed;
$data =~ s/\'/$ssqqmask/g;
# here's why: we're going to turn doublequoted strings into single
# quoted strings to avoid nastiness like variable interpolation.
$data =~ s/\"/\'/g;
# and then we're going to turn the inline ones all back except
# ssqq, which we'll do last so that our syntax checker still works.
$data =~ s/$bbqqmask/\\\\/g;
$data =~ s/$ddqqmask/"/g;
print STDOUT "$data\n" if ($superverbose);
# first, generate a syntax tree.
$tdata = $data;
1 while $tdata =~ s/'[^']*'//; # empty strings are valid too ...
$tdata =~ s/-?[0-9]+\.?[0-9]*([eE][+-][0-9]+)?//g;
# have to handle floats *and* their exponents
$tdata =~ s/(true|false|null)//g;
$tdata =~ s/\s//g;
print STDOUT "$tdata\n" if ($superverbose);
# now verify the syntax tree.
# the remaining stuff should just be enclosed in [ ], and only {}:,
# for example, imagine if a bare semicolon were in this ...
if ($tdata !~ s/^\[// || $tdata !~ s/\]$// || $tdata =~ /[^{}:,]/) {
$tdata =~ s/'[^']*$//; # cut trailing strings
if (($tdata =~ /^\[/ && $tdata !~ /\]$/)
|| ($tdata =~ /^\{/ && $tdata !~ /\}$/)) {
# incomplete transmission
&exception(10, "*** JSON warning: connection cut\n");
return undef;
}
if ($tdata =~ /(^|[^:])\[\]($|[^},])/) { # oddity
&exception(11, "*** JSON warning: null list\n");
return undef;
}
# at this point all we should have are structural elements.
# if something other than JSON structure is visible, then
# the syntax tree is mangled. don't try to run it, it
# might be unsafe.
if ($tdata =~ /[^\[\]\{\}:,]/) {
&exception(99, "*** JSON syntax error\n");
die(<<"EOF");
--- data received ---
$data
--- syntax tree ---
$tdata
--- JSON PARSING ABORTED DUE TO SYNTAX TREE FAILURE --
EOF
exit;
return undef;
}
}
# syntax tree passed, so let's turn it into a Perl reference.
# have to turn colons into ,s or Perl will gripe. but INTELLIGENTLY!
1 while
($data =~ s/([^'])'\s*:\s*(true|false|null|\'|\{|\[|-?[0-9])/\1\',\2/);
# removing whitespace to improve interpretation speed actually made
# it SLOWER.
#($data =~ s/'\s*,\s*/',/sg);
#($data =~ s/\n\s*//sg);
# finally, single quotes, just before interpretation.
$data =~ s/$ssqqmask/\\'/g;
# now somewhat validated, so safe (?) to eval() into a Perl struct
eval "\$my_json_ref = $data;";
print STDOUT "$data => $my_json_ref $@\n" if ($superverbose);
# do a sanity check
if (!defined($my_json_ref)) {
&exception(99, "*** JSON syntax error\n");
print STDOUT <<"EOF" if ($verbose);
--- data received ---
$data
--- syntax tree ---
$tdata
--- JSON PARSING FAILED --
$@
--- JSON PARSING FAILED --
EOF
}
return $my_json_ref;
}
sub backticks {
# more efficient/flexible backticks system
my $comm = shift;
my $rerr = shift;
my $rout = shift;
my $resource = shift;
my $data = shift;
my $dont_do_auth = shift;
my $buf = '';
my $undersave = $_;
my $pid;
my $args;
($comm, $args, $data) = &$stringify_args($comm, $resource,
$data, $dont_do_auth, @_);
print STDOUT "$comm\n$args\n$data\n" if ($superverbose);
if(open(BACTIX, '-|')) {
while(<BACTIX>) {
$buf .= $_;
} close(BACTIX);
$_ = $undersave;
return $buf; # and $? is still in $?
} else {
$in_backticks = 1;
&sigify(sub {
die(
"** user agent not honouring timeout (caught by sigalarm)\n");
}, qw(ALRM));
alarm 120; # this should be sufficient
if (length($rerr)) {
close(STDERR);
open(STDERR, ">$rerr");
}
if (length($rout)) {
close(STDOUT);
open(STDOUT, ">$rout");
}
if(open(FRONTIX, "|$comm")) {
print FRONTIX "$args\n";
print FRONTIX "$data" if (length($data));
close(FRONTIX);
} else {
die(
"backticks() failure for $comm $rerr $rout @_: $!\n");
}
$rv = $? >> 8;
exit $rv;
}
}
sub wherecheck {
my ($prompt, $filename, $fatal) = (@_);
my (@paths) = split(/\:/, $ENV{'PATH'});
my $setv = '';
push(@paths, '/usr/bin'); # the usual place
@paths = ('') if ($filename =~ m#^/#); # for absolute paths
foreach(@paths) {
if (-r "$_/$filename") {
$setv = "$_/$filename";
1 while $setv =~ s#//#/#;
last;
}
}
if (!length($setv)) {
die ($fatal) if ($fatal);
exit(1);
}
return $setv;
}
sub url_oauth_sub {
my $x = shift;
$x =~ s/([^-0-9a-zA-Z._~])/"%".uc(unpack("H*",$1))/eg; return $x;
}
# this is a sucky nonce generator. I was looking for an awesome nonce
# generator, and then I realized it would only be used once, so who cares?
# *rimshot*
sub generate_nonce { unpack("H32", pack("u", rand($$).$$.time())); }
sub exception {
my ($num, $tex) = (@_);
$lastexception = $num;
print STDOUT "$tex" if ($verbose);
}
sub grabjson {
my $url = shift;
my $no_auth = shift;
my $data;
chomp($data = &backticks($curl,
'/dev/null', undef, $url, undef,
$no_auth, @wind));
return &genericnetworkjson($data, $url, $no_auth);
}
sub postjson {
my $url = shift;
my $postdata = shift;
my $no_auth = shift;
my $data;
chomp($data = &backticks($curl,
'/dev/null', undef, $url, $postdata,
$no_auth, @wend));
return &genericnetworkjson($data, $url, $no_auth);
}
sub putjson {
my $url = shift;
my $postdata = shift;
my $no_auth = shift;
my $data;
chomp($data = &backticks($curl,
'/dev/null', undef, $url, $postdata,
$no_auth, @wund));
return &genericnetworkjson($data, $url, $no_auth);
}
sub genericnetworkjson {
my $data = shift;
my $url = shift;
my $no_auth = shift;
my $my_json_ref = undef; # durrr hat go on foot
my $i;
my $tdata;
my $seed;
my $k = $? >> 8;
$data =~ s/[\r\l\n\s]*$//s;
$data =~ s/^[\r\l\n\s]*//s;
if (!length($data) || $k == 28 || $k == 7 || $k == 35) {
&exception(1, "*** warning: timeout or no data\n");
return undef;
}
if ($k > 0) {
&exception(4,
"*** warning: unexpected error code ($k) from user agent\n");
return undef;
}
# handle things like 304, or other things that look like HTTP
# error codes
if ($data =~ m#^HTTP/\d\.\d\s+(\d+)\s+#) {
$code = 0+$1;
print $stdout $data if ($superverbose);
# 304 is actually a cop-out code and is not usually
# returned, so we should consider it a non-fatal error
if ($code == 304 || $code == 200 || $code == 204) {
&exception(1, "*** warning: timeout or no data\n");
return undef;
}
&exception(4,
"*** warning: unexpected HTTP return code $code from server\n");
return undef;
}
$my_json_ref = &parsejson($data);
$laststatus = 0;
return $my_json_ref;
}
$curl ||= &wherecheck("checking for cURL", "curl", <<"EOF");
cURL is required. if cURL is not usually in your path, you can
hardcode it with -curl=/path/to/curl
EOF
$vv = `$curl --version`;
($vv =~ /^curl (\d+)\.(\d+)/) && ($major = $1, $minor = $2);
die("at least cURL 7.58 required, you have ${major}.${minor}.\n\n$vv\n")
if ($major < 7 || ($major == 7 && $minor < 58));
$json_ref = &grabjson("https://raw.githubusercontent.com/mozilla-services/shavar-prod-lists/master/disconnect-blacklist.json");
die("this doesn't look like the right format\n\ncheck URL\n$url\n")
if (!defined($json_ref->{'license'}) ||
!defined($json_ref->{'categories'}) ||
!defined($json_ref->{'categories'}->{'Cryptomining'}));
select(STDOUT); $|++;
%dupedupe = ();
&emit('Cryptomining');
&emit('FingerprintingInvasive');
# considering
#&emit('Analytics');
sub emit {
my $cat = shift(@_);
foreach $a (@{ $json_ref->{'categories'}->{$cat} }) {
foreach $b (keys(%{ $a })) {
die("illegal newline: $b\n") if ($b =~ /[\r\n]/s);
print "// $b\n";
foreach $c (keys(%{ $a->{$b} })) {
next if ($c eq 'performance');
die("illegal newline: $c\n") if ($c =~ /[\r\n]/s);
print "// $c\n";
foreach $d (@{ $a->{$b}->{$c} }) {
die("illegal quote: $d\n") if ($d =~ /"/);
next if ($dupedupe{$d}++);
# whitelist (with regrets)
next if (0 ||
$d eq 'ibm.com' ||
$d eq 'godaddy.com' ||
0);
print " BLOK(\"$d\") ||\n";
print " BLOKD(\".$d\") ||\n";
}
}
}
}
}