Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. sub encode_base64 ($;$)
  4. {
  5. if ($] >= 5.006) {
  6. require bytes;
  7. if (bytes::length($_[0]) > length($_[0]) ||
  8. ($] >= 5.008 && $_[0] =~ /[^\0-\xFF]/))
  9. {
  10. require Carp;
  11. Carp::croak("The Base64 encoding is only defined for bytes");
  12. }
  13. }
  14.  
  15. use integer;
  16.  
  17. my $eol = $_[1];
  18. $eol = "\n" unless defined $eol;
  19.  
  20. my $res = pack("u", $_[0]);
  21. # Remove first character of each line, remove newlines
  22. $res =~ s/^.//mg;
  23. $res =~ s/\n//g;
  24.  
  25. $res =~ tr|` -_|AA-Za-z0-9+/|; # `# help emacs
  26. # fix padding at the end
  27. my $padding = (3 - length($_[0]) % 3) % 3;
  28. $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
  29. # break encoded string into lines of no more than 76 characters each
  30. if (length $eol) {
  31. $res =~ s/(.{1,76})/$1$eol/g;
  32. }
  33. return $res;
  34. }
  35.  
  36.  
  37. sub decode_base64 ($)
  38. {
  39. local($^W) = 0; # unpack("u",...) gives bogus warning in 5.00[123]
  40. use integer;
  41.  
  42. my $str = shift;
  43. $str =~ tr|A-Za-z0-9+=/||cd; # remove non-base64 chars
  44. if (length($str) % 4) {
  45. require Carp;
  46. Carp::carp("Length of base64 data not a multiple of 4")
  47. }
  48. $str =~ s/=+$//; # remove padding
  49. $str =~ tr|A-Za-z0-9+/| -_|; # convert to uuencoded format
  50. return "" unless length $str;
  51.  
  52. ## I guess this could be written as
  53. #return unpack("u", join('', map( chr(32 + length($_)*3/4) . $_,
  54. # $str =~ /(.{1,60})/gs) ) );
  55. ## but I do not like that...
  56. my $uustr = '';
  57. my ($i, $l);
  58. $l = length($str) - 60;
  59. for ($i = 0; $i <= $l; $i += 60) {
  60. $uustr .= "M" . substr($str, $i, 60);
  61. }
  62. $str = substr($str, $i);
  63. # and any leftover chars
  64. if ($str ne "") {
  65. $uustr .= chr(32 + length($str)*3/4) . $str;
  66. }
  67. return unpack ("u", $uustr);
  68. }
  69.  
  70. IRC::print "Loading llrcombs's Binary/Hex/ASCII converter script";
  71.  
  72. #Replace a string without using RegExp.
  73. sub str_replace
  74. {
  75. my $search = shift;
  76. my $replace = shift;
  77. my $subject = shift;
  78. if (! defined $subject) { return -1; }
  79. my $count = shift;
  80. if (! defined $count) { $count = -1; }
  81.  
  82. my ($i,$pos) = (0,0);
  83. while ( (my $idx = index( $subject, $search, $pos )) != -1 )
  84. {
  85. substr( $subject, $idx, length($search) ) = $replace;
  86. $pos=$idx+length($replace);
  87. if ($count>0 && ++$i>=$count) { last; }
  88. }
  89.  
  90. return $subject;
  91. }
  92.  
  93. sub convert_binary
  94. {
  95. my $query = shift;
  96. $l=(length $query)*8;@a=unpack "B$l",$query;
  97.  
  98. $binary = "";
  99.  
  100. foreach $line (@a){
  101. $binary = $binary.$line;
  102. }
  103.  
  104. $binary = "$binary";
  105.  
  106. $i = 0;
  107.  
  108. $binary2 = "";
  109.  
  110. foreach $digit (split(undef,$binary)){
  111. $i++;
  112. $binary2 = $binary2.$digit;
  113. if($i == 8){
  114. $binary2 = $binary2." ";
  115. $i = 0;
  116. }
  117. }
  118. IRC::print ("Sent binary: $query");
  119. if(length($binary2)>422){
  120. while(length($binary2)){
  121. $binary_432 = substr($binary2,0,422);
  122. $binary2 = substr($binary2,423);
  123. IRC::command ("/say $binary_432");
  124. }
  125. }else{
  126. IRC::command ("/say $binary2");
  127. }
  128.  
  129. return 1;
  130. }
  131.  
  132. sub convert_hex {
  133. $query = shift;
  134. $hex = unpack("H*",$query);
  135. IRC::print ("Sent hex: $query");
  136. IRC::command ("/say $hex");
  137. }
  138.  
  139. sub convert_ascii{
  140. $query = shift;
  141. @ascii = unpack("C*",$query);
  142. IRC::print ("Sent ASCII: $query");
  143. IRC::command ("/say @ascii");
  144. }
  145.  
  146. sub convert_base64{
  147. $query = shift;
  148. $base64 = encode_base64($query);
  149. IRC::print ("Sent Base64: $query");
  150. IRC::command ("/say $base64");
  151. }
  152.  
  153. sub watch_binary {
  154. foreach $line (@_) {
  155. $line =~ m/\:(.*?)\!(.*?)\sPRIVMSG\s(.*?)\s\:(.*)?/;
  156.  
  157. $m_nick = $1;
  158. $m_send = $2;
  159. $m_chan = $3;
  160. $m_line = $4;
  161.  
  162. $m_line =~ s/^\s+//; # Remove trailing whitespace
  163. $m_line =~ s/\s+$//;
  164.  
  165. $m_line_original = $m_line;
  166.  
  167. $m_line = str_replace(" ","",$m_line);
  168. if(int($m_line) && length($m_line) % 8 == 0 && $m_line =~ m/^[10]*$/) {
  169. $s=$m_line;$l=length $s;@a=pack "B$l",$s;
  170. IRC::print "Binary from $m_nick: @a";
  171. }elsif(int($m_line) && $m_line =~ m/^[0-9]*$/){
  172. foreach(split(" ",$m_line_original)){
  173. if(!length($_) >= 2 || !length($_) <= 3 || int($_) > 255){
  174. return;
  175. }
  176. }
  177. $a = pack("C*",split(" ",$m_line_original));
  178. IRC::print "ASCII from $m_nick: $a";
  179. }elsif($m_line =~ m/^[0-9A-F]*$/ && length($m_line) % 2 == 0){
  180. $a = pack("H*",$m_line);
  181. IRC::print "Hex from $m_nick: $a";
  182. }elsif($m_line =~ m{^(?: [A-Za-z0-9+/]{4} )*(?:[A-Za-z0-9+/]{2} [AEIMQUYcgkosw048] =|[A-Za-z0-9+/] [AQgw] ==)?\z}x){
  183. $a = decode_base64($m_line);
  184. $str = $a;
  185. if(!$a || !($str=~tr/\200-\377/?/) == $a){
  186. return;
  187. }
  188. IRC::print "Base64 from $m_nick: $a";
  189. }
  190. }
  191. }
  192.  
  193.  
  194.  
  195. IRC::add_message_handler("PRIVMSG", "watch_binary");
  196.  
  197. IRC::add_command_handler ("base64", "convert_base64");
  198.  
  199. IRC::add_command_handler ("hex", "convert_hex");
  200.  
  201. IRC::add_command_handler ("ascii", "convert_ascii");
  202.  
  203. IRC::add_command_handler ("binary", "convert_binary");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement