Breaking RSA Algorithm — Fermat’s Surprise (2024)

Breaking RSA Algorithm — Fermat’s Surprise (2)

Recently I was doing a CTF challenge wherein the objective was to compromise the integrity of the RSA algorithm. Central to the challenge was Fermat’s Factorization Method and its deadly application to bring down a modern cryptographic system. I would like to point out that the compromise of RSA happens only when it is not properly implemented. Specifically, when the prime numbers (p, q) that make up the RSA keys are not sufficiently spaced apart. In this limiting scenario, Fermat’s Factorization Method can completely compromise the integrity of RSA.

In this article I wanted to take the readers on a cryptographic journey into the workings of RSA and watch for themselves how it can be broken with this 300 year old factorization method! I have structured this article into 3 broad sections.

  1. The Background — In a Nutshell
  2. Breaking RSA
  3. References

In The Background we will cover some of the very basic mathematics that goes with RSA and also understand specifically what this vulnerability is. This is a theoretical section and in case if people want to get in on the action they can move to the second section directly.

In Breaking RSA we will have a look at a deliberately vulnerable application that I have designed for this tutorial. We will go over the vulnerability and exploit it to gain a reverse shell on the containerized application.

Finally, in the References section, I will give all the supporting material for this article. In fact, this vulnerability has been documented as CVE-2022–26320 and is chiefly the work of a gentleman by the name Hanno Bock. I did uncover some great resources while working on this article and links to all of those will be shared here in this section.

RSA is an asymmetric cryptographic system that consists of a Public Key and a Private Key. Encryption is done via the public key while decryption is achieved through the private key. The security resilience in RSA is achieved because of the inherent difficulty in factorizing very large numbers into their constituent prime factors. As an example, consider n=77, which can be easily factorized into p=11 and q=7. This factorization is easy because of the small magnitudes involved.

But let us consider an n = 106,109,701. Factorizing this would not be so simple! The prime numbers that give this product are p=10,331 and q=10,271. While this example is a little tough, but it is still within the bounds of possibility to factorize n. And in fact you very easily can do this programmatically.

For an industry standard cryptosystem like RSA that has been entrusted with safeguarding the integrity of data, the standards are quite high. NIST recommends key size of 2048 bits to 4096 bits for modern systems. This means that for the lower boundary of 2048 bit key size, the value of n would be 4096 bits long! The work function or in other words the time & effort required to brute force modern crptographic systems would be in hundreds of years with those kind of key sizes.

It will not be feasible to explain RSA at length, but we will touch upon some of its salient components that would faciliate our discussion going forward. For the purpose of this article, let us lay out all the components involved in RSA cryptography.

Breaking RSA Algorithm — Fermat’s Surprise (3)

Let us go over these components in brief.

  1. p & q are very large prime numbers that are used in the public as well as private key
  2. Modulus or n is the product of p & q and is available inside the Public key that is shared with everyone
  3. Totient Function Φ(n) is derived from the value of p & q and is utilized in generating the Private key
  4. Integer e satisfies 2 conditions generally. Condition one is that 2 < e < Φ(n) and condition two is gcd(e,Φ(n)) = 1 or e and Φ(n) are co-prime. It is worth noting that the value of e is declared inside the public key.
  5. Private Key Exponent, d which is d ≡ e^(-1)(mod Φ(n)). The value of d is kept a secret and is used in decrypting cipher text received.

At this point it is not very important to understand all of the math that comes with the above elements. What is important however, is to understand the distinct components involved and which components go where i.e. which cryptographic elements are revealed in the public key and the ones that are not.

As one can see the value of n and e are revealed inside the Public Key that is shared with everyone. The other components p, q, d and Φ(n) are not shared. But a careful examination tells us that only the values of p & q are critical. If we know p & q, we can compute the values of d & Φ(n). And this is extremely important because it tells us that if we can factorize n or in other words figure out p & q, all other components can be calculated.

And hence the damage that a factorization method like Fermat’s can do becomes disinctly severe in case if it is able to crack open the factors of n. Because then all other cryptographic elements can be calculated and a private key can then be trivially generated at the attacker’s end. Keep in mind, the values of n & e are publicly available e.g., inside Public Certificates. The stage is set now for Fermat’s Theorem.

