我有一个接受 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";