we're given a source code for the challenge : from random import randint from…
- User
we're given a source code for the challenge : from random import randint from Crypto.Util.number import getPrime, long_to_bytes as l2b from Crypto.Cipher import AES from Crypto.Util.Padding import pad from secret import FLAG from hashlib import sha256
class RSAGen: def init(self, bits): self.bits = bits
def keygen(self): p = getPrime(self.bits//2) q = getPrime(self.bits//2) self.n = p*q self.e = 0x10001 phi = (p-1)*(q-1) self.d = pow(self.e, -1, phi) return (self.n, self.e), (p, q, phi, self.d) def encrypt(self, m): return pow(m, self.e, self.n) def decrypt(self, c): return pow(c, self.d, self.n)class AESGen: def init(self, size): key1 = randint(1 << size, 1 << (size+1)) key2 = randint(1 << size, 1 << (size+1)) self.k = key1 * key2 assert 40 <= self.k.bit_length() <= 42 self.KEY = sha256(str(self.k).encode()).digest()
def encrypt(self, m): cipher = AES.new(self.KEY, AES.MODE_ECB) enc_secret = cipher.encrypt(pad(m, 16)) return enc_secretdef main(): rsa = RSAGen(1024) aes = AESGen(20) pubkey, _ = rsa.keygen() enc_aes_key = l2b(rsa.encrypt(aes.k)) enc_secret = aes.encrypt(FLAG)
with open('output.txt', 'w') as f: f.write("Bob :: Hi Alice, here is my public key:\n") f.write(f"({pubkey[0]}, {pubkey[1]})\n") f.write("Alice :: Hi Bob, here is my encrypted AES key, don't forget to sha256-hash it!\n") f.write(f"Encrypted AES Key = {enc_aes_key.hex()}\n") f.write("Bob :: Got it, here is the encrypted secret you requested:\n") f.write(f"Encrypted Secret = {enc_secret.hex()}")if name == 'main': main()
- Assistant
- User
please, provide some possible solution on Python. we have this data as well : Bob :: Hi Alice, here is my public key -> (72741423208033405403492275698762968936514657314823442485453559870105200118330405900858299998747406127848670334407387228444029070060538220448699667905891284937839901536444798774307744865631349770845980738192442639071823803272283670943732769371979418893953521892212745135191661138195973757608889180051128601323, 65537) Alice :: Hi Bob, here is my encrypted AES key, don't forget to sha256-hash it! Encrypted AES Key = 4da0230d2b8b46e3a7065f32c46df019739cc002a208cc37767a82c3e94edfc3440fa4b24a32274e35d5ddceaa33505c4f2a57281c3a5d6d4db3a0dbdbb30dbf373241319ce4a7fdd1780b6bafc86e37d283c9f9787c567434e2fc29c988fb05fd411fe4453ea40eb45fc41a423839b485e238dd2530fba284e9b07a0bba6546 Bob :: Got it, here is the encrypted secret you requested: Encrypted Secret = ce8f36aa844ab00319bcd4f86460a10d77492c060b2c2a91615f4cd1f2d0702e76b68f1ec0f11d15704ba52c5dacc60018d5ed87368464acd030ce6230efdbff7b18cba72ccaa9455a6fe6021b908dd1
- Assistant
- User
need to add extra checks for decrypted messages -- if it contains HTB{ portion
- Assistant
- User
please, not just decrypted.startswith ’HTB{' portion maybe somewhere in the text. instead, should check if text contains ’HTB{' or not
- Assistant
Understood. To check if the text contains the 'HTB{' pattern anywhere within it, we should modify the script to search for that pattern in the decrypted plaintext rather than just at the beginning. Here's the revised script performing the check:
from Crypto.Cipher import AES from Crypto.Util.Padding import unpad from hashlib import sha256 import itertools # Provided data n = 72741423208033405403492275698762968936514657314823442485453559870105200118330405900858299998747406127848670334407387228444029070060538220448699667905891284937839901536444798774307744865631349770845980738192442639071823803272283670943732769371979418893953521892212745135191661138195973757608889180051128601323 e = 65537 enc_aes_key = bytes.fromhex("4da0230d2b8b46e3a7065f32c46df019739cc002a208cc37767a82c3e94edfc3440fa4b24a32274e35d5ddceaa33505c4f2a57281c3a5d6d4db3a0dbdbb30dbf373241319ce4a7fdd1780b6bafc86e37d283c9f9787c567434e2fc29c988fb05fd411fe4453ea40eb45fc41a423839b485e238dd2530fba284e9b07a0bba6546") enc_secret = bytes.fromhex("ce8f36aa844ab00319bcd4f86460a10d77492c060b2c2a91615f4cd1f2d0702e76b68f1ec0f11d15704ba52c5dacc60018d5ed87368464acd030ce6230efdbff7b18cba72ccaa9455a6fe6021b908dd1") # Brute force 20-bit numbers for i, j in itertools.product(range(1 << 19, 1 << 20), repeat=2): k = i * j # Check if product has between 40 and 42 bits if 40 <= k.bit_length() <= 42: key = sha256(str(k).encode()).digest() cipher = AES.new(key, AES.MODE_ECB) try: # try to decrypt with the derived key decrypted = unpad(cipher.decrypt(enc_secret), 16) # Check if the decrypted message contains 'HTB{' if b'HTB{' in decrypted: print(f"Decryption successful! AES key was found: k={k}") print(f"Decrypted FLAG: {decrypted}") break except ValueError as e: # Padding is incorrect, continue trying continue - User
in itertools.product(range(1 << 19, 1 << 20) is it correct ? or it should be start_range = 1 << 20 end_range = (1 << 21) - 1 Am I missingsomething ?
- Assistant
You are correct; my apologies for the oversight. The range should start at
1 << 20and go up until(1 << 21) - 1to encompass the full range of 20-bit numbers. Here's the corrected range in the script:from Crypto.Cipher import AES from Crypto.Util.Padding import unpad from hashlib import sha256 import itertools # Provided data n = 72741423208033405403492275698762968936514657314823442485453559870105200118330405900858299998747406127848670334407387228444029070060538220448699667905891284937839901536444798774307744865631349770845980738192442639071823803272283670943732769371979418893953521892212745135191661138195973757608889180051128601323 e = 65537 enc_aes_key = bytes.fromhex("4da0230d2b8b46e3a7065f32c46df019739cc002a208cc37767a82c3e94edfc3440fa4b24a32274e35d5ddceaa33505c4f2a57281c3a5d6d4db3a0dbdbb30dbf373241319ce4a7fdd1780b6bafc86e37d283c9f9787c567434e2fc29c988fb05fd411fe4453ea40eb45fc41a423839b485e238dd2530fba284e9b07a0bba6546") enc_secret = bytes.fromhex("ce8f36aa844ab00319bcd4f86460a10d77492c060b2c2a91615f4cd1f2d0702e76b68f1ec0f11d15704ba52c5dacc60018d5ed87368464acd030ce6230efdbff7b18cba72ccaa9455a6fe6021b908dd1") # Brute force 20-bit numbers start_range = 1 << 20 end_range = (1 << 21) - 1 for i, j in itertools.product(range(start_range, end_range + 1), repeat=2): k = i * j # Check if product has between 40 and 42 bits if 40 <= k.bit_length() <= 42: key = sha256(str(k).encode()).digest() cipher = AES.new(key, AES.MODE_ECB) try: # try to decrypt with the derived key decrypted = unpad(cipher.decrypt(enc_secret), 16) # Check if the decrypted message contains 'HTB{' if b'HTB{' in decrypted: print(f"Decryption successful! AES key was found: k={k}") print(f"Decrypted FLAG: {decrypted}") break except ValueError as e: # Padding is incorrect, continue trying continue - User
or is it should be start_range = 1 << 20 end_range = 1 << 21 ?? what is correct ?
- Assistant
The correct range to iterate over all 20-bit numbers should include the number that has a "1" followed by 20 zeros in binary, up to the number that has "1" followed by 20 ones in binary. This represents the range from (2^{20}) (inclusive) to (2^{21}) (exclusive). The reason the end range is exclusive is that the
range()function in Python does not include the endpoint.Here are the correct start and end values for a full range of 20-bit numbers using bitwise operations in Python:
start_range = 1 << 20 # This is 2^20, the smallest 20-bit number end_range = 1 << 21 # This is 2^21, the smallest 21-bit number (not part of the range itself)Thus, when using the
range()function in a loop, you do not subtract 1 fromend_rangebecauserange()already generates numbers up to but not includingend_range. Your loop should look like this:for i, j in itertools.product(range(start_range, end_range), repeat=2):This will iterate over all combinations of two numbers where both
iandjare 20-bit numbers, starting from (2^{20}) to (2^{21}-1) inclusive. - Assistant
It seems that your message did not come through. If you need further assistance or clarification on the topic we were discussing or if you have another question, please feel free to send it again, and I'll be happy to help!
- User
sorry. how to pause this bruteforcing code on python every 30 minutes and just pause it for 3 or 5 seconds. to chill CPU a bit
- Assistant
To add a pause every 30 minutes for a brief amount of time (e.g., 3 seconds), you can use the
timemodule in Python to track the elapsed time and sleep accordingly. Here's an example of how you can modify the brute-forcing loop to include such a pause:import time # ... (Other code before the loop) start_range = 1 << 20 end_range = 1 << 21 pause_interval = 30 * 60 # 30 minutes pause_duration = 3 # 3 seconds start_time = time.time() # Record the start time for i, j in itertools.product(range(start_range, end_range), repeat=2): # ... (Brute-forcing code) # Check if it's time to pause current_time = time.time() if current_time - start_time >= pause_interval: print("Pausing for a bit to chill the CPU...") time.sleep(pause_duration) # Pause for 3 seconds start_time = current_time # Reset the start timeThe
start_timevariable holds the timestamp when the loop started or when the last pause ended. The is checked each iteration to see if it has been 30 minutes since . If so, the script prints a message indicating a pause, then the function pauses the script for 3 seconds. After the pause, is reset to the current time, so the next pause will occur after another 30 minutes. - User
what if i wan't to pause bruteforcing , save values of the keys k = i * j and maybe later restore them if i wish.. Howto do this in Python ??
- Assistant
- User
can you spot other solution instead ofbrutforcing AES key ? Any possibility to extract RSA private key ?
- Assistant
In typical RSA usage, extracting the private key from the public key is computationally infeasible due to the difficulty of factoring large semiprime numbers (numbers with exactly two prime factors). However, given the specific setup of the AES key generation in the provided challenge, where the AES key (
k) is known to be the product of two randomly generated 20-bit numbers, one potential solution could be to use the known properties of the RSA system and a vulnerability arising from small factors in the key generation. - User
Coppersmith's attack on RSA? is it feasible here?
- Assistant
Coppersmith's attack can indeed be an effective method for attacking RSA in specific scenarios, particularly when the public exponent
eis small and a plaintext messagemis small or has known high-order bits. However, it is most effective when the plaintext message is close to a small multiple of the RSA modulus 's eth root.