Debian Codegraph @

o-saft.cgi

raw
1 #!/usr/bin/perl
2
3 #!# Copyright (c) 2022 Achim Hoffmann
4 #!# This software is licensed under GPLv2. Please see o-saft.pl for details.
5
6 ## no critic qw(RegularExpressions::ProhibitComplexRegexes)
7 ## no critic qw(RegularExpressions::RequireExtendedFormatting)
8 # because we use /x as needed for human readability (may change in future)
9 ## no critic qw(RegularExpressions::RequireLineBoundaryMatching)
10 # hmm, we're matching only on line here (Severity: 2)
11 ## no critic qw(RegularExpressions::RequireDotMatchAnything)
12 # hmm, we're matching only on line here (Severity: 2)
13 ## no critic qw(Variables::ProhibitPunctuationVars)
14 # we make regular use of these variables as we know them;-) (Severity: 2)
15 ## no critic qw(ControlStructures::ProhibitPostfixControls)
16 # we believe that postfix control make some code more readable (Severity: 2)
17
18 =pod
19
20 =head1 NAME
21
22 o-saft.cgi - wrapper script to start o-saft.pl as CGI script
23
24 =head1 DESCRIPTION
25
26 Calls o-saft.pl if first parameter is I<--cgi> and prints its result
27 as plain text. The result is prefixed with proper HTTP headers.
28
29 Some parameters are silently ignored (removed from argument list), i.e:
30
31 --dump --exec --list --libversion --version --trace* --v --ca*
32
33 Some lazy checks according parameters are done, exits silently if any of
34 following will be found:
35
36 =over 4
37
38 =item * not allowed characters in parameters, allowed are:
39
40 a-zA-Z0-9,.:_&\!\/=\+-
41
42 =item * not allowed options:
43
44 --env* --exe* --lib* --call* --openssl*
45
46 =item * illegal hostnames or IPs:
47
48 localhost, *.local, (0|10|127|169|172|192|224|240|255).X.X.X
49
50 =item * any IP notation other than "dotted decimal" are illegal:
51
52 * https://0127.00.000.01/ - octal IP address
53
54 * https://0x7f000001/ - hexadecimal IP address
55
56 * https://2130706433/ - integer or DWORD IP address
57
58 * https://0x0b.026.8492/ - any mixed notation
59
60
61 =item * any IPv6 addresses in URLs
62
63 =item * any octal (prefix 0) or hex (prefix 0x) notation in IP addresses:
64
65 0x0b.026.8492
66
67 =back
68
69 To get a list of RegEx for invalid parameters, please use:
70
71 grep qr/ o-saft.cgi
72
73 where $key is --(host|url)=
74
75 =head1 OPTIONS, PARAMETERS
76
77 =over 4
78
79 =item --cgi
80
81 Must be used as first parameter, otherwise dies.
82
83 =item --format=html
84
85 Sends HTTP header:
86
87 Content-type: text/html;charset=utf-8
88
89 and convert output to HTML format using contrib/HTML-table.awk .
90
91 =item --content-type=html
92
93 Forces to sends HTTP header:
94
95 Content-type: text/html;charset=utf-8
96
97 =item --cgi-no-header
98
99 Omit all HTTP headers (useful if headers are added by web server).
100
101 =back
102
103 =head1 DEBUG
104
105 If the environment variable I<OSAFT_CGI_TEST> is set, detailed error
106 messages are printed. In particular, it prints the RegEx matching any of
107 the dangerous parameters (i.e. hostname or IP).
108
109 This is only useful for testing on command line. It's not intended to be
110 used when run as a CGI script in a web server.
111
112 As it is not possible to set environment variables for the CGI script by
113 the client (browser), the code should be safe.
114
115 =head1 EXAMPLES
116
117 Call as CGI from command line:
118
119 env QUERY_STRING='--cgi&--host=demo.tld&--cmd=cn' o-saft.cgi
120
121 For testing only, call from command line:
122
123 o-saft.cgi --cgi --host=demo.tld --cmd=cn
124
125 For debugging only, call from command line:
126
127 env OSAFT_CGI_TEST=1 --cgi --host=localhost --cmd=cn o-saft.cgi
128
129 =head1 SEE ALSO
130
131 =head2 L<o-saft.pl(1)|o-saft.pl(1)>
132
133 =head1 AUTHOR
134
135 12-sep-12 Achim Hoffmann
136
137 =cut
138
139 use strict;
140 use warnings;
141
142 my $SID_cgi = "@(#) o-saft.cgi 1.67 22/10/27 22:39:21";
143 my $VERSION = '22.06.22';
144 my $me = $0; $me =~ s#.*/##;
145 my $mepath = $0; $mepath =~ s#/[^/\\]*$##;
146 $mepath = './' if ($mepath eq $me);
147 my $header = 1;
148 local $| = 1; # don't buffer, synchronize STDERR and STDOUT
149
150 ##############################################################################
151 my $osaft = "$mepath/o-saft.pl";
152 # $osaft = '/bin/o-saft/o-saft.pl'; # <== adapt as needed
153 my $openssl = '/usr/local/openssl/bin/openssl'; # <== adapt as needed
154 # NOTE tainted perl (-T) will complain if the path given in $osaft
155 # or $openssl is writable; it also must be an absolute path
156 ##############################################################################
157
158 my @argv = @ARGV;
159
160 sub _print_if_test {
161 #? print text if environment variable OSAFT_CGI_TEST is set
162 local $\ = "\n";
163 print @_ if (defined $ENV{'OSAFT_CGI_TEST'});
164 return;
165 } # _print_if_test
166
167 sub _warn_and_exit {
168 #? print error and exit
169 my $txt = shift;
170 _print_if_test "**ERROR: $txt";
171 # ####################################################################
172 #
173 # This function should print an empty string and exit with status 0 in
174 # production environments.
175 # Printing above detailed error message is safe, see POD above.
176 #
177 # ####################################################################
178 print "";
179 exit 0;
180 } # _warn_and_exit
181
182 if (not $ENV{'QUERY_STRING'}) {
183 print "**WARNING: test mode: restart using args as value in QUERY_STRING\n";
184 _warn_and_exit "call without parameters" if (1 > $#argv);
185 # may be a command line call without QUERY_STRING environment variable
186 # call myself with QUERY_STRING to simulate a call from CGI
187 # NOTE: this produces output before any HTTP header, which results in a
188 # Server 500 error in the web server; that's ok here for testing
189 ## no critic qw(Variables::RequireLocalizedPunctuationVars)
190 $ENV{'QUERY_STRING'} = join('&', @argv);
191 $ENV{'QUERY_STRING'} =~ s/[+]/%2b/g;
192 $ENV{'QUERY_STRING'} =~ s/[ ]/%20/g;
193 exec $0;
194 }
195
196 if ($me =~/\.cgi$/) {
197 # CGI mode is pretty simple:
198 # * URL-decode once: QUERY_STRING and POST data
199 # * check if data contains suspicious characters, die if so
200 # NOTE that % is suspicious as we decode only once
201 # * check if target is suspicious host or net, die if so
202 # * then split data at & to get our options and arguments
203 # * ready we go by calling $osaft
204 # NOTE: in true CGI-mode, QUERY_STRING just contains the form fields,
205 # when used with our own osaft: schema, the QUERY_STRING also
206 # contains the schema and path, i.e. osaft:///o-saft.cgi?
207 # NOTE: for debugging using system() writing to a file is better than
208 # using perl's print() as it may break the HTTP response
209 my $cgi = 0;
210 my $typ = 'plain';
211 my $qs = '';
212 $qs = $ENV{'QUERY_STRING'} if (defined $ENV{'QUERY_STRING'});
213 #dbx# system "echo 'QS=$qs #' >> /tmp/osaft.cgi.log";
214 $qs =~ s/^"?(.*?)"?$/$1/; # remove enclosing " (windows problem)
215 $qs =~ s#^o-?saft://##g; # remove schema if any (used in o-saft.cgi.html)
216 $qs =~ s#^[^?]*\?##g; # remove path left of ? if any (used in o-saft.cgi.html)
217 $qs =~ s/[+]/ /g;
218 $qs =~ s/(?:%([0-9a-f]{2,2}))/pack("H2", $1)/egi; # url-decode once
219 undef @argv;
220 push(@argv, split(/[&?]/, $qs));
221 #dbx# system "echo 'argv=@argv' >> /tmp/osaft.cgi.log";
222
223 $cgi = shift @argv || ''; # remove first argument, which must be --cgi
224 # || '' avoids uninitialized value
225 push(@argv, "--cgi-exec"); # argument required for some more checks
226 die "**ERROR: CGI mode requires strict settings\n" if ($cgi !~ /^--cgi=?$/);
227
228 # TODO: check if following RegEx need $ at end
229 $typ = 'html' if ($qs =~ m/--format=html/); # --format=html already in @argv
230 $header = 1 if (0 < (grep{/--cgi.?header/} $qs));
231 $header = 0 if (0 < (grep{/--cgi.?no.?header/} $qs));
232 $header = 0 if (0 < (grep{/--no.?cgi.?header/} $qs));
233 if (0 < $header) {
234 my $_typ = $typ; # check if force using text/html
235 $_typ = 'html' if ($qs =~ m/--content-type=html/);
236 print "X-Cite: Perl is a mess. But that's okay, because the problem space is also a mess. Larry Wall\r\n";
237 print "X-O-Saft: OWASP – SSL advanced forensic tool 1.67\r\n";
238 print "Content-type: text/$_typ; charset=utf-8\r\n";# for --usr* only
239 print "\r\n";
240 }
241
242 _print_if_test "**WARNING: test mode: die with detailed messages on errors";
243
244 if (defined $ENV{'REQUEST_METHOD'}) { # ToDo: NOT WORKING
245 $qs .= <> if ($ENV{'REQUEST_METHOD'} eq 'POST');# add to GET data
246 }
247
248 # check for potential dangerous commands and options, simply ignore
249 # (just remove) them; examples:
250 # --cgi&--cmd=dump # ignore
251 # --cgi&--cmd=+dump # ignore
252 # --cgi&--url=+dump # ignore
253 # --cgi&--trace= # ignore
254 # --cgi&--opt=--trace= # ignore
255 # --cgi&--unknown=--v= # ignore
256 # also fix trailing =
257 my $ignore = qr/
258 ^--(?:
259 # illegal commands and options
260 (?:cmd|url)=[+]?(?:dump|exec|list|libversion|version)
261 |(?:cmd|url)=--(?:trace|--v) # illegal options given as URL
262 # illegal options
263 |trace|v # options to verbose output
264 |ca[._-]?(?:file|path)|rc= # may be used to enumerate paths
265 )|=(?:
266 # illegal commands as parameter value
267 [+]?(?:dump|exec|list|libversion|version)
268 # illegal options as parameter value
269 |--(?:trace|--v)
270 |--ca[._-]?(?:file|path)|rc=
271 )/xi;
272 # o-saft.pl splits key=value arguments at = , if 'key' is an
273 # unknown option then 'value' turns into a valid command or
274 # option argument; see o-saft.pl's argument parser
275 #dbx# system "echo 'argv=@argv' >> /tmp/osaft.cgi.log";
276 my @save_argv;
277 foreach my $arg (@argv) {
278 #dbx# print "#dbx: $arg # silently ignored\n" if ($arg =~ m#$ignore#);
279 next if ($arg =~ m#$ignore#);
280 # quick&dirty fix generated parameters also:
281 # in o-saft.cgi.html there may be parameter names like:
282 # --lagacy=owasp
283 # as these are input tags with type checkbox, the value is
284 # empty, hence the parameter passed in is like:
285 # --lagacy=owasp=
286 # because the input tag's value is empty; this would result
287 # in passing the value owasp= instead of owasp for the
288 # the paramter name legacy ; the trailing = is removed
289 $arg =~ s#=$##; # remove trailing = in key=value
290 push(@save_argv, $arg);
291 }
292 _print_if_test "**ARGS_in: @argv";
293 _print_if_test "**ARGSuse: @save_argv";
294 @argv = @save_argv;
295 #dbx# system "echo 'argv=@argv' >> /tmp/osaft.cgi.log";
296
297 # check for suspicious characters and targets, die if any
298 # Matches against QUERY_STRING (in $qs), which still contains the
299 # usual separator & . The first parameter in $qs must be --cgi, all
300 # others must be prefixed with & . Hence most pattern start with &
301 # to avoid matches inside a valid parameter, for example:
302 # --cgi&--host=a42&--cmd=cn # ok
303 # --cgi&--host=42&--cmd=cn # bad
304 # --cgi&--cmd=cn&--host=42 # bad
305 # --cgi&--cmd=cn&=42 # bad
306 # --cgi&--cmd=cn&--other-opt=42 # ok
307 # FIXME: last example will be detected as malicious and dies, this is
308 # a false positive, bug here
309 # NOTE: Technically & may be a ? too, it is not really RFC compliant,
310 # but possible. Someone may sends malicious data.
311 my $err = 0;
312 my $key = '&--(?:host|url)=';
313 foreach my $dangerous (
314 qr/[^a-zA-Z0-9,.:_&\!\/=+-]/i,
315 # dangerous characters anywhere
316 # above whitelist for allowed characters!
317 # FIXME: this blocks also valid IPv6 in URL because of [ and/or ]
318
319 qr/&--(?:env|exe|lib|call|openssl)/i,
320 qr/=--(?:env|exe|lib|call|openssl)/i, # see comment for $ignore above
321 # dangerous commands and options
322
323 # RFC addresses are not allowed, see https://tools.ietf.org/html/rfc5735
324 # 0.0.0.0/8 This Network
325 # 10.0.0.0/8 Private-Use Networks # 10.0.0.0 .. 10.255.255.255
326 # 100.64.0.0/10 CGN - Carrier- Grade NAT # 100.64.0.0 .. 100.127.255.255
327 # 127.0.0.0/8 Loopback # 127.0.0.0 .. 127.255.255.255
328 # 169.254.0.0/16 Link local # 169.254.0.0 .. 169.254.255.255
329 # 172.16.0.0/12 Private-Use Networks # 172.16.0.0 .. 172.31.255.255
330 # 192.0.0.0/24 IETF Protocol Assignments # 192.0.0.0 .. 192.0.0.255
331 # 192.0.2.0/24 TEST-NET-1 # 192.0.2.0 .. 192.0.2.255
332 # 192.88.99.0/24 6to4 Relay Anycast # 192.88.99.0 .. 192.88.99.255
333 # 192.168.0.0/16 Private-Use Networks # 192.168.0.0 .. 192.168.255.255
334 # 198.18.0.0/15 Network Interconnect,
335 # Device Benchmark Testing # 198.18.0.0 .. 198.19.255.255
336 # 198.51.100.0/24 TEST-NET-2 # 198.51.100.0 .. 198.51.100.255
337 # 203.0.13.0/24 TEST-NET-3 # 203.0.13.0 .. 203.0.13.255
338 # 224.0.0.0/4 Multicast # 224.0.0.0 .. 239.255.255.255
339 # # https://www.iana.org/assignments/multicast-addresses/multicast-addresses.xhtml
340 # 240.0.0.0/4 Reserved for future use # 240.0.0.0 .. 255.255.255.255
341 # 255.255.255.255/32 Limited Broadcast
342
343 # fe80: IPv6 link local
344 # fe[c-f][0-9a-f]: IPv6 site local
345 # ff0[0-9a-f]|f[c-d][0-9a-f][0-9a-f]: IPv6 multicast or unique local unicast (RFC6762)
346 # 64:::IP IPv4-mapped IPv6 addresses as NAT64 (RFC6052): 64:ff9b::192.0.2.128
347 # ::::IP IPv4-mapped IPv6 addresses: ::ffff:192.0.2.128
348 # 127.1 127.0.1 IPv4 abbreviated
349 # TODO: better, more accuarte checks
350 # ::1/128 localhost
351 # fe80::/64 link local
352 # ff00::/8 ULA - Unique Local Address
353 # ff00::0/8 Multicast
354 # fd00::/8 Unique Local Address, not routable
355 # fc00::/7 ? Global Unique Address
356 # ::ffff:0:0/96 IPv4 mapped addresses
357 # ::ffff:0:0:0/96 IPv4 translated addresses (SIIT protocol)
358 # 64:ff9b::/96 6to4 addressing
359 # 2000::/3
360 # 2001::/16 GUA - Global Unique Address, routable!
361 # 2001::/32 Teredo tunneling (RFC 4380)
362 # 2001:2:::/48 Reserved for Benchmarking Methodology Working Group
363 # 2001:20::/28 ORCHIDv2 crypto hash identifiers, not routable
364 # 2001:db8::/32 ? Documentation
365 # ff02::2 Neighbor Discovery Protocol
366
367 # match IPv4: ((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}
368 # match IPv6: ([0-9a-f]{0,4}:){1,8}
369 # match 1234567890: IP as integer not yet allowed
370 # match IPv4: less than 4 parts for dotted IP
371
372 # TODO: build a map of integer ranges for IPs to be denied:
373 # 10.x.x.x = 167772161 .. 184549375
374 # 127.x.x.x = 2130706433 .. 2147483647
375 # ...
376 # then check given host againts this map if conversion to
377 # an integer is succesful.
378 # It will also work for mixed IPv6-IPv4 IPs like:
379 # ::ffff:127.0.0.1 which is an alias for ::ffff:7f00:1.
380 # This should eliminate some of the restriction (missing)
381 # of RegEx (see NOTEs below).
382 # Unfortunately Math::BigInt is required (breaks usage on
383 # ancient systems).
384
385 # hex IP addresses may look like:
386 # 127.0xb.0.1 127.0x00000b.0.1
387
388 # octal IP addresses may look like:
389 # 0127.000000002.0.1
390 # i.g. each octet is prefixed with 0, followed by any amount of
391 # 0, followed by [1-4] (may be missing), followed by [0-7][0-7]
392 # NOTE: octal addresses are not bad in general, but the checks
393 # below expect only CIDR numbers in decimal notation, hence
394 # any occourance of octal numbers are rejected
395
396 # NOTE: according following RegExs
397 # - grouping with back reference is used insted of (?: ... )
398 # sometimes, this is because : is used literally in RegExs
399 # - RegExs are not case sensitive to match FQDN and (hex) IP,
400 # but this also allows --URL= --HOST= (which is ok)
401 # - sequence of following RegExs is important, more specific
402 # ones first
403 # - the leeading option like --host= is optional as the word to
404 # be checked may be passed without key, something like:
405 # --cgi&--host=good.FQDN&localhost&--enabled=
406 # - IPv4 matching is lazy with [0-9]+
407
408 qr/(?:&(localhost|10|127|224(.[0-9]){1,3}|(ffff)?::1|(ffff:)?7f00:1)(&|$))/i,
409 # first match bare hostname argument without --host=
410 # this avoids false positive matches in more lazy RegEx
411 # FIXME: probably necessary for all following RegEx
412 # NOTE: also bad 127.666 (= 127.0.2.154)
413
414 qr/(?:(?:$key)?((?:0?127|0x0?7f).[0-9afx.]+))/i,
415 # any 127.*
416
417 qr/(?:(?:$key)?[0-9.]*(?:(0+[1-4]?[0-7]{1,2}[.])|([.]0+[1-4]?[0-7]{1,2})))/,
418 # octal addresses are always ignored
419
420 qr/(?:(?:$key)?[0-9x.]*(?:(0x0*[0-9af]{1,2}[.])|([.]0x0*[0-9af]{1,2})))/i,
421 # hex addresses are always ignored
422
423 qr/(?:(?:$key)?((10|224).[0-9]+(.[0-9]{1,3})?))/i,
424 # abbreviated IPv4: 10.1 10.41.1 10.0.1 224.1
425
426 qr/(?:(?:$key)(localhost|::1|ffff::1|(ffff:)?7f00:1)(&|$))/i,
427 # localhost
428 # TODO: IPv6 localhost: [7f00:1] .. [7fff:ffff]
429
430 qr/(?:(?:$key)?((ffff:)?(100\.64|169.254|172\.(1[6-9]|2\d|3[01])|192\.168|198\.18)\.[\d]+.[\d]+))/i,
431 # common Class B RFC networks for private use
432 # TODO: to pedantic: 100.64.0.0/10 CGN is not really class B
433
434 qr/(?:(?:$key)?((ffff:)?(192\.0\.[02]|192.88\.99|198\.51\.100|203\.0\.13)\.[\d]+))/i,
435 # common class C RFC networks for private use
436
437 qr/(?:(?:$key)?((ffff:)?(0|10|22[4-9]|23[0-9]|24[0-9]|25[0-5])\.[\d]+.[\d]+.[\d]+))/i,
438 # loopback, mulicast
439
440 qr/(?:(?:$key)?((fe80|fe[c-f][0-9a-f]:)))/i,
441 # IPv6 link local or site local
442
443 qr/(?:(?:$key)?((ff0[0-9a-f]|f[c-d][0-9a-f][0-9a-f]:)))/i,
444 # IPv6 multicast or unique local unicast (RFC6762)
445
446 qr/(?:(?:$key)?64:([0-9a-f]{1,4}:){1,2}:(&|$))/i,
447 # any IPv4-mapped IPv6 addresses as NAT64 (RFC6052): 64:ff9b::
448
449 qr/(?:(?:$key)?64:([0-9a-f]{1,4}:){1,2}:((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4})/i,
450 # any IPv4-mapped IPv6 addresses as NAT64 (RFC6052): 64:ff9b::192.0.2.128
451 # NOTE: would also be matched by next more general RegEx
452
453 qr/(?:(?:$key)?([0-9a-f]{0,4}:){1,3}((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4})/i,
454 # any IPv4-mapped IPv6 addresses: ::ffff:192.0.2.128
455 # NOTE: ([0-9a-f]{0,4}:){1,3} is lazy, matches also ffff:IP or :IP
456
457 qr/(?:&[0-9]+(&|$))/i,
458 # just 11111; does not match +11111 or -11111 or --host=11111
459 qr/(?:(?:$key)[0-9]+(&|$))/i,
460 # just --host=11111
461 # NOTE: in general not bad, but needs to be mapped to
462 # allowed IPv4 or IPv6 which is not that simple
463 # FIXME: i.e. valid 3221225473 = 192.0.0.1 is denied
464
465 qr/(?:(?:$key)?.*?\.local(&|$))/i,
466 # multicast domain .local (RFC6762)
467
468 qr{(?:(?:$key)?([a-z0-9:]+)?(//)?\[?([a-f0-9:]+)])}i,
469 # IPv6
470 # NOTE: final ] not escaped, it's a literal character here!
471 # FIXME: blocks any IPv6
472 # TODO: IPv6 still experimental
473 # possible formats to be blocked:
474 # ftp://[::ffff:7f00:1]/path
475 # //[::ffff:7f00:1]/path
476 # [::ffff:7f00:1]/path
477 # ::ffff:7f00:1/path # illegal, but possible
478 # HTTPS://[::ffff:7f00:1]:80/path
479 # any:[::ffff:7f00:1]/path # also matched
480 # FIXME: also blocks FQDN:port like cafe:4711/path
481
482 ) {
483 if ($qs =~ m#$dangerous#) {
484 _print_if_test "**ERROR: $qs";
485 _print_if_test "**ERROR: $dangerous";
486 $err++;
487 }
488 }
489 _warn_and_exit "dangerous parameters; aborted" if 0 < $err;
490
491 # prepare execution environment
492 local $ENV{LD_LIBRARY_PATH} = "$openssl/lib/";
493 local $ENV{PATH} = "$openssl/bin/";
494 $ENV{PATH} .= ':' . $ENV{PATH} if (defined $ENV{PATH}); # defensive programming
495 local $| = 1; # don't buffer, synchronize STDERR and STDOUT
496
497 # start $osaft
498 #dbx# system "$osaft @argv >> /tmp/osaft.cgi.log";
499 _print_if_test "$osaft @argv";
500 if ('html' eq $typ) {
501 # 11/2021 ah: experimental: generate HTML output
502 # need to use system, as exec can't pipe
503 my $cmd = join(" ", $osaft, @argv);
504 #dbx# print "# mepath=$mepath\n";
505 #dbx# print "# system($cmd | /usr/bin/gawk -f $mepath/contrib/HTML-simple.awk)\n";
506 system("$cmd | /usr/bin/gawk -f $mepath/contrib/HTML-table.awk");
507 exit;
508 }
509 exec $osaft, @argv; # exec is ok, as we call ourself only
510 # TODO: Win32 not tested: exec 'perl.exe', $osaft, @argv;
511 }
512 exit 0; # never reached