Fermat’s factorization method aims to quickly identify factors for n. And Fermat’s method is such that in case if the prime numbers (p & q) are not very far apart or the difference between them is not very high then the method can quickly identify the factors or in this case the value of p & q. The associated math for Fermat’s method can be looked at separately. I have provided the link to it in references section.

So to summarize, the security resilience of RSA depends on the difficulty in factoring large numbers. That central notion of security is challenged by Fermat’s Factorization Method for factors that are not very far apart in magnitude. Fermat’s method in such situations can effectively find factors even for very large values of n as mandated by Industry Standards like NIST. And this leads to a complete compromise of the keys because once n, e, p and q are available, all other parameters can be calculated and keys can be generated too.

Let us now shift our focus to a deliberately vulnerable web application developed for this demo. I have containerized this application and anyone reading this article can build an image on cloud or locally and work alongisde. The repository to the vulnerable web application is here.

For the sake of this writeup, I have setup the application on an AWS EC2 instance (which I will take down once my article is done). The application in itself is not the center of focus. In fact we are more interested in the certificate (TLS) that is being used in the web app.

The Certificate contains the public key, from which an attacker can trivially obtain the value of n & e. As a next step, we use Fermat’s Factorization method and try to find the prime numbers p & q that are factors of n. Since, this application is vulnerable we are able to obtain values for p & q by using the Fermat’s method.

Next, we leverage our understanding of RSA to derive remaining cryptographic elements d & Φ(n) or Totient Function. Armed with these we generate the Private Key of the certificate successfully!

Because this is a demo of the severity of this vulnerability, I thought let us take it up a notch. This private key is also the private key that is used to SSH into the container that is running this application. And as is to be expected we are running as ROOT inside the container!

One can say that it would have been more fun if the private key was used to get SSH access to the host (or EC2 in this case). And I agree, but I didn’t want to leave EC2 instances open to SSH with a method to get to their private keys even in a tutorial! Enough said, let us get to action.

When we get to the web app we can see that our browser complains because the certificate is self-signed. We will “Accept” the risk and continue.

Breaking RSA Algorithm — Fermat’s Surprise (4)

Once we do that we can see that the web app has a very basic home page.

Breaking RSA Algorithm — Fermat’s Surprise (5)

There is one more page, in the web application that contains a small note indicating the potential vulnerability that might be present.

Breaking RSA Algorithm — Fermat’s Surprise (6)

As part of reconnaissance we can also look at the certificate that is present on the website.

Breaking RSA Algorithm — Fermat’s Surprise (7)

We can see certificate and its associated details. In the Public Key info section we can see the algorithm being used along with the key size and the exponent or e value and also the modulus. As discussed earlier, these are shared as part of the public key.

As a next step, I will share the repository that contains the exploit for this vulnerability. This exploit automates the process of extracting public key information (n, e) and then it attempts to use Fermat’s Factorization method to break the algorithm.

We can provide the domain name and the script will connect with the domain and then download the certificate and public key. Post that it can attempt to use Fermat’s Factorization method to crack open the prime numbers (p & q). Let us see this in action.

Breaking RSA Algorithm — Fermat’s Surprise (8)

In our case, we don’t have a domain and hence we can provide the IP address and there is no need to define the port because it takes 443 by default. Let us run and see what happens.

Breaking RSA Algorithm — Fermat’s Surprise (9)

At this point the exploit asks whether we would like to proceed with Fermat’s Factorization Method to identify p & q. We will say “YES” to proceed with the exploit.

Breaking RSA Algorithm — Fermat’s Surprise (10)

The moment we said yes, a few things happened.

  1. It successfully factorized the modulus (n) into its prime numbers (p & q) in very quick time. Note, this happened in less than a second for a 2048 bit long key size!
  2. We see the difference between the 2 prime numbers p & q is a mere 214. This is the real culprit why Fermat’s Factorization method was able to factorize n so quickly.
  3. Remaining cryptographic components such as d & Φ(n) are computed (only d is shown here). These components help is generating the private key
  4. Finally, we also have the private key available to us. The exploit stores the key in a separate PEM file while displaying it on the STDOUT.

In a nutshell, what we saw above is a complete breakdown & compromise of the RSA implementation. Because we were able to arrive at the Private Key from publicly declared elements in extremely quick time!

The key obtained can be leveraged to SSH inside the container that is running this application. So, let us first log inside the EC2 instance or the host and then we will use the retrieved key to show that final step as well.

