tags:

views:

1169

answers:

4

There are some errors in my Perl script, I looked though the source code but couldn't find the problem.

#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 <file> [-x | -a] \n\t";
print '-p <path to input file>'."\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(<INFILE>) {
#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);
}

gives me the errors

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

Do you see the problems?

+13  A: 
#Strips newlines
s/\n/g;

Is wrong. You forgot an extra /:

#Strips newlines
s/\n//g;

Also, there are too few parenthesis here:

if((!opts{f} || (!$gen_hex && !$gen_ascii)) {

Rather than add some, you appear to have one extra one. Just take it out.

As a side note, try to use warnings; whenever possible. It's a Good Thing™.

EDIT: While I'm at it, you might want to be careful with your open()s:

open(INPUT,$input);

can be abused. What if $input is ">file.txt"? Then open() will try to open the file for writing - not what you want. Try this instead:

open(INPUT, "<", $input);
Chris Lutz
A: 

Actually, I think the error is here :

s/"//g;

The double quotes should be escaped, so that the line would become :

s/\"//g;

You can notice that this is the line the syntax highlighting goes wrong on SO.

Geo
The "s don't need to be escaped. They're inside //s. If there were /'s, they would need to be escaped, but "s only need to be escaped inside "s. SO just doesn't highlight syntax for Perl regexes very well.
Chris Lutz
I would backslash it just to fix the syntax highlighting, but Perl doesn't care one way or the other. Most editors just aren't as good at parsing Perl as Perl is.
cjm
That maybe where the syntax highlighter loses it, but Perl is okay, because the opening slash "quotes" everything but another slash.
Axeman
+5  A: 

There are many errors: trailing use, missing / in s operator, unbalanced brackets in if expression. Little bit tidy up:

use strict;
use Getopt::Std;

my %opts = ();
getopts( "f:xa", \%opts );

my ( $gen_hex, $gen_ascii );

sub usage() {
    print <<EOU
$0 -f <file> [-x | -a]
    -p <path to input file>
    -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;
}
Hynek -Pichi- Vychodil
Just curious, but why have a usage() sub if usage() is only called once?
Chris Lutz
It's separation of code by meanings. This technique allows easy future refactoring end extensibility. Goal is not make as shorter code as possible, but well readable, maintainable and well working code.
Hynek -Pichi- Vychodil
Have you heard about literal programming. It similar but without literal programing tools ;-)
Hynek -Pichi- Vychodil
+1  A: 
line25: if((!opts{f} || (!$gen_hex && !$gen_ascii)) {
line26: usage();

It's $opts{f}