What this code is doiing? Part of a challenge : import time import numpy as np import…
- User
What this code is doiing? Part of a challenge : import time import numpy as np import socket import base64
This might be useful with the exploitation of the device at some point!
#import lascar
HOST = '0.0.0.0' # This must be changed to the corresponding value of the live instance PORT = 1337 # This must be changed to the corresponding value of the live instance
This function is used to decode the base64 transmitted power trace (which is a NumPy array)
The function should only be called for the response of the 1. option and on the data received
after we send the plaintext (as seen in the example code below)
def b64_decode_trace(leakage): byte_data = base64.b64decode(leakage) return np.frombuffer(byte_data) # convert binary data into a NumPy array
This function is used to communicate with the remote machine (Laptop-2) via socket
def connect_to_socket(option, data): # Initialize a socket connection with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((HOST, PORT))
resp_1 = s.recv(1024) s.sendall(option) resp_2 = s.recv(1024) # Receive response # Send the data # option one: binary plaintext # option two: Hex encoded AES KEY s.sendall(data) # Receive response # option one: receive base64 encoded binary data # that represented the power traces as a Numpy array # option two: receive an ASCII Message # (if the key is correct the flag will be returned) resp_data = b'' while True: temp_data = s.recv(8096) if not temp_data: break resp_data += temp_data s.close() # The print commands can be used for debugging in order to observe the responses # The following print commands can be commented out. print(resp_1.decode('ascii')) print(option) print(resp_2.decode('ascii')) print(data) #print(resp_data) return resp_dataSample binary plaintext
plaintext = b'0123456789ABCDEF'
Example use of option 1
print("Option 1:") leakage = connect_to_socket(b'1', plaintext) power_trace = b64_decode_trace(leakage)
print("Length of power trace: {}".format(power_trace.shape)) #print(power_trace) # Outputs the NumPy array that represents the power trace.
Always use a delay between each connection
in order to have a stable connection
time.sleep(0.1)
Sample HEX encoded AES KEY
KEY = b'00112233445566778899AABBCCDDEEFF'
print("\nOption 2:")
Example use of option 2
response = connect_to_socket(b'2', KEY) print(response)
- Assistant
- User
The web service let the attackers to enter a plaintext and return the power trace of the secure device while encrypting the plaintext.
Remote Lab Interface:
- Encrypt plaintext (Returns power trace)
- Verify key (Returns ASCII Text) Select: 1 Encrypt a 16 bytes long plaintext:
The encryption method used by the secure device is standard AES with 128-bit key. To obtain the key, a differential power analysis (DPA) attack can be performed. The attack can be divied into 2 stages:
- Traces collecting stage
- Analysis stage Around 1000 traces of random plaintext needs to be obtained first using the web service provide. Please helpme with that on Python and save results into the file.
- Assistant
- User
- Analysis stage after collecting traces. A DPA attack targeting the LSB of the output of the first sub-byte needs to be performed. Please help me with this with Python script !
- Assistant
- User
Then in the first script i need to store generated corresponding plaintexts intoanother file and then load them from the second script correspondingly.. and where I get AES S-boxvalues from ? I don't have it. We need to use DPA to extract a whole AESkey.
- Assistant
- User
Please help me to Extending to Full Key Analysis script .. i'm slightly lost !
- Assistant
- User
dpa-full.py", line 80, in recovered_key = full_key_recovery() File "dpa-full.py", line 73, in full_key_recovery key_byte = dpa_for_key_byte(traces, plaintexts, byte_position) File "dpa-full.py", line 50, in dpa_for_key_byte plaintext_byte = plaintexts[trace_index][byte_position] IndexError: index out of range
- Assistant
- User
plaintexts all fine >>> print(traces.shape, len(plaintexts), len(plaintexts[16])) (985, 1042) 985 16 Even if i do def hypothesis(plaintext, key_guess): # Assuming you’re targeting the first byte of the plaintext and key return s_box[plaintext[15] ^ key_guess] I get IndexOut of range... maybe s_box is incomplete and it cause problems ?
- Assistant
- User
how to save array like plaintexts = [] into file on python3 ?
- Assistant
- User
how ot view .ipynb files on linux ?
- Assistant