2012-08-13 27 views
-3

我試圖運行下面的perl腳本,但它顯示一個錯誤。所需的文件是我把它們放在腳本文件的同一目錄中。perl腳本顯示錯誤:找不到文件

#! /usr/bin/perl 

@attackFileList=("_perParamDefaut_URLs.txt", "_perParam_URLs.txt", "_Cartesian_URLs.txt", "_Random_URLs.txt"); 
@legitFileList=("_Legit_URLs.txt"); 

$app=$ARGV[0]; 

print "Testing $app\n"; 
$startTime=time(); 
$attackCaught=$notCaught=$syntaxError=$other=$total=$error=$redirect=$success=0; 
for $fileEnding (@attackFileList) { 
    $fileName="$app$fileEnding"; 
    open(inputFile, "D:\\$app/$fileName") || die "Could not open $fileName\n"; 
    while ($line=<inputFile>) { 
     chomp $line; 
     @wget = split//, $line; 

     if ($wget[1] ne "--post-data") { 
         $wget[1] =~ s/"/\\"/g; 
       $wget[1] =~ s/`/\\`/g; 
       $wget[1] =~ s/_nosessions/_current/; 
       $wget[1] =~ s/endeavor.cc.gt.atl.ga.us/localhost:8080/; 
       $wget[1] =~ s/^\\"(.*)\\"$/"\1"/; 
       $command=$wget[0]." -O - "."\"".$wget[1]."\""; 
     } else { 
       $wget[2] =~ s/"/\\"/g; 
       $wget[2] =~ s/`/\\`/g; 
       $wget[3] =~ s/_nosessions/_current/; 
       $wget[3] =~ s/endeavor.cc.gt.atl.ga.us/localhost:8080/; 
      $wget[2] =~ s/^\\"(.*)\\"$/"\1"/; 
      $command=$wget[0]." -O - ".join(" ", $wget[1], $wget[2], $wget[3]); 
     } 

     $output=`$command 2>&1`; 
     $retValue=$?>>8; 
     if (($retValue == 0)||($output=~/302 Moved Temporarily/)) { 
      if ($output=~/amnesia\.SQLIAException/) { 
        $attackCaught++; 
       #print errorLog "PolicyViolationException:\n \t$wget[3]\n\t$wget[2]\n\t$command\n\n"; 
       } elsif ($output=~/amnesia\.UndetectedSQLIA/) { 
        $notCaught++; 
       print errorLog "UndetectedSQLIA:\n \t$wget[3]\n\t$wget[2]\n\t$command\n\n"; 
       } elsif ($output=~/lexer\.SQLLexerException/) { 
        $syntaxError++; 
       #print errorLog "SyntaxError:\n \t$wget[3]\n\t$wget[2]\n\t$command\n\n"; 
       } else { 
       if ($output=~/302 Moved Temporarily/) { 
        $redirect++; 
       } 
        $other++; 
      } 
      $success++; 
     } else { 
      $error++; 
     } 
     $total++; 
     if (($total % 100) == 0) {print "$total, ";} 
    } 
    close(inputFile); 
} 
print "\n"; 
$endTime=time(); 

$elapsedTime=$endTime-$startTime; 

print "**Testing Results: Attack**\n"; 
print "Total: $total\n"; 
print "\tValid URL requests: $success\n"; 
print "\t\tSQLIA detected: $attackCaught\n"; 
print "\t\tUndetected: $notCaught\n"; 
print "\t\tSyntax Errors: $syntaxError\n"; 
print "\t\tOther: $other\n"; 
print "\t\t\tRedirects: $redirect\n"; 
print "\tError URL requests: $error\n"; 
$omitted=$total-$error-$success; 
print "\tOmitted: $omitted\n"; 
print "\nTime: $elapsedTime seconds\n"; 

open (outFile, ">>Effective.results") || die "Could not open Effective.results\n"; 
print outFile "$app-attk\t$total\t$success\t$attackCaught\t$notCaught\t$syntaxError\t$other\t$error\t$omitted\n"; 
close(outFile); 

