#!/usr/bin/perl -w
#
# hack to disresemble Code Red overflow code
#
# Don't grok perl well --- someone else can fix this!
#
# cw@f00f.org, Aug 5

while(<>){
    chomp;
    if(/default\.ida\?(\S+)\s/) {
        $code = $1;
        print "$code";
        print "\n\n";
        open(F,">cr.c") || die "Can't open cr.c";
        print F "/*\n$1\n*/\n\n";
        # print F "__attribute__ ((__section__ (\".text\")))\n";
        print F "char main[] = {\n";
      arrywrite: {
          while(length($code) >= 1) {
              if($code =~ /^%u(..)(..)(.*)$/) { # %u[0-9A-F]{4}
                  $x1 = $1;
                  $x2 = $2;
                  $code = $3;
                  if($x1 =~ /[0-9A-Za-z]{2}/) {
                      print F "0x$x1,\n";
                      if($x2 =~ /[0-9A-Za-z]{2}/) {
                          print F "0x$x2,\n";
                      } else {
                          last arrywrite;
                      }
                  } else {
                      last arrywrite;
                  }
              } else {
                  $x = substr($code, 0, 1);
                  $code = substr($code,1);
                  print F "'$x',\n";
              }
          }
      }
        print F "};\n";
        close(F);
        system "gcc -c cr.c" || die "Unable to compile!";
        system "objdump -D cr.o" || die "Unable to disassemble code!";
        # unlink("cr.c");
    }
}