Breaking RSA Algorithm — Fermat’s Surprise (11)

We will use this key to log in into the container that is running at 172.17.0.2

Breaking RSA Algorithm — Fermat’s Surprise (12)

And indeed, we were able to SSH into the container from the key that we retrieved by leveraging the exploit. This completes the attack chain that we had laid out before in this section. We began by navigating to the web application and had a look at its certificate. We then used our exploit to leverage Fermat’s Factorization Method to factorize the modulus (n) and retrieve p & q. Post that, we generated the private key because we had all the cryptographic primitives required to generate the Private Key. And finally we used this private key to get (SSH) inside the container running the application.

At this point it would be worth discussing how the exploit works at a high level. There is one main component (rsaBreak.py) and it is supported by 4 other python modules that perform various tasks. One module extracts the public certificate from the domain provided and its public key components (n, e). Another module, uses the Fermat method for factorizing n into its prime numbers. There is another module that computes the private key exponent (d). And finally there is a module that uses all of the cryptographic primitives to generate the Private Key. I must confess that I have taken the logic for Fermat’s factorization & Modulo Inverse from generous donors in the GitHub community!

All of these modules tie together inside the main component from where we launch the exploit. This exploit can be used to test a key individually as well for its susceptibility to this vulnerability.

Breaking RSA Algorithm — Fermat’s Surprise (2024)

FAQs

Can RSA algorithm be broken? ›

I would like to point out that the compromise of RSA happens only when it is not properly implemented. Specifically, when the prime numbers (p, q) that make up the RSA keys are not sufficiently spaced apart. In this limiting scenario, Fermat's Factorization Method can completely compromise the integrity of RSA.

Can RSA algorithm be cracked? ›

RSA hinges on the fact that there is no polynomial time solution to number factorization problem using classical computers. In the quantum computing world we have Shor's algorithm which already would break RSA if a quantum computer of sufficient size can be built.

How long does it take to break RSA algorithm? ›

Time Required for 10^40 Operations:

So, even with the assumed computational capacity of Google's data centers, it would take approximately 19.8 quadrillion years to crack RSA-2048 using brute force. This is an astronomical time frame, far longer than the current age of the universe (which is about 13.8 billion years).

Why is RSA encryption hard to break? ›

‍RSA encryption is strong because factoring is a one-way problem. It's very easy to multiply two primes together, but very difficult to find prime factors of a large number. That's what the technology relies on.

Will quantum computers break RSA? ›

Quantum computers can break RSA encryption by finding the prime factors of the composite number that is used to generate the public and private keys. Once the prime factors are known, the private key can be easily calculated from the public key, and the encrypted messages can be decrypted.

How many qubits to break RSA? ›

The current estimate is that breaking a 1,024-bit or 2,048-bit RSA key requires a quantum computer with vast resources. Specifically, those resources are about 20 million qubits and about eight hours of them running in superposition.

Is RSA breakable? ›

Breaking RSA encryption is known as the RSA problem. Whether it is as difficult as the factoring problem is an open question. There are no published methods to defeat the system if a large enough key is used. RSA is a relatively slow algorithm.

Has RSA ever been hacked? ›

The RSA SecurID breach was a highly sophisticated cyberattack that occurred in March 2011, in which hackers accessed the computer systems of RSA, a company that provides two-factor authentication solutions to many organizations.

What algorithm breaks RSA? ›

Shor's algorithm, a quantum algorithm used to factorize large numbers, poses significant threats to RSA, a widely used public key cryptosystem. RSA relies on the difficulty of factoring large semi-primes to keep its security.

How fast can quantum computers break encryption? ›

