From 909b610fe3cfad7b45a66b83918103557b9cb122 Mon Sep 17 00:00:00 2001 From: Acts1631 Date: Wed, 29 Jul 2026 20:46:10 -0400 Subject: [PATCH] Avoid shell execution in multi.pl rules processing Rules-file fields were interpolated into a one-string system call, allowing shell metacharacters to run as the invoking user. Replace the shell pipeline with an explicit zcat child and list-form ngrep invocation. Validate rule names before using them in output paths. --- scripts/multi.pl | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/scripts/multi.pl b/scripts/multi.pl index 230e0f0..5bbbf9a 100755 --- a/scripts/multi.pl +++ b/scripts/multi.pl @@ -67,16 +67,30 @@ sub go { } elsif (not $daddy) { - my(@args); + my($zcat); + my($ngrep_status); $SIG{INT} = 'IGNORE'; sigprocmask(SIG_UNBLOCK, $old_sig_set); - system("zcat $rule{'file'} | " . - "ngrep -qtI - $rule{'regex'} $rule{'filter'} 2>&1 > " . - "ngrepped.$rule_name"); - - exit; + # Rule fields are untrusted. Keep them out of a shell command. + $zcat = open(ZCAT, "-|"); + if (not defined $zcat) { + warn "[$rule_name] zcat fork() error: $!\n"; + exit 1; + } elsif (not $zcat) { + exec "zcat", "--", $rule{'file'}; + warn "[$rule_name] zcat exec() error: $!\n"; + exit 1; + } + + open(STDIN, "<&ZCAT") || die "[$rule_name] couldn't read zcat output: $!\n"; + open(STDOUT, ">ngrepped.$rule_name") || die "[$rule_name] couldn't open output: $!\n"; + $ngrep_status = system("ngrep", "-qtI", "-", $rule{'regex'}, $rule{'filter'}); + close(ZCAT); + waitpid($zcat, 0); + + exit($ngrep_status == -1 ? 1 : $ngrep_status >> 8); } else { @@ -105,10 +119,18 @@ sub go { die "Rules file $rules_file empty, exiting.\n"; } -%rules = map { chomp(local(@fields) = split / /, $_); - $fields[0] => { "file" => $fields[1], - "regex" => $fields[2], - "filter" => $fields[3] }; } @lines; +foreach (@lines) { + chomp; + my(@fields) = split /\s+/, $_, 4; + + die "Malformed rule: $_\n" unless scalar(@fields) == 4; + die "Invalid rule name: $fields[0]\n" + unless $fields[0] =~ /^[A-Za-z0-9][A-Za-z0-9_.-]*$/; + + $rules{$fields[0]} = { "file" => $fields[1], + "regex" => $fields[2], + "filter" => $fields[3] }; +} @rules = keys %rules; print "Hi, I'm ngrepper, and here we go.\n"; @@ -126,4 +148,3 @@ sub go { exit; -