$startTime=time(); 
$attackCaught=$notCaught=$syntaxError=$other=$total=$error=$redirect=$success=0; 
foreach $fileEnding (@legitFileList) { 
    $fileName="$app$fileEnding"; 
    open(inputFile, "$app/$fileName") || die "Could not open $fileName\n"; 
    while ($line=<inputFile>) { 
     chomp $line; 
     @wget = split//, $line; 

     if ($wget[1] ne "--post-data") { 
      $wget[1] =~ s/"/\\"/g; 
       $wget[1] =~ s/`/\\`/g; 
      $wget[1] =~ s/_nosessions/_current/; 
      $wget[1] =~ s/endeavor.cc.gt.atl.ga.us/localhost:8080/; 
      $wget[1] =~ s/^\\"(.*)\\"$/"\1"/; 
        $command=$wget[0]." -O - "."\"".$wget[1]."\""; 
     } else { 
      $wget[2] =~ s/"/\\"/g; 
         $wget[2] =~ s/`/\\`/g; 
      $wget[3] =~ s/_nosessions/_current/; 
      $wget[3] =~ s/endeavor.cc.gt.atl.ga.us/localhost:8080/; 
      $wget[2] =~ s/^\\"(.*)\\"$/"\1"/; 
      $command=$wget[0]." -O - ".join(" ", $wget[1], $wget[2], $wget[3]); 
     } 

     $output=`$command 2>&1`; 
     $retValue=$?>>8; 
     if (($retValue == 0)||($output=~/302 Moved Temporarily/)) { 
      if ($output=~/amnesia\.SQLIAException/) { 
        $attackCaught++; 
       print errorLog "SQLIAException:\n\t$wget[3]\n\t$wget[2]\n\t$command\n\n"; 
       } elsif ($output=~/amnesia\.UndetectedSQLIA/) { 
        $notCaught++; 
       print errorLog "UndetectedSQLIA:\n \t$wget[3]\n\t$wget[2]\n\t$command\n\n"; 
       } elsif ($output=~/lexer\.SQLLexerException/) { 
        $syntaxError++; 
       print errorLog "SyntaxError:\n \t$wget[3]\n\t$wget[2]\n\t$command\n\n"; 
       } else { 
       if ($output=~/302 Moved Temporarily/) { 
        $redirect++; 
       } 
        $other++; 
      } 
      $success++; 
     } else { 
      $error++; 
     } 

     $total++; 
     if (($total % 100) == 0) {print "$total, ";} 
    } 
    close(inputFile); 

} 
print "\n"; 
$endTime=time(); 
close(errorLog); 
$elapsedTime=$endTime-$startTime; 

print "**Testing Results: Legit**\n"; 
print "Total: $total\n"; 
print "\tValid URL requests: $success\n"; 
print "\t\tSQLIA detected: $attackCaught\n"; 
print "\t\tUndetected: $notCaught\n"; 
print "\t\tSyntax Errors: $syntaxError\n"; 
print "\t\tOther: $other\n"; 
print "\t\t\tRedirects: $redirect\n"; 
print "\tError URL requests: $error\n"; 
$omitted=$total-$error-$success; 
print "\tOmitted: $omitted\n"; 
print "\nTime: $elapsedTime seconds\n"; 

open (outFile, ">>Effective.results") || die "Could not open Effective.results\n"; 
print outFile "$app-legit\t$total\t$success\t$attackCaught\t$notCaught\t$syntaxError\t$other\t$error\t$omitted\n"; 
close(outFile); 

電話: perl的script.bat門戶

結果: 無法打開文件portal_perParamDefault_URLs.txt

+1

使用'$'知道爲什麼打開失敗的原因:'死「無法打開$文件名:$!」;' – Toto 2012-08-13 09:32:56

+0

是的!我們都會放下一切,爲你讀幾十行代碼。如果你想在這裏尋求幫助,你應該a)確保你使用'strict'和'warnings',b)要求Perl儘可能多地提供幫助(例如,在你的錯誤信息中使用'$ 1'開放「)和c)給我們提供你的程序的一小部分,因爲我們需要了解你的問題。 – 2012-08-13 10:00:11

+0

你說文件和程序在同一個目錄下。但是當你運行程序時,當前的工作目錄是什麼? – 2012-08-13 10:01:44

回答

1

Windows路徑可以用斜槓,而不是反斜槓,或更好地利用File::Spec來處理它處理像:

my $full_path = File::Spec->catpath("D", $app, $fileName) 
open(inputFile, $full_path) or die $!; 
3

你的編碼是可怕的,但我建議你的問題的答案可能是你的意思是_perParamDefault_URLs你有_perParamDefaut_URLs

之前要求別人饒恕自己的時間來幫助你,請有足夠周到正確格式化您的代碼,以便它是可讀的,加use strictuse warnings你的程序的頂部,所有變量的使用my接近申報他們的第一個使用點。

您還應該使用詞法文件句柄,open的三參數形式,並將$!變量添加到您的骰子字符串的內容中。一個樣板調用open應該像

open my $fh, '<', $filename or die "Unable to open '$filename': $!";