#!/usr/bin/perl -w

# $Id: hex0r.pl,v 1.34 2003/01/16 20:41:44 dmuz Exp $

##
# sec.angrypacket.com
##

##
# multi function hex convertor
#
# by dmuz@angrypacket.com
#
# i see you know regex style...
# pheer my perlfoo! hi-yaa!
##

use strict;
use Getopt::Std;

my (%args);
my ($infile, $hex, @lines, $line);
my ($quiet);
my ($do_hex, $do_ascii, $do_tcpd, $do_zerox, $do_slash, $do_ethereal);
my ($no_newl, $no_tabs, $no_quot);

getopts("f:s:xazhtnTcdqe", \%args);

if(!$args{q}){
 print STDERR "\nhex0r.pl - multi function hex convertor\n\n";
}

sub USAGE() {
 select STDERR;
 print "$0 -s <string> -f <file> -xdatz -Tn -h\n\t";
 print '-s <input> OR -f <input file>'."\n\t";
 print '-q quiet output mode (no \n or banner)'."\n\t";
 print '-n do not interpret newlines in files'."\n\t";
 print '-T do not interpret tabs in files'."\n\t";
 print '-c remove double quotes from files'."\n\t";
 print '-x convert "\x" hex to ascii'."\n\t";
 print '-a convert ascii to "\x" hex'."\n\t";
 print '-t convert tcpdump style hex to "\x" hex'."\n\t";
 print '-z convert comma seperated "0x" style hex to "\x" hex'."\n\t";
 print '-d convert "/00" style hex to "\x" hex'."\n";
 print '-e convert ethereal tcp stream format to "\x" hex'."\n";
 print "\n";
 exit;
}

if($args{h}) {
 &USAGE;
}

if (!$args{s} && !$args{f}) {
 print STDERR "Reading from STDIN\n\n";
 $args{f} = "<&STDIN";
}

if ($args{s} && $args{f}) {
 print STDERR "-s and -f are mutually exclusive, dude.\n\n";
 &USAGE;
}

$infile = $args{f};
$hex = $args{s};
$quiet = $args{q};

$do_hex = $args{a};
$do_ascii = $args{x};
$do_tcpd = $args{t};
$do_zerox = $args{z};
$do_slash = $args{d};
$do_ethereal = $args{e};

$no_newl = $args{n};
$no_tabs = $args{T};
$no_quot = $args{c};

if (!$do_hex && !$do_ascii && !$do_tcpd && !$do_zerox && !$do_slash && !$do_ethereal) {
 print STDERR "You must specify either -x, -z, -d, -a, -t or -e... try -h for help\n";
 exit;
}

if ($infile) {
 open(IN,$infile) || die "Error opening '$infile': $!\n";
 while (<IN>) {
  if ($no_newl) {
   # strip newlines
   s/\n//g;
  }
  if ($no_tabs) {
   # strip tabs
   s/\t//g;
  }
  if ($no_quot) {
   # strip quotes
   s/"//g;
  }
  $hex .= $_;
 }
}

if ($do_ascii) {
 # hex to ASCII
 $hex =~ s/\\x([a-fA-F0-9]{2,2})/chr(hex($1))/eg;
} elsif ($do_hex) {
 # ASCII to hex
 $hex =~ s/([\W|\w])/"\\x" . uc(sprintf("%2.2x",ord($1)))/eg;
} elsif ($do_tcpd) {
 #tcpdump (0AAE 0D0A 2F2E) style to \x
 $hex =~ s/ |\n//g;
 $hex =~ s/[a-fA-F0-9]{2}/ $&/g;
 $hex =~ s/ /\\x/g;
} elsif ($do_zerox) {
 # 0x style (0x2e, 0x2f) to \x
 $hex =~ s/ //g;
 $hex =~ s/^0x|,0x/\\x/g;
} elsif ($do_slash) {
 $hex =~ s/ |\n//g;
 $hex =~ s/\//\\x/g;
} elsif ($do_ethereal) {
 @lines = split(/\n/,$hex);
 $hex = "";
 foreach $line (@lines) {
  $line =~ s/^([0-9]|[A-F]){8}  //g;
  $line =~ s/.{8} .{8}$//g;
  $line =~ s/!([0-9]|[A-F])//g;
  $line =~ s/ {3,9}.*//g;
  $line =~ s/ |\n|\r//g;
  $line =~ s/[a-fA-F0-9]{2}/ $&/g;
  $line =~ s/ /\\x/g;
  $hex = $hex . $line
 }
}

if($quiet) {
 print "$hex";
 #print STDERR "\n";
} else {
 print "$hex\n";
}


if ($infile) {
 close(IN);
}

# you are now a hex0r!
