Raw TX Dumper


SUBMITTED BY: Guest

DATE: Sept. 19, 2012, 4:47 a.m.

FORMAT: Text only

SIZE: 2.8 kB

HITS: 1582

  1. #!/usr/bin/perl
  2. use IO::Socket;use IO::Select;use Digest::SHA;
  3. $tx=shift(@ARGV);
  4. $btc=IO::Socket::INET->new(PeerAddr=>"localhost",PeerPort=>'8333',Proto=>'tcp') or die "Socket";
  5. $port="\x20\x8d";$magic="\xf9\xbe\xb4\xd9";$mmatch="f9.be.b4.d9";
  6. $sel=IO::Select->new();$sel->add($btc);
  7. $p="\x2c\x7e\x00\x00"; # Version
  8. $p.="\x01\x00\x00\x00\x00\x00\x00\x00"; # Services
  9. $h=sprintf('%x',time());while(length($h)<16){$h="0".$h;}
  10. $h=reverse($h);$p.=pack('h*',$h); # Unix Timestamp
  11. $p.="\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF"; # To IP
  12. $p.="\x7f\x00\x00\x01".$port; # 127.0.0.1:port
  13. $p.="\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF"; # From IP
  14. $p.="\x00\x00\x00\x00".$port; # 0.0.0.0:port
  15. @chars=('a'..'f', 0..9);$rr=join '',map {$chars[rand @chars]} 1..16;
  16. $p.=pack('H*',$rr); # Random Unique ID
  17. $p.="\x00"; # No User Agent
  18. $p.="\x00\x00\x00\x00"; # No Blocks
  19. sendpacket('version',$p);
  20. while($gotver==0){
  21. ($c,$p)=readpacket();
  22. if($c eq 'version'){$gotver=1;}
  23. sleep(1);}
  24. sendpacket('verack','');
  25. $tt=pack('H*',$tx);
  26. $h=Digest::SHA::sha256(Digest::SHA::sha256($tt));
  27. $p="\x01\x01\x00\x00\x00".pack('H*',reverse(unpack('h*',$h)));
  28. sendpacket('inv',$p);$gotreq=0;
  29. while($gotreq==0){
  30. ($c,$pp)=readpacket();
  31. if($c eq 'getdata' && $p eq $pp){$gotreq=1;}
  32. sleep(1);}
  33. sendpacket('tx',$tt);
  34. print "Sent transaction ".unpack('H*',$h)."\n";
  35. close($btc);
  36. sub sendpacket{my $c=shift(@_);my $p=shift(@_);
  37. my $o=$magic;while(length($c)<12){$c.="\x00";}$o.=$c; #Command
  38. my $h=sprintf('%x',length($p));while(length($h)<8){$h="0".$h;}
  39. $h=reverse($h);$o.=pack('h*',$h); # Payload length
  40. $h=Digest::SHA::sha256(Digest::SHA::sha256($p));
  41. $h=unpack('H*',$h);$o.=pack('H*',substr($h,0,8)); # Checksum
  42. $o.=$p; # Payload
  43. print $btc $o;print STDERR "Sent $c (".length($p)." byte payload)\n";
  44. }
  45. sub readpacket{
  46. if(!$sel->can_read(1)){return 0;}
  47. my @x;my $x;while(1){read($btc,$x,1);$x=unpack('H*',$x);
  48. push(@x,$x);if($#x>3){shift(@x);}
  49. if(join('.',@x) eq $mmatch){
  50. my $cmd;my $len;
  51. read($btc,$cmd,12);read($btc,$len,4);read($btc,$x,4);
  52. $x=unpack('h*',$len);$x=reverse($x);$len=hex($x);
  53. $x=chop($cmd);while($x eq "\x00"){$x=chop($cmd);}$cmd.=$x;
  54. print STDERR "Read $cmd ($len byte payload)\n";
  55. if($len>0){read($btc,$x,$len);return ($cmd,$x);}
  56. else{return ($cmd,'');}
  57. }
  58. }
  59. }

comments powered by Disqus