Skip to content

Commit

Permalink
Add solution
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouxinan committed Nov 29, 2024
1 parent dafcec2 commit 5d3706b
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 0 deletions.
34 changes: 34 additions & 0 deletions TP-Link/crack_512_rsa_solution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Crack a 512-bit RSA public key and learn the private key

## Attack Step 1: Print the number n and e from the public key file

I have prepared a [script](./read_n_e_from_rsa_key.py) for this purpose. Run this script with your 512-bit RSA public key file.

![](./print_n_e.png)

My number n in RSA is:
`10194421925714091479168644488467502646386138254367497052153922599663232361010270415141154162088882150710608345234221701696770734074532297681915467648125021`

## Attack Step 2: Factor the big number n and get p and q

Read [this blog](http://gilchrist.great-site.net/jeff/factoring/nfs_beginners_guide.html?i=1)

This step might take several days.

I have my p and q here:
`98475604330270434872663742776655274553799038562230192750546533524033149071703`
`103522308850461414952118967146955078800028111873755980559168280414306621177707`

## Attack Step 3: Reconstruct the private key with p and q

I have prepared another [script](./generate_private_key_from_p_q.py) for this purpose. Run this script with your p and q.

![](./generate_private_key.png)

## Attack Step 4: Let openssl parse this private key and produce a new private key in openssl format
You have to execute this command with openssl on Ubuntu 20.04, not on macOS.
`openssl rsa -in private_key_tplink.pem -out private_key_tplink_new.pem`

![](./show_pem.png)

Then you can switch back to the [main tutorial](./README.md)
Binary file added TP-Link/generate_private_key.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 59 additions & 0 deletions TP-Link/generate_private_key_from_p_q.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import argparse
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends import default_backend
from sympy import mod_inverse

def generate_private_key_pem(p, q, public_exponent=65537):
"""Generate a private key in PEM format from given primes p and q."""
# Calculate modulus n
n = p * q
# Calculate Euler's totient function φ(n)
phi_n = (p - 1) * (q - 1)

# Calculate private exponent d
d = mod_inverse(public_exponent, phi_n)

# Convert primes into RSA parameters
dmp1 = d % (p - 1)
dmq1 = d % (q - 1)
iqmp = mod_inverse(q, p)

# Construct the private key
private_key = rsa.RSAPrivateNumbers(
p=p,
q=q,
d=d,
dmp1=dmp1,
dmq1=dmq1,
iqmp=iqmp,
public_numbers=rsa.RSAPublicNumbers(public_exponent, n)
).private_key(default_backend())

# Serialize the private key to PEM format
pem = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()
)
return pem

def main():
parser = argparse.ArgumentParser(description="Generate an RSA private key PEM file using given primes p and q.")
parser.add_argument("p", type=int, help="First large prime number (p)")
parser.add_argument("q", type=int, help="Second large prime number (q)")
parser.add_argument("--output", type=str, default="private_key.pem", help="Output file name for the private key (default: private_key.pem)")

args = parser.parse_args()

# Generate PEM
pem = generate_private_key_pem(args.p, args.q)

# Save to file
with open(args.output, "wb") as pem_file:
pem_file.write(pem)

print(f"Private key saved to '{args.output}'")

if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions TP-Link/my_tplink_pub.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAQQDCpUvga/nk2lu+KxLsaEqrRXBZzflAJ2YYNwvSNzC8Ro+yxTJxpZ+V8qBYzZ54Kntc+Ojx0nZdR530CIdRhshd
Binary file added TP-Link/print_n_e.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions TP-Link/private_key_tplink.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-----BEGIN PRIVATE KEY-----
MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAwqVL4Gv55NpbvisS
7GhKq0VwWc35QCdmGDcL0jcwvEaPssUycaWflfKgWM2eeCp7XPjo8dJ2XUed9AiH
UYbIXQIDAQABAkBJLx89TTvTCSrPcKCX1IqmpOAeU+xwa5/eYOwH6Qpg7zr0XXJD
ab1a8gQFMpeg+E7urddKPcH207/qPiIabn6dAiEA2bc3hL64S/pX3gkFW769eIRA
nJaqY4Tqdk2oQvcD8VcCIQDk34znNwaLJtYqLjgE2dHpdJyvIrjS9Lq2UyCBRRe/
awIgMfhNc9hacIXbGQk1A6O51Sl1svsZzJG7Rr/4OLZ9Xy0CIQCHlKJcqe0Xa5c8
/4ox3XMLMAhNe20vksg0j8PWsD5kJwIhALtIHKwQ/aTpHYHnXEOjma+kkVyje3h2
s+MHzPSM+Vnx
-----END PRIVATE KEY-----
10 changes: 10 additions & 0 deletions TP-Link/private_key_tplink_new.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-----BEGIN PRIVATE KEY-----
MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAwqVL4Gv55NpbvisS
7GhKq0VwWc35QCdmGDcL0jcwvEaPssUycaWflfKgWM2eeCp7XPjo8dJ2XUed9AiH
UYbIXQIDAQABAkBJLx89TTvTCSrPcKCX1IqmpOAeU+xwa5/eYOwH6Qpg7zr0XXJD
ab1a8gQFMpeg+E7urddKPcH207/qPiIabn6dAiEA2bc3hL64S/pX3gkFW769eIRA
nJaqY4Tqdk2oQvcD8VcCIQDk34znNwaLJtYqLjgE2dHpdJyvIrjS9Lq2UyCBRRe/
awIgMfhNc9hacIXbGQk1A6O51Sl1svsZzJG7Rr/4OLZ9Xy0CIQCHlKJcqe0Xa5c8
/4ox3XMLMAhNe20vksg0j8PWsD5kJwIhALtIHKwQ/aTpHYHnXEOjma+kkVyje3h2
s+MHzPSM+Vnx
-----END PRIVATE KEY-----
20 changes: 20 additions & 0 deletions TP-Link/read_n_e_from_rsa_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from Crypto.PublicKey import RSA
import argparse

def main(filename):
try:
with open(filename, "r") as file:
key = RSA.importKey(file.read())
print("Modulus (n):", key.n)
print("Public Exponent (e):", key.e)
except FileNotFoundError:
print(f"Error: File '{filename}' not found.")
except ValueError:
print(f"Error: Could not import key from '{filename}'. Ensure it's a valid RSA public key.")

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Display RSA public key parameters (n and e).")
parser.add_argument("filename", type=str, help="Path to the RSA public key file.")
args = parser.parse_args()

main(args.filename)
Binary file added TP-Link/show_pem.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5d3706b

Please sign in to comment.