#!/usr/bin/perl
#######################################
# WIN32 BINARY TO HEX CONVERTER       #
# BY YAG KOHHA skyhole [at] gmail.com #
#######################################


my($strInputFile) = $ARGV[0];
my($intByteCount) = "";
my($binBytes) = "";
my($intReadLength) = "";

if ($#ARGV ne "0")
{
 print "WIN32 BINARY TO C++ CONVERTER\n";
 print "BY YAG KOHHA skyhole [at] gmail.com\n";
 print "Usage:\n";
 print "convert <file name>\n";
 print "convert payload.exe\n";

 exit;
}

$intReadLength = 16;

$fl_nm=generate_wd(6);

if (open(INPUTFILE, "< $strInputFile"))
{
 open(OUTPUTFILE, "> $fl_nm.txt");
 
  binmode(INPUTFILE);
  
   $intByteCount = 0;
   
    while (read(INPUTFILE, $binBytes, $intReadLength))
     {
       print (OUTPUTFILE uc(unpack("H" . ($intReadLength * 2), $binBytes)), "\n");
        }
	
	 close(INPUTFILE);
	  close(OUTPUTFILE);
	  }
	  else
	  {
	   print "ERROR! Cannot open file $strInputFile";
	   }
	   

@data=`cat ./$fl_nm.txt`;
my @dump, $lnstr, $ln;
foreach $ln (@data){
chomp ($ln);
$lnstr=length($ln);
convert();
#push (@dump, "\n");
}
print @dump;
`rm -rf ./$fl_nm.txt`;

sub convert(){
@dump_temp=split (//,$ln);

$byte="";
for ($i = 0; $i < $lnstr; $i++) {

  $lnbyte= length($byte);
  if ($lnbyte < 2) {
  $byte="\\\%@dump_temp[$i]@dump_temp[$i+1]"; 
  push (@dump, $byte);
  }
  else {$byte=""; }
}  
}

sub generate_wd()
{
my $wdsize = shift;
my @alphanumeric = ('a'..'z', 'A'..'Z');
my $wd = join '',
map $alphanumeric[rand @alphanumeric], 0..$wdsize;
return $wd;
}
