Breaking RSA

Hop in and break poorly implemented RSA using Fermat's factorization algorithm.

Let's start with a simple nmap scan.

┌──(kali㉿kali)-[~]
└─$ nmap -sT -p- 10.10.251.239 -T4          
Starting Nmap 7.94SVN ( https://nmap.org ) at 2024-02-19 00:51 EST
Warning: 10.10.251.239 giving up on port because retransmission cap hit (6).
Stats: 0:18:56 elapsed; 0 hosts completed (1 up), 1 undergoing Connect Scan
Connect Scan Timing: About 92.68% done; ETC: 01:12 (0:01:30 remaining)
Nmap scan report for 10.10.251.239
Host is up (0.15s latency).
Not shown: 65530 closed tcp ports (conn-refused)
PORT      STATE    SERVICE
22/tcp    open     ssh
80/tcp    open     http
27567/tcp filtered unknown
31710/tcp filtered unknown
36144/tcp filtered unknown

Nmap done: 1 IP address (1 host up) scanned in 1247.83 seconds

We can see there are 2 open ports, SSH on port 22 and a web server on port 80.

Let's enumerate the directories now. We are using Gobuster.

We have found only one directory i.e: /development.

Let's visit the web server now.

Nothing to go one here. Let's visit /development.

There are two files as we can see.

  • id_rsa.pub

  • log.txt

The log.txt re iterates what is already mentioned in the description of the room.

We know from the challenge that we are dealing with a weak RSA implementation. We will most likely be to extract N and e from the public key in order to factorize N, getting p and q, and finally calculate d. The value d is the private key. If we have N, e, and d, we can generate the private SSH key and then access the machine.

Let's download the public key.

We can use the ssh-keygen utility to find the length of the discovered RSA key.

The following source gives us an example of how the parameters N and e can be retrieved from the public key using Python.

The challenge gives us a direct indication of how to calculate p and q in this case of weak implementation. We have to use Fermat's Factorization method. If we have p and q, we can calculate the private key d via the inverse of e. The challenge room also gives us an implementation of Fermat's Factorization algorithm in python.

Now we have to generate the private SSH key from our values of the parameters N, e, and d. With the help of the followig source:

After being held at this point for quite some time. I eventually reffered to other write ups and this one helped the most with implementing the code of fermat factorization. I recommend giving it a read for a much more detailed understanding.

We use gmpy2 to calculate with large values.

It is now time to run our finished script.

After running the script we answer question 4 and 6. ie

We can SSH in as root and we get our flag.

Last updated

Was this helpful?