【问题标题】:How can I parse and echo CGI query string parameters?如何解析和回显 CGI 查询字符串参数?
【发布时间】:2011-01-15 07:58:04
【问题描述】:

我想使用 URL 中的系统请求参数在服务器上执行命令。我正在运行服务器。

在浏览器中 ->> //localhost:9009/?comd 显示目录中的文件列表,正如我在代码中放置的那样

if (/comd/i )        { print  $client `dir`;      }

如何解析请求参数?例如:

@987654321@<b>&amp;user=kkc&amp;mail=kkc@kkc.com</b>

我想回来 -->> hello <b>user</b> please confirm your email <b>kkc@kkc.com</b>

如何解析url中的请求参数?

【问题讨论】:

    标签: perl cgi


    【解决方案1】:
    
    use CGI;
    
    my $cgi = CGI->new();
    
    my $user = $cgi->param( "user" );
    my $mail = $cgi->param( "mail" );
    
    print( "Hello $user please confirm your email $mail\n" );
    

    【讨论】:

    • 您没有返回 Content-Type 标头,也没有验证用户可能发送的输入。不验证输入是在自找麻烦。
    • 嗯,很好的 XSS 漏洞。
    • 问题是关于如何解析查询字符串,而不是如何防止黑客攻击。如果我们每次都必须为每个问题编写防止攻击所需的所有代码,那么这个网站将毫无用处。
    • @Htbaa 我曾与一名承包商共事,该承包商在他的简历中声称自己是伦敦一家大型城市银行的 Perl“主题专家”。他从未听说过污染模式。如果您认真对待安全性,我希望您的第一条评论会提到污染模式。不过我相信你知道我回答的问题很简洁。
    【解决方案2】:
    #!/usr/bin/perl
    $|++;                              # auto flush output
    
    use CGI;                           # module that simplifies grabbing parameters from query string
    use HTML::Entities;                # for character encoding
    use strict;                        # to make sure your Perl code is well formed
    
    print qq{Content-type: text/html\n\n};  # http header b/c outputting to browser
    
       main();                         # calling the function
    
       sub main{
          my $cgi    = new CGI;        # store all the cgi variables
          # the following stores all the query string variables into a hash
          #   this is unique because you might have multiple query string values for
          #   for a variable. 
          #   (eg http://localhost/?email=a@b.com&email=b@c.com&email=c@d.com )
          my %params = map { $_ => HTML::Entities::encode(join("; ", split("\0", $cgi->Vars->{$_}))) } $cgi->param;
    
          #Input: http://localhost:9009/?comd&user=kkc&mail=kkc@kkc.com
          #Output: Hello kcc please confirm your email kkc@kkc.com
    
          print <<HTML;
          <html>
             <head>
                <style type="text/css">.bold{font-weight:bold}</style>
             </head>
             <body>
                Hello <span class="bold">$params{user}</span> please confirm your email <span class="bold">$params{mail}</span>
             </body>
          </html>
    HTML
    
          return 1;
       }
    

    我希望这会有所帮助。

    【讨论】:

    • 在 4 个月前给出的答案上查看 cmets 关于 XSS 的内容。
    • 您对此投反对票?这是一个例子,并不意味着是最安全的事情。您的评论被视为噪音。
    • 添加了一些编码,只是为了绕过你的 XSS 废话。
    • 明确注释的代码回答了“查询字符串”的问题,值得投票。
    【解决方案3】:
    use CGI;
    use HTML::Template;
    
    my $cgi = CGI->new;
    
    my $html = qq{
        <!DOCTYPE HTML>
        <html>
        <head><title>Confirmation</title></head>
        <body><p>Hello <TMPL_VAR USER ESCAPE=HTML>. 
        Please confirm your email <TMPL_VAR MAIL ESCAPE=HTML></p></body>
        </html>
    };
    
    my $tmpl = HTML::Template->new(scalarref => \$html, associate => $cgi);
    print $cgi->header('text/html'), $tmpl->output;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多