-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathicmpcat_client.py
More file actions
77 lines (61 loc) · 1.63 KB
/
icmpcat_client.py
File metadata and controls
77 lines (61 loc) · 1.63 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import threading
import time
from scapy.all import *
IP_DST = sys.argv[1]
PING_INTERVAL = float(sys.argv[2])
PACKET_EMPTY = threading.Event()
PACKET_EMPTY.set()
PACKET = ""
def stdin_reader():
global PACKET
while True:
#Read in a packet from STDIN
line = sys.stdin.readline()
if line == '':
return
PACKET = line
#CLEAR the event, notifying that a packet is ready
PACKET_EMPTY.clear()
#WAIT here before reading the next packet
PACKET_EMPTY.wait(None)
def icmp_reader():
def process_packet(pkt):
if pkt.haslayer(IP):
if pkt[IP].src == IP_DST:
if pkt.haslayer(ICMP):
if pkt[ICMP].type == 0:
payload = str(pkt[ICMP].payload)
if len(payload) > 1:
if payload[0] == 'a':
sys.stdout.write(payload[1:])
sys.stdout.flush()
sniff(store=0, prn=process_packet)
#Spawn a thread of stdin_reader()
t_stdin_reader = threading.Thread(target=stdin_reader)
t_stdin_reader.setDaemon(True)
t_stdin_reader.start()
#Spawn a thread of icmp_reader()
t_icmp_reader = threading.Thread(target=icmp_reader)
t_icmp_reader.setDaemon(True)
t_icmp_reader.start()
while True:
try:
#Should we send the packet from stdin_reader or an empty one?
if not PACKET_EMPTY.isSet():
#Useful Packet
line_slice = PACKET[0:55]
PACKET = PACKET[55:]
pkt = IP(dst=IP_DST)/ICMP(type=8)/Raw("a" + line_slice)
send(pkt, verbose=False)
if len(PACKET) == 0:
PACKET_EMPTY.set()
else:
#Empty Packet
pkt = IP(dst=IP_DST)/ICMP(type=8)/Raw("b")
send(pkt, verbose=False)
time.sleep(PING_INTERVAL)
except KeyboardInterrupt:
break