我相信这个Perl脚本是安全的.可以改进吗?

Joe*_*Joe 1 regex security perl

我有以下Perl脚本.人们可以(现在不是因为我把它拿下来)ping一个像这样的URL

http://www.joereddington.com/testsound/getsound.pl?text=hello%20mum
Run Code Online (Sandbox Code Playgroud)

然后找到该文件

http://www.joereddington.com/testsound/hope.wav
Run Code Online (Sandbox Code Playgroud)

是一个电脑声音的录音说"你好妈妈".

#!/usr/bin/perl
use strict;
use warnings;

use CGI qw(:standard -debug);

my $text = param('text');
$text =~ s/[^0-9a-zA-Z\s]//g;

print "Content-type: text/html\n\n";

system("/home8/projedf4/tts/espeak-1.48.04-source/src/speak \"$text\" -w hope.wav");
Run Code Online (Sandbox Code Playgroud)

让用户可能利用注入攻击等等,我有点紧张.我相信我已经做好了这条线

$text =~ s/[^0-9a-zA-Z\s]//g; 
Run Code Online (Sandbox Code Playgroud)

因为我只是简单地从字符串中提取所有可能造成损害的东西.

但这够了吗?我甚至可以走得那么远

$text =~ s/[^0-9a-zA-Z\s\.,]//g;
Run Code Online (Sandbox Code Playgroud)

ike*_*ami 6

是啊,你的代码是罚款(忽略错误perl,speak,DOS攻击等),假设特殊的唯一论点speak开始-.

但它可以改善.

  1. 没有必要删除这么多字符.

    sub shell_quote {
       return map {
          die if /\x00/;
          my $lit = $_;
          $lit =~ s/'/'\\''/g;
          "'$lit'"              
       } @_;
    }
    
    $text =~ s/^-+//;
    system(shell_quote('/.../speak', $text, '-w', 'hope.wav'));
    
    Run Code Online (Sandbox Code Playgroud)

    要么

    use String::ShellQuote qw( shell_quote );
    
    $text =~ s/^-+//;
    system(shell_quote('/.../speak', $text, '-w', 'hope.wav'));
    
    Run Code Online (Sandbox Code Playgroud)
  2. 也没有必要启动shell.

    die if $text =~ /\x00/;
    $text =~ s/^-+//;
    system('/.../speak', $text, '-w', 'hope.wav');
    
    Run Code Online (Sandbox Code Playgroud)
  3. 如果你的speak支持--,你甚至可以使用

    die if $text =~ /\x00/;
    system('/.../speak', '-w', 'hope.wav', '--', $text);
    
    Run Code Online (Sandbox Code Playgroud)

  • @Joe:`"\ x00"`是NUL角色.这是一种威胁,因为标准C代码将其视为字符串终止符 (2认同)