Even if you had a quantum computer with millions of qubits (which we don't have yet), it would still take years or decades to crack 256 bit encryption.

What is the math behind RSA algorithm? ›

The RSA cryptosystem is composed of three steps: Key generation: Each user u generates two primes p,q, calculates n=pq and φ(n)=(p−1)(q−1), picks a random e (which must be relatively prime to φ(n)) and calculates d=e−1(modφ(n)). The user publishes the public key pubu=(n,e) and keeps for herself the private key priu=d.

What are the flaws of RSA algorithm? ›

Disadvantages Of RSA

Sometimes, it's necessary for a third party to confirm the dependability of public keys. Since so many people are engaged, the data transfer rate is slow. RSA cannot be used for public data encryption, such as electoral voting. Decryption requires intensive processing on the receiver's end.

Has anyone cracked RSA? ›

The team say they cracked 48-bit RSA using a 10-qubit quantum computer-based hybrid system and could do the same for 2048-bit if they had access to a quantum computer with at least 372 qubits.

What is the hardest encryption to break? ›

AES 256-bit encryption is the strongest and most robust encryption standard that is commercially available today. While it is theoretically true that AES 256-bit encryption is harder to crack than AES 128-bit encryption, AES 128-bit encryption has never been cracked.

What is the largest RSA key cracked? ›

As of 2020 the largest RSA key publicly known to be cracked is RSA-250 with 829 bits.

Is RSA encryption breakable? ›

"Breaking RSA is usually attempted by using Shor's algorithm in a quantum computer but there are no quantum computers in existence that can produce enough gates to implement Shor's algorithm that would break 2048 keys," Woodward said.

Is RSA algorithm still secure? ›

According to the National Institute of Standards and Technology recommendations, RSA encryption with 2048-bit encryption keys is safe to use until the end of 2030. While you can always choose the 4096-bit key length that would stay relevant a bit longer, longer keys are not sustainable.

Is RSA 1024 broken? ›

However, cryptography advancements and the rise of quantum computing have rendered the 1024-bit RSA keys vulnerable to cyberattacks. Continuing to use 1024-bit RSA keys for encryption increases the risk of exposing sensitive data to eavesdropping, decryption, and data breaches.

Top Articles
Advancement
Drone Photo or Video Files Export Guide
Ron Martin Realty Cam
Combat level
Danatar Gym
Midflorida Overnight Payoff Address
Ingles Weekly Ad Lilburn Ga
Craigslist Pet Phoenix
Walgreens Alma School And Dynamite
Nm Remote Access
Select The Best Reagents For The Reaction Below.
Tanger Outlets Sevierville Directory Map
Jscc Jweb
Herbalism Guide Tbc
Newgate Honda
Diablo 3 Metascore
VMware’s Partner Connect Program: an evolution of opportunities
Panorama Charter Portal
Bend Pets Craigslist
Craiglist Kpr
ARK: Survival Evolved Valguero Map Guide: Resource Locations, Bosses, & Dinos
Classic | Cyclone RakeAmerica's #1 Lawn and Leaf Vacuum
The Grand Canyon main water line has broken dozens of times. Why is it getting a major fix only now?
Craigslist Portland Oregon Motorcycles
Missed Connections Inland Empire
My Homework Lesson 11 Volume Of Composite Figures Answer Key
Selfservice Bright Lending
Craigslist Pearl Ms
Clare Briggs Guzman
Morse Road Bmv Hours
Kirsten Hatfield Crime Junkie
Hesburgh Library Catalog
208000 Yen To Usd
NV Energy issues outage watch for South Carson City, Genoa and Glenbrook
Best Town Hall 11
Mississippi Craigslist
Trust/Family Bank Contingency Plan
Datingscout Wantmatures
Ancestors The Humankind Odyssey Wikia
What Time Is First Light Tomorrow Morning
Soulstone Survivors Igg
20 Best Things to Do in Thousand Oaks, CA - Travel Lens
World Social Protection Report 2024-26: Universal social protection for climate action and a just transition
boston furniture "patio" - craigslist
Is Ameriprise A Pyramid Scheme
National Weather Service Richmond Va
Blue Beetle Showtimes Near Regal Evergreen Parkway & Rpx
412Doctors
10 Types of Funeral Services, Ceremonies, and Events » US Urns Online
The Great Brian Last
Petfinder Quiz
Nfhs Network On Direct Tv
Latest Posts
Article information

Author: Jamar Nader

Last Updated:

Views: 5603

Rating: 4.4 / 5 (55 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Jamar Nader

Birthday: 1995-02-28

Address: Apt. 536 6162 Reichel Greens, Port Zackaryside, CT 22682-9804

Phone: +9958384818317

Job: IT Representative

Hobby: Scrapbooking, Hiking, Hunting, Kite flying, Blacksmithing, Video gaming, Foraging

Introduction: My name is Jamar Nader, I am a fine, shiny, colorful, bright, nice, perfect, curious person who loves writing and wants to share my knowledge and understanding with you.