1717from python import unpack
1818from python import repack
1919
20- def corrupt_binary_data (data , positions ):
20+ #To be removed
21+ def corrupt_binary_data_old (data , positions = None ):
2122 # for each position, flip the last bit of the byte in the data
2223 for pos in positions :
2324 # get the byte index
@@ -33,17 +34,37 @@ def corrupt_binary_data(data, positions):
3334 # return the corrupted data
3435 return data
3536
37+ def corrupt_binary_data (data ):
38+ # first byte align the hex string
39+ odd_length = 0
40+ if len (data )% 2 == 1 :
41+ data = '0x0' + data .split ('0x' )[1 ]
42+ odd_length = 1
43+ #remove '0x' and create a bytearray
44+ data = bytearray .fromhex (data [2 :])
45+ import random
46+ #Go byte wise and chose a bit to corrupt
47+ for corrupt_byte_index in range (0 , len (data )):
48+ corrupt_bit_index = (random .randint (0 ,7 ))
49+ data [corrupt_byte_index ] ^= 1 << corrupt_bit_index
50+ #print(corrupt_byte_index, corrupt_bit_index, len(data), data[corrupt_byte_index], data)
51+ #Convert back to hexstring
52+ data = '0x' + data .hex ()[odd_length :]
53+ print (data )
54+ return data
55+
3656def descriptor_error (output_dict ):
3757 """
3858 This function injects error to the initial descriptor data of the first record
3959 Parameter:
4060 output_dict: ouptut dictionary having all the values
4161 """
4262 data = output_dict ["FirmwareDeviceIdentificationArea" ]["FirmwareDeviceIDRecords" ][0 ]["RecordDescriptors" ][0 ]["InitialDescriptorData" ]
43- data = bytearray (data ,"utf-8" )
44- corrupted_data = corrupt_binary_data (data , [3 , 5 , 4 ])#function for corruption data
45- corrupted_data_hex = corrupted_data .decode ("utf-8" )
46- output_dict ["FirmwareDeviceIdentificationArea" ]["FirmwareDeviceIDRecords" ][0 ]["RecordDescriptors" ][0 ]["InitialDescriptorData" ] = corrupted_data_hex
63+ #data = bytearray(data,"utf-8")
64+ #corrupted_data = corrupt_binary_data(data)#function for corruption data
65+ corrupted_data = corrupt_binary_data (data )#function for corruption data
66+ #corrupted_data_hex = corrupted_data.decode("utf-8")
67+ output_dict ["FirmwareDeviceIdentificationArea" ]["FirmwareDeviceIDRecords" ][0 ]["RecordDescriptors" ][0 ]["InitialDescriptorData" ] = corrupted_data
4768 #### Data is corruptes inside output dictionary
4869
4970def UUID_error (output_dict ):
@@ -52,14 +73,22 @@ def UUID_error(output_dict):
5273 Parameter:
5374 output_dict: ouptut dictionary having all the values
5475 """
55- data = output_dict ["FirmwareDeviceIdentificationArea" ]["FirmwareDeviceIDRecords" ][4 ]["RecordDescriptors" ][1 ]["AdditionalDescriptorIdentifierData" ]
56- data = bytearray (data ,"utf-8" )
57- corrupted_data = corrupt_binary_data (data , [2 , 4 , 4 ])#function for corruption data
58- corrupted_data_hex = corrupted_data .decode ("utf-8" )
59- output_dict ["FirmwareDeviceIdentificationArea" ]["FirmwareDeviceIDRecords" ][4 ]["RecordDescriptors" ][1 ]["AdditionalDescriptorIdentifierData" ] = corrupted_data_hex
76+ target_record = random .randint (0 ,len (output_dict ["FirmwareDeviceIdentificationArea" ]["FirmwareDeviceIDRecords" ])- 1 )
77+ index = 0
78+ for element in output_dict ["FirmwareDeviceIdentificationArea" ]["FirmwareDeviceIDRecords" ][target_record ]["RecordDescriptors" ]:
79+ if 'AdditionalDescriptorType' in element and element ['AdditionalDescriptorType' ]== 'UUID' :
80+ target_desc = index
81+ index = index + 1
82+ data = output_dict ["FirmwareDeviceIdentificationArea" ]["FirmwareDeviceIDRecords" ][target_record ]["RecordDescriptors" ][target_desc ]["AdditionalDescriptorIdentifierData" ]
83+ print (data )
84+ #data = bytearray(data,"utf-8")
85+ #corrupted_data = corrupt_binary_data(data, [2, 4, 4])#function for corruption data
86+ corrupted_data = corrupt_binary_data (data )#function for corruption data
87+ #corrupted_data_hex = corrupted_data.decode("utf-8")
88+ output_dict ["FirmwareDeviceIdentificationArea" ]["FirmwareDeviceIDRecords" ][target_record ]["RecordDescriptors" ][1 ]["AdditionalDescriptorIdentifierData" ] = corrupted_data
6089 #### Data is corruptes inside output dictionary
6190
62- def image_error (output_dict ,firmware_data ,error_folder ):
91+ def image_error_old (output_dict ,firmware_data ,error_folder ):
6392 """
6493 This function injects error in the image file having ComponentIdentifier 0x70
6594 Parameter:
@@ -82,6 +111,32 @@ def image_error(output_dict,firmware_data,error_folder):
82111 with open (file_name_path ,'wb' ) as f :
83112 f .write (corrupted_data )
84113
114+ def image_error (output_dict ,firmware_data ,error_folder ):
115+ """
116+ This function randomly picks the individual image components and injects error in them
117+ Parameter:
118+ output_dict: ouptut dictionary having all the values
119+ firmware_data:Pldm firmware package
120+ error_folder:output error folder
121+ """
122+ error_folder = str (error_folder )
123+ image_information_index = 0
124+ for image_information_index in range (0 ,len (output_dict ["ComponentImageInformationArea" ]['ComponentImageInformation' ])):
125+ file_name_version = output_dict ["ComponentImageInformationArea" ]['ComponentImageInformation' ][image_information_index ]['ComponentVersionString' ]
126+ file_name_identifier = output_dict ["ComponentImageInformationArea" ]['ComponentImageInformation' ][image_information_index ]['ComponentIdentifier' ]
127+ file_name = file_name_identifier + "_" + file_name_version + "_image.bin"
128+ image_start = output_dict ["ComponentImageInformationArea" ]['ComponentImageInformation' ][image_information_index ]['ComponentLocationOffset' ]
129+ image_end = image_start + output_dict ["ComponentImageInformationArea" ]['ComponentImageInformation' ][image_information_index ]['ComponentSize' ]
130+ image_information_index = image_information_index + 1
131+ #extarct the image data
132+ image_data = firmware_data [image_start :image_end ]
133+ mask = 0b00000010 # create a mask with a 1 bit at position 7
134+ corrupted_data = bytes ([image_data [0 ] ^ mask ] + list (image_data [1 :])) # flip the bit using bitwise XOR and create a new byte string
135+ file_name_path = error_folder + "/unpack/" + file_name
136+ #write back the corrupted data to image bin file
137+ with open (file_name_path ,'wb' ) as f :
138+ f .write (corrupted_data )
139+
85140def signkey_error (signkey_data ,error_folder ):
86141 """
87142 This function injects error to the sign key that is stored inside remaining firmware data file
0 commit comments