-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathICMP.py
More file actions
58 lines (46 loc) · 1.73 KB
/
ICMP.py
File metadata and controls
58 lines (46 loc) · 1.73 KB
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
import socket
import struct
from collections import namedtuple
class ConstICMP:
ICMP_ECHO_REQUEST = 8
ICMP_ECHO_REQUEST_CODE = 0
ECHO_REPLY = 0
TIME_EXCEEDED = 11
DEST_UNREACHABLE = 3
def build_packet(sequence):
checksum = 0
ID = socket.htons(1)
header = struct.pack("bbHHh", ConstICMP.ICMP_ECHO_REQUEST,
ConstICMP.ICMP_ECHO_REQUEST_CODE,
checksum, ID, sequence)
data = struct.pack("qqqqqqqq", 0, 0, 0, 0, 0, 0, 0, 0)
checksum = socket.htons(get_checksum(header + data))
header = struct.pack("bbHHh", ConstICMP.ICMP_ECHO_REQUEST,
ConstICMP.ICMP_ECHO_REQUEST_CODE,
checksum, ID, sequence)
packet = header + data
return packet
def get_checksum(string):
string = bytearray(string)
csum = 0
count_to = (len(string) // 2) * 2
for count in range(0, count_to, 2):
this_val = string[count + 1] * 256 + string[count]
csum = csum + this_val
csum = csum & 0xffffffff
if count_to < len(string):
csum = csum + string[-1]
csum = csum & 0xffffffff
csum = (csum >> 16) + (csum & 0xffff)
csum = csum + (csum >> 16)
answer = ~csum
answer = answer & 0xffff
answer = answer >> 8 | (answer << 8 & 0xff00)
return answer
def parse_header(recv_packet):
icmp_header = recv_packet[20:28]
request_type, code, checksum, packet_id, sequence = \
struct.unpack('bbHHh', icmp_header)
Header = namedtuple('Header', ['request_type', 'code', 'checksum',
'packet_id', 'sequence'])
return Header(request_type, code, checksum, packet_id, sequence)