0

我有一个接受 GET 和 POST 的 CGI 服务器端脚本,带有登录参数。我想对其进行测试以确保它不易受到攻击。所以计划是使用Perl LWP,在GET和POST中发送登录参数,比较结果。界面已更改,因此只有在 POST 中我们才能在会话 cookie 中发送用户名和密码(不确定这是否是个好主意),那么我该如何测试呢?这是我到目前为止所拥有的:

#!/usr/bin/perl
use LWP;
print "This is libwww-perl-$LWP::VERSION\n";
# Create a user agent object
  use LWP::UserAgent;
  my $ua = LWP::UserAgent->new;
  $ua->agent("MyApp/0.1 ");

  # Create a request
  #my $req = HTTP::Request->new(POST => 'http://search.cpan.org/search');
  #my $req = HTTP::Request->new(GET => 'https://qa.co.net:443/cgi-bin/n-cu.cgi');
  my $req = HTTP::Request->new(GET => 'https://qa.co.net:443/cgi-bin/n-cu.cgi?mode=frameset&JScript=1&remote_user&login=foo&password=foo HTTP/1.1');
  $req->content_type('application/x-www-form-urlencoded');
  $req->content('query=libwww-perl&mode=dist');
  # Pass request to the user agent and get a response back
  my $res = $ua->request($req);

  # Check the outcome of the response
  if ($res->is_success) {
      print $res->content;
      #print $res->code;
      #print $res->message;
  }
  else {
      print $res->status_line, "\n";
  }

这不会这样做,因为它没有会话 cookie 的东西。但可能是一个好的开始。这是测试 GET 和 POST 的正确方法吗?

以下是在 cgi 中实现的内容:

#cr_login for POST && login for GET -- leave GET param as it used to be.
     if ($m eq 'GET' && defined($req->param('login'))) {
         $msg = 'parameter "login" is invalid for this request type.';
+        my $seclog = $event_logging_directory . '/invalid_request.log';
+        open(S, ">>$seclog") or die $!;
+        my $logmsg = sprintf("%4d-%02d-%02d %02d:%02d:%02d",Today_and_Now())
+            . "|mode:" . $req->param('mode')
+            . "|login:" . $req->param('login')
+            . "|remote_addr:" . $ENV{REMOTE_ADDR}
+            . "|$msg\n";
+        print S $logmsg;

和 :

POST request to n-cu.cgi should use parameter "cr_login". If the parameter "login" is passed in a post request, it should throw error and return to login screen.

GET request to n-cu.cgi should use the parameter "login". If the parameter "cr_login" is passed in a post request, it should throw error and return to login screen.

所以我们是这样做的:

  • 保持会话 cookie 和上下文活动:

    我的 $browser = LWP::UserAgent->new(keep_alive => 10); $browser->cookie_jar( {} ); $browser->agent('Mozilla/8.0'); #$browser->ssl_opts({ verify_hostname => 0 }); $browser->show_progress(1);

及以后:打印响应

print "Cookies:\n", Dumper($browser->cookie_jar()), "\n\n";

  my $content =  $response->as_string;
  print "$content\n";
4

1 回答 1

1

在 cookie 中发送密码?没有。

禁止 GET /login。

通过 SSL 将用户名和密码发布到 /login。


CGI中,GET/POST 通过 REQUEST_METHOD 环境变量指示。

您不能阻止确定的人向您的服务器发出 GET 请求,但您可以拒绝像这样处理它(未经测试的代码 - 您必须填写详细信息):

 if ($ENV{REQUEST_METHOD} ne 'POST') {
    # issue a redirect to a suitable error page, then return.        
 }

 my $q = CGI->new();
 my $user = $q->params('username');
 my $password = $q->params('password');

 my $encrypted_password = my_password_encryptor($password);
 unless ( can_log_in($user, $encrypted_password) ) {
   # issue an error message - redirect&return or fall-through...
 }
 else {
   $session->set_user_logged_in();
 }

大多数人不会推出自己的身份验证或会话处理。他们主要使用 CPAN 中的一种,或者包含在更大的应用程序框架中的一种。如果你在做 CGI,你可以使用 CGI::Session。

你可以看看CGI::Application和/或其后代。这些作者已经解决了您遇到的许多问题。

于 2013-06-21T23:04:10.480 回答