#!/usr/bin/perl

##
#         Name: msfcli
#       Author: spoonm <ninjatools [at] hush.com>
#      Version: $Revision: 1.11 $
#  Description: Simple utility to view log files created with msf
#      License:
#
#      This file is part of the Metasploit Exploit Framework
#      and is subject to the same licenses and copyrights as
#      the rest of this package.
#
##

use Getopt::Std;
use strict;
no utf8;
no locale;

my $VERSION = '$Revision: 1.11 $';

my %opts;
getopts('hv', \%opts);

Version() if($opts{'v'});
if ($opts{'h'} || ! scalar(@ARGV)) {
    Usage();
}


# heh, shokdial is good for something
my $NORMAL = "\033[0m";
my $BLUE   = "\033[34m";
my $RED    = "\033[31m";

foreach my $filename (@ARGV) {
  open(INFILE, "<$filename") or do { print STDOUT "Error opening $_: $!\n"; next; };
  while(<INFILE>) {
    s/\r//g;
    chomp;

    if(/Socket(In|Out): ([^ ]+) ([^ ]+)/ig) {
      my $in   = $1;
      my $src  = $2;
      my $dest = $3;
      print "Socket$in: $BLUE$src$NORMAL -> $RED$dest$NORMAL\n";
      print "-" x 60 . "\n" if($in eq 'Out');
    }
    elsif(/(.*?) CLIENT (.*)/ig) {
      print $BLUE . HexToAscii($2) . $NORMAL;
    }
    elsif(/(.*?) SERVER (.*)/ig) {
      print $RED . HexToAscii($2) . $NORMAL;
    }
  }
}

sub HexToAscii {
  my $hex = shift;
  $hex =~ s/([0-9a-f]{2})/chr(hex($1))/egi;
  return($hex);
}

sub Usage {
    print STDERR "\nUsage: $0 <log1> ... <log99>\n\n";
    exit(0);
}

sub Version {
    my $ver = Pex::Utils::Rev2Ver($VERSION);
    print STDERR qq{
  Msflogdump Version:  $ver

};
  exit(0);
}

