|
|
@ -11,75 +11,106 @@ import scapy.contrib.modbus as mb
|
|
|
|
from triPacket import triPacket
|
|
|
|
from triPacket import triPacket
|
|
|
|
|
|
|
|
|
|
|
|
def decode(pkt):
|
|
|
|
def decode(pkt):
|
|
|
|
'''Insert the packet's `pkt` main informations into the `connec` database'''
|
|
|
|
'''Insert the packet's `pkt` the [type_call,address,status] informations into the `connec` database '''
|
|
|
|
if "ModbusADU" in pkt:
|
|
|
|
if "ModbusADU" in pkt:
|
|
|
|
global miniL
|
|
|
|
global miniL
|
|
|
|
modpkt = pkt["ModbusADU"]
|
|
|
|
modpkt = pkt["ModbusADU"]
|
|
|
|
type_call = ""
|
|
|
|
type_call = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#Multiple Coils/Registers request-> else: Single request
|
|
|
|
if "Multiple" in modpkt.payload.name:
|
|
|
|
if "Multiple" in modpkt.payload.name:
|
|
|
|
type_call += "m"
|
|
|
|
type_call += "m"
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
type_call += "s"
|
|
|
|
type_call += "s"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#Coils or Registers request
|
|
|
|
if "Coil" in modpkt.payload.name:
|
|
|
|
if "Coil" in modpkt.payload.name:
|
|
|
|
type_call += "C"
|
|
|
|
type_call += "C"
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
type_call += "R"
|
|
|
|
type_call += "R"
|
|
|
|
miniL[0] = type_call
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
miniL[0] = type_call
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#Read request (read request are all "Multiple request")
|
|
|
|
if "Read" in modpkt.payload.name:
|
|
|
|
if "Read" in modpkt.payload.name:
|
|
|
|
type_call += "r"
|
|
|
|
type_call += "r"
|
|
|
|
|
|
|
|
#Response for a read request packet's
|
|
|
|
if "Response" in modpkt.payload.name:
|
|
|
|
if "Response" in modpkt.payload.name:
|
|
|
|
|
|
|
|
#Number of byte that have been read
|
|
|
|
byte_count = modpkt.payload.getfieldval("byteCount")
|
|
|
|
byte_count = modpkt.payload.getfieldval("byteCount")
|
|
|
|
status = []
|
|
|
|
status = []
|
|
|
|
|
|
|
|
#Response for a read coils request packet's
|
|
|
|
if "C" in type_call:
|
|
|
|
if "C" in type_call:
|
|
|
|
|
|
|
|
#Get the list of byte's values that have been read
|
|
|
|
value = modpkt.payload.getfieldval("coilStatus")
|
|
|
|
value = modpkt.payload.getfieldval("coilStatus")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#Transform the byte's values into a list of bits values for each byte
|
|
|
|
for j in range(byte_count):
|
|
|
|
for j in range(byte_count):
|
|
|
|
|
|
|
|
#Transform the j'th value to binary -> delete the '0x' header, fill with '0' to have 8 values
|
|
|
|
|
|
|
|
#and then revert the list to get into the growing order
|
|
|
|
val=str(bin(value[j]))[2:].rjust(8,'0')[::-1]
|
|
|
|
val=str(bin(value[j]))[2:].rjust(8,'0')[::-1]
|
|
|
|
|
|
|
|
#Add the values on `status`
|
|
|
|
status.extend(k for k in [*val])
|
|
|
|
status.extend(k for k in [*val])
|
|
|
|
|
|
|
|
#Response for a read registers request packet's
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
|
|
|
|
#Get the list of byte's values that have been read
|
|
|
|
status = modpkt.payload.getfieldval("registerVal")
|
|
|
|
status = modpkt.payload.getfieldval("registerVal")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#Add the read's status into the `connect` database
|
|
|
|
for j in range(len(status)):
|
|
|
|
for j in range(len(status)):
|
|
|
|
#bigL.append([miniL[0],status[j],miniL[2]+j])
|
|
|
|
|
|
|
|
triPacket([miniL[0][1],miniL[2]+j,status[j]],connec)
|
|
|
|
triPacket([miniL[0][1],miniL[2]+j,status[j]],connec)
|
|
|
|
|
|
|
|
#Reset the globalization's list
|
|
|
|
miniL = [0,0,0]
|
|
|
|
miniL = [0,0,0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#First request for a read packet's : Get the starting address and globalize it before getting the response
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
addr = modpkt.payload.getfieldval("startAddr")
|
|
|
|
addr = modpkt.payload.getfieldval("startAddr")
|
|
|
|
miniL[2] = addr
|
|
|
|
miniL[2] = addr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#Write request
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
type_call += "w"
|
|
|
|
type_call += "w"
|
|
|
|
|
|
|
|
#Write request needed information aren't on the Response packet's
|
|
|
|
if "Response" in modpkt.payload.name:
|
|
|
|
if "Response" in modpkt.payload.name:
|
|
|
|
pass
|
|
|
|
#Reset of the globalization
|
|
|
|
|
|
|
|
miniL = [0,0,0]
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
|
|
|
|
#Multiple write request
|
|
|
|
if "m" in type_call:
|
|
|
|
if "m" in type_call:
|
|
|
|
|
|
|
|
#Get the starting address
|
|
|
|
addr = modpkt.payload.getfieldval("startAddr")
|
|
|
|
addr = modpkt.payload.getfieldval("startAddr")
|
|
|
|
#byte_count = modpkt.payload.getfieldval("quantityOutput")
|
|
|
|
#Get the list of values that have been read
|
|
|
|
output_value = modpkt.payload.getfieldval("outputsValue")
|
|
|
|
output_value = modpkt.payload.getfieldval("outputsValue")
|
|
|
|
status = []
|
|
|
|
status = []
|
|
|
|
|
|
|
|
#Multiple write coils request (register's values are already on the good format)
|
|
|
|
if "C" in type_call:
|
|
|
|
if "C" in type_call:
|
|
|
|
|
|
|
|
#Transform the byte's values into a list of bits values for each byte
|
|
|
|
for j in range(len(output_value)):
|
|
|
|
for j in range(len(output_value)):
|
|
|
|
|
|
|
|
#Transform the j'th value to binary -> delete the '0x' header, fill with '0' to have 8 values
|
|
|
|
|
|
|
|
#and then revert the list to get into the growing order
|
|
|
|
val=str(bin(output_value[j]))[2:].rjust(8,'0')[::-1]
|
|
|
|
val=str(bin(output_value[j]))[2:].rjust(8,'0')[::-1]
|
|
|
|
|
|
|
|
#Add the values on `status`
|
|
|
|
status.extend(k for k in [*val])
|
|
|
|
status.extend(k for k in [*val])
|
|
|
|
|
|
|
|
#Add the write's status into the `connect` database
|
|
|
|
for j in range(len(status)):
|
|
|
|
for j in range(len(status)):
|
|
|
|
#bigL.append([miniL[0],status[j],addr+j]) # changer 5 pour mettre l'adresse
|
|
|
|
|
|
|
|
triPacket([miniL[0][1],addr+j,status[j]],connec)
|
|
|
|
triPacket([miniL[0][1],addr+j,status[j]],connec)
|
|
|
|
#print(bigL)
|
|
|
|
#Single write request
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
|
|
|
|
#Single write coil request
|
|
|
|
if "C" in type_call:
|
|
|
|
if "C" in type_call:
|
|
|
|
addr = modpkt.payload.getfieldval("outputAddr")
|
|
|
|
addr = modpkt.payload.getfieldval("outputAddr")
|
|
|
|
output_value = modpkt.payload.getfieldval("outputValue")
|
|
|
|
output_value = modpkt.payload.getfieldval("outputValue")
|
|
|
|
|
|
|
|
#Single write register request
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
addr = modpkt.payload.getfieldval("registerAddr")
|
|
|
|
addr = modpkt.payload.getfieldval("registerAddr")
|
|
|
|
output_value = modpkt.payload.getfieldval("registerValue")
|
|
|
|
output_value = modpkt.payload.getfieldval("registerValue")
|
|
|
|
|
|
|
|
#Add the write's status into the `connect` database
|
|
|
|
triPacket([miniL[0][1],addr,output_value],connec)
|
|
|
|
triPacket([miniL[0][1],addr,output_value],connec)
|
|
|
|
#bigL.append([miniL[0],output_value,addr])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#print(bigL)
|
|
|
|
|
|
|
|
#bigL = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#Connection to the database
|
|
|
|
print("In order for data sniffed to be stored inside the database, please register the following :")
|
|
|
|
print("In order for data sniffed to be stored inside the database, please register the following :")
|
|
|
|
DB_HOST = input('host of the database server : ')
|
|
|
|
DB_HOST = input('host of the database server : ')
|
|
|
|
if not DB_HOST:
|
|
|
|
if not DB_HOST:
|
|
|
@ -93,7 +124,9 @@ if not DB_USER:
|
|
|
|
DB_PASSWORD = getpass.getpass('user password : ')
|
|
|
|
DB_PASSWORD = getpass.getpass('user password : ')
|
|
|
|
connec=[DB_HOST,DB_NAME,DB_USER,DB_PASSWORD]
|
|
|
|
connec=[DB_HOST,DB_NAME,DB_USER,DB_PASSWORD]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#Initialization in order globalize the First Packet with his response
|
|
|
|
miniL = [0,0,0]
|
|
|
|
miniL = [0,0,0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#Sniff the packets received on the localhost's network interface and call the decode() function for each of them
|
|
|
|
scapy.sniff(iface="lo", prn=decode)
|
|
|
|
scapy.sniff(iface="lo", prn=decode)
|
|
|
|
|
|
|
|
|
|
|
|