如何清除 CGI.pm 中的系统调用

发布于 2024-12-17 08:22:02 字数 538 浏览 0 评论 0原文

我有以下 CGI 脚本:

#!/usr/bin/perl -T
use strict;
use warnings;
use CGI::Carp qw(fatalsToBrowser);
use CGI qw/:standard/;
my $query = CGI->new();
my $searchterm = param('name');

my $file = "justafile.txt";
# Begin searching terms and ignoring case
my @entries = `grep -i \"$searchterm\" $file`; # Line10
chomp @entries;
# Do something

当我执行该命令时,它会给我这个

Insecure dependency in `` while running with -T switch at /foo/cgi-bin/mycode.cgi line 10.

How line 10 can be fix?

I have the following CGI script:

#!/usr/bin/perl -T
use strict;
use warnings;
use CGI::Carp qw(fatalsToBrowser);
use CGI qw/:standard/;
my $query = CGI->new();
my $searchterm = param('name');

my $file = "justafile.txt";
# Begin searching terms and ignoring case
my @entries = `grep -i \"$searchterm\" $file`; # Line10
chomp @entries;
# Do something

When I execute the command it gives me this

Insecure dependency in `` while running with -T switch at /foo/cgi-bin/mycode.cgi line 10.

How line 10 can be fixed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

|煩躁 2024-12-24 08:22:02

污染的全部目的是确保未经检查的输入不能提供给潜在不安全的函数。

在这种情况下,您的 $searchterm 变量可能包含意外输入,这些输入可能允许攻击者在您的系统上执行任意程序。

因此,您需要:

  1. untaint 通过确保变量与预先确定的正则表达式匹配(请参阅@flesk的答案),此时 Perl 假设您知道自己在做什么,或者

  2. 不要使用反引号(根据@eugene y的答案)。

如果您使用反引号,您还应该指定 grep 命令的完整路径,这样您就不会依赖于 $PATH

The whole point of tainting is to ensure that unchecked input cannot be supplied to potentially unsafe functions.

In this case, your $searchterm variable might contain unexpected input that might allow an attacker to execute arbitrary programs on your system.

Hence, you either need to:

  1. untaint the variable by ensuring that it matches a pre-determined regexp (see @flesk's answer), at which point Perl assumes that you know what you're doing, or

  2. don't use backticks (per @eugene y's answer).

If you're using backticks you should also specify the full path to the grep command so that you're not depending on $PATH.

千纸鹤 2024-12-24 08:22:02

使用内置的 grep 函数,例如:

open my $fh, '<', $file or die $!;    
my @entries = grep /$searchterm/i, <$fh>;

Use the built-in grep function, e.g.:

open my $fh, '<', $file or die $!;    
my @entries = grep /$searchterm/i, <$fh>;
倾城月光淡如水﹏ 2024-12-24 08:22:02

我认为这里的问题是反引号运算符正在 perl 环境之外有效地执行代码,因此完全不可信,即。被污染了。

当然,您可以尝试在有问题的行之前执行类似的操作:

$ENV{"PATH"} = "";

您可能仍然会从这一行收到错误:

my $file = "justafile.txt";

要解决此问题,您可能只需给它一个绝对路径,例如:

my $file = "/home/blah/justafile.txt";

您几乎肯定必须给您也使用反引号运算符执行的 grep 命令的绝对路径,因为清除环境变量将丢失路径。换句话说,

# Begin searching terms and ignoring case
my @entries = `/bin/grep -i \"$searchterm\" $file`; # Line10

您可能还想在清除 $ENV 的值之前复制它,以防万一您以后需要它......

希望其中一些有所帮助!

I think the problem here is that the backtick operator is effectively executing code outside of the perl environment, and is therefore quite rightly not trusted, ie. tainted.

You could, of course, try doing something like this before the offending line:

$ENV{"PATH"} = "";

You will probably still get an error from this line:

my $file = "justafile.txt";

To fix that, you could probably just give it an absolute path, eg:

my $file = "/home/blah/justafile.txt";

You would almost certainly have to give an absolute path to the grep command you are executing with the backtick operator too, as clearing the environment variables will loose the path. In other words do:

# Begin searching terms and ignoring case
my @entries = `/bin/grep -i \"$searchterm\" $file`; # Line10

You might also want to copy the value of $ENV before you clear it, just in case you need it later...

Hope some of this helps!

七颜 2024-12-24 08:22:02

-T 开关仅警告您可能存在污染的输入:http: //perldoc.perl.org/perlsec.html#Taint-mode

你需要自己去污染它,例如使用

my $safe_searchterm = "";
$safe_searchterm .= $_ for $searchterm =~ /\w+/g;

这不是一个非常复杂的测试,而且可能也不太安全,除非你完全控制什么\w 匹配。

编辑:更改了我的最小解决方案以反映下面评论中给出的信息。

The -T switch only warns you about possible tainted input: http://perldoc.perl.org/perlsec.html#Taint-mode

You need to untaint it yourself, for example using

my $safe_searchterm = "";
$safe_searchterm .= $_ for $searchterm =~ /\w+/g;

That's not a very sophisticated test though, and possibly not too safe either, unless you have complete control over what \w matches.

EDIT: Changed my minimal solution to reflect the info given in the comments below.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文