我有一個CGI服務器端腳本,它接受GET和POST以及登錄參數。 我想測試它以確保它不易受到攻擊。所以計劃是使用Perl LWP,並在GET和POST中發送登錄參數,並比較結果。界面已被更改,所以只有在POST中,我們才能在會話cookie中發送用戶名和密碼(不確定這是否是一個好主意),那麼如何測試它?這是我到目前爲止:Perl:我如何測試使用「登錄」參數接受GET請求的URL(https)
#!/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";