I am trying to simulate a Side Channel attack using Python.
The script bellow generates multiple strings that are used to simulate the user input and stores them in the list_of_tries.
The password is defined in the verify_password function (‘ThisIsMyPass’).
Then, it tests every possibility in the list_of_tries and gets the duration of the verification.
Finally, stores the duration and the string used, sorts the array and prints the results.
The results I am getting are different each time I execute it (nothing uncommon) but they are not what I was hoping to achieve.
Theoretically, checking the word ‘thisist00000’ takes less time than checking the word ‘thisism00000’.
In ‘thisist00000’ the loop breaks on ‘t’ because ‘t’ != ‘m’ (‘thisisMypass’).
In ‘thisism00000’ the loop breaks on ‘0’ because ‘0’ != ‘y’ (‘thisismYpass’).
This is not happening. Most of the times, ‘thisist00000’ will take way longer than ‘thisism00000’.
I have changed a single random element of list_of_tries to ‘thisismypas0’ in hope it would take the most time but that did not happen.
I tried using cryptography with the BCrypt package where I measured the time it would take to compare the password hash and the input_try hash.
Lastly, I tried the same example in C and the results were also not what I wanted.
Is there something missing in my code? Is it actually possible to simulate this? Is the CPU speed too fast to detect changes in the duration when verifying such small strings?
import string import time def verify_password(string): password = 'ThisIsMyPass'.lower() if len(password) != len(string): return 0 for i in range(len(password)): if password[i] == string[i]: continue else: return 0 return 1 def generate_input(string, letter): password_size = 12 remaining_characters = password_size - len(string) - 1 # Letter return string + letter + '0' * remaining_characters list_of_tries =  duration_res =  abc = list(string.ascii_lowercase) for letter in abc: new_input = generate_input('thisis', letter) list_of_tries.append(new_input) # list_of_tries = 'thisismypas0' print(list_of_tries) for input_try in list_of_tries: start_time = time.time() res = verify_password(input_try) time.sleep(0.1) duration = time.time() - start_time if res: print("Correct Password!") data = "%1.20f:%s" % (duration, input_try) duration_res.append(data) duration_res.sort() print(duration_res) print_res =  for i, item in enumerate(duration_res): print_res.append(item.split(':')) print("Result:", print_res)
Source: Python Questions