From 812a9b2c9c510e9861126b06ec043d5caed513f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eloan=20ANDR=C3=89?= Date: Fri, 24 Mar 2023 14:13:05 +0100 Subject: [PATCH] =?UTF-8?q?Mise=20=C3=A0=20jour=20de=20'src/decoder=5Fprop?= =?UTF-8?q?re.py'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/decoder_propre.py | 57 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 12 deletions(-) diff --git a/src/decoder_propre.py b/src/decoder_propre.py index 49378ce..d9072b1 100644 --- a/src/decoder_propre.py +++ b/src/decoder_propre.py @@ -11,75 +11,106 @@ import scapy.contrib.modbus as mb from triPacket import triPacket 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: global miniL modpkt = pkt["ModbusADU"] type_call = "" + #Multiple Coils/Registers request-> else: Single request if "Multiple" in modpkt.payload.name: type_call += "m" else: type_call += "s" + #Coils or Registers request if "Coil" in modpkt.payload.name: type_call += "C" else: 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: type_call += "r" + #Response for a read request packet's if "Response" in modpkt.payload.name: + #Number of byte that have been read byte_count = modpkt.payload.getfieldval("byteCount") status = [] + #Response for a read coils request packet's if "C" in type_call: + #Get the list of byte's values that have been read 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): + #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] + #Add the values on `status` status.extend(k for k in [*val]) + #Response for a read registers request packet's else: + #Get the list of byte's values that have been read status = modpkt.payload.getfieldval("registerVal") + + #Add the read's status into the `connect` database 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) + #Reset the globalization's list miniL = [0,0,0] + + #First request for a read packet's : Get the starting address and globalize it before getting the response else: addr = modpkt.payload.getfieldval("startAddr") miniL[2] = addr + + #Write request else: type_call += "w" + #Write request needed information aren't on the Response packet's if "Response" in modpkt.payload.name: - pass + #Reset of the globalization + miniL = [0,0,0] else: + #Multiple write request if "m" in type_call: + #Get the starting address 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") status = [] + #Multiple write coils request (register's values are already on the good format) 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)): + #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] + #Add the values on `status` status.extend(k for k in [*val]) - + #Add the write's status into the `connect` database 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) - #print(bigL) + #Single write request else: + #Single write coil request if "C" in type_call: addr = modpkt.payload.getfieldval("outputAddr") output_value = modpkt.payload.getfieldval("outputValue") + #Single write register request else: addr = modpkt.payload.getfieldval("registerAddr") output_value = modpkt.payload.getfieldval("registerValue") + #Add the write's status into the `connect` database 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 :") DB_HOST = input('host of the database server : ') if not DB_HOST: @@ -93,7 +124,9 @@ if not DB_USER: DB_PASSWORD = getpass.getpass('user password : ') connec=[DB_HOST,DB_NAME,DB_USER,DB_PASSWORD] - +#Initialization in order globalize the First Packet with his response 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)