我的Perl脚本中有一些错误,我查看了源代码,但找不到问题.
#Tool: decoding shell codes/making shell codes use strict; use Getopt::Std; my %opts=(); getopts("f:xa", \%opts); my($infile, $hex); my($gen_hex, $gen_ascii); sub usage() { print "$0 -f[-x | -a] \n\t"; print '-p '."\n\t"; print '-x convert "\nxXX" hex to readable ascii'."\n\t"; print '-a convert ascii to "\xXX" hex'."\n\t"; print "\n"; exit; } $infile = $opts{f}; $gen_hex = $opts{a}; $gen_ascii = $opts{x};use if((!opts{f} || (!$gen_hex && !$gen_ascii)) { usage(); exit; } if($infile) { open(INFILE,$infile) || die "Error Opening '$infile': $!\n"; while( ) { #Strips newlines s/\n/g; #Strips tabs s/\t//g; #Strips quotes s/"//g; $hex .= $_; } } if($gen_ascii) { # \xXX hex style to ASCII $hex =~ s/\\x([a-fA-F0-9]{2,2})/chr(hex($1)/eg; } elsif ($gen_hex) { $hex =~ s/([\W|\w)/"\\x" . uc(sprintf("%2.2x",ord($1)))/eg; } print "\n$hex\n"; if($infile) { close(INFILE); }
给了我错误
Backslash found where operator expected at 2.txt line 36, near "s/\" (Might be runaway multi-line // string starting on line 34) syntax error at 2.txt line 25, near ") {" syntax error at 2.txt line 28, near "}" syntax error at 2.txt line 36, near "s/\" syntax error at 2.txt line 41. nar "}" Execution of 2.txt aborted due to compilation errors
你看到了问题吗?
#Strips newlines s/\n/g;
是错的.你忘记了一个额外的/
:
#Strips newlines s/\n//g;
此外,这里的括号太少:
if((!opts{f} || (!$gen_hex && !$gen_ascii)) {
你可能看起来只有一个额外的,而不是添加一些.把它拿出来吧.
作为旁注,请尽可能尝试use warnings;
.这是一件好事.
编辑:虽然我在这,你可能要小心你的open()
s:
open(INPUT,$input);
可以被滥用.如果$input
是的话">file.txt"
?然后open()
会尝试打开文件进行写作 - 而不是你想要的.试试这个:
open(INPUT, "<", $input);
有许多错误:尾随use
,失踪/
的s
操作,不平衡的括号if
表达.有点整理:
use strict; use Getopt::Std; my %opts = (); getopts( "f:xa", \%opts ); my ( $gen_hex, $gen_ascii ); sub usage() { print <[-x | -a] -p -x convert "\\xXX" hex to readable ascii -a convert ascii to "\\xXX" hex EOU } @ARGV = ( $opts{f} ) if exists $opts{f}; $gen_hex = $opts{a}; $gen_ascii = $opts{x}; if ( not( $gen_hex xor $gen_ascii ) ) { usage(); exit; } my $transform = $gen_ascii ? sub { s/\\x([a-fA-F0-9]{2,2})/pack'H2', $1/eg; } : sub { s/([^[:print:]])/'\\x'.uc unpack'H2', $1/eg; }; while (<>) { s/\n #Strips newlines | \t #Strips tabs | " #Strips quotes //xg; &$transform; print; }