-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPercivalReceiveTCP.py
executable file
·66 lines (57 loc) · 2.26 KB
/
PercivalReceiveTCP.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
64
65
66
'''
PercivalReceiveTCP - Capture TCP traffic, extract and display the Percival payload
Acting as TCP server
'''
import socket, time, sys, numpy as np
# sys.stdout.flush() # Flush stdout
# Or start from the command line with: python -u
def PercivalReceiveTCP(address, udpPort):
TCP_IP = address # "192.168.0.103"
TCP_PORT = udpPort # 4321
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Modified to act as a server [bind, listen]
try:
sock.bind((TCP_IP, TCP_PORT))
sock.listen(1)
except Exception as e:
print "PRT Socket setup error:", e
(subframe, data, packetCounter) = (0, 0, 0)
listFiles = []
try:
print "Listening for client on %s:%d." % (TCP_IP, TCP_PORT)
connection, client_address = sock.accept()
data, addr = connection.recvfrom(36) #1024) # buffer size is 1024 bytes
npData = np.fromstring(data, dtype=np.uint8)
if len(npData) > 0:
print "Received %d bytes:" % len(npData)
for index in range(len(npData)):
if (index != 0) and (index % 16 == 0):
print ""
if (index % 8 == 0):
print " ",
print "%02X" % npData[index],
print ""
# Close connection
connection.close()
except KeyboardInterrupt:
print "\n*** User initiated shutdown (2) ***"
except Exception as e:
print "PRT Unsuspected error:", e
else:
print "Closing down socket and exiting. [Aware: Using numpy puts 0 at 0x30, etc..]"
connection.close()
sock.close()
if __name__ == "__main__":
# Test user specified (TCP port) command line argument
(address, port) = ("10.2.0.11", 8000)
try:
address = sys.argv[1]
port = int(sys.argv[2])
if not "." in address: # rudimentary sanity check
raise Exception
except Exception:
print "Invalid input(s); Correct Usage: "
print "python PercivalReceiveTCP.py <address> <TCPport>\n"
print "e.g. ' python PercivalReceiveTCP.py 10.2.0.1 8000'\n"
else:
PercivalReceiveTCP(address, port)