StackZero
  • Homepage
  • Cryptography and Privacy
  • Ethical Hacking
  • Reverse Engineering
  • Contacts
  • About Me
No Result
View All Result
StackZero
No Result
View All Result

Introduction to SSRF Exploitation: A Practical Tutorial for Ethical Hackers

February 17, 2023
in Ethical Hacking
0 0
Introduction to SSRF Exploitation: A Practical Tutorial for Ethical Hackers
0
SHARES
839
VIEWS
Share on FacebookShare on Twitter

Server-side request forgery (SSRF) is a type of web application vulnerability that allows an attacker to send a crafted request from a vulnerable web application to an arbitrary destination. This can include internal systems, such as a database server or a file server.

The vulnerability occurs when the web application takes an unsanitized user-supplied input and uses it to build a request.
In this way is possible to craft a malicious request and force the vulnerable server to send it.

For example, imagine a web application that:

  • takes a URL as an input
  • displays the content of the associated web page.

Suppose the web application does not properly validate or sanitize the user input. In that case, an attacker could enter a URL that points to an internal system and then retrieve sensitive information.

Table of Contents

Toggle
  • What is the difference between CSRF and SSRF?
    • SSRF VS CSRF in a nutshell:
  • What are the few preventive measures against SSRF attacks?
  • Server-Side Request Forgery attack by example
    • The SSRF-vulnerable server
    • The Internal Service
    • Exploit SSRF
  • Conclusion

What is the difference between CSRF and SSRF?

It often turns out that the difference between SSRF and CSRF is not clear enough. However, in this paragraph, I will do my best to make it as plain as possible.
Let’s waste no more time and see how the two vulnerabilities in question differ and what are their impacts.

CSRF allows an attacker to send a malicious request to a web application on behalf of a victim. And the attacker typically follows the steps :

  • Tricks the victim into clicking a link or loading a malicious website that sends the request.
  • That request contains the victim’s authentication credentials, so the target web application treats it as if it came from the victim.
  • This can allow the attacker to perform actions on the web application as if he were the victim, such as:
    • Changing passwords
    • Making unauthorized purchases.
    • Post something on the victim’s socials.

SSRF, on the other hand, is a vulnerability that allows an attacker to send a request from a vulnerable web application to an arbitrary destination. This can include internal systems that are not normally accessible from the internet. The vulnerability occurs when the web application takes a plain user’s input to construct a request to another server or service.

SSRF VS CSRF in a nutshell:

Just to further clarify the difference:

  • CSRF: the target is a user/client and he has to perform some actions, to become a victim. The vulnerability does not depend directly on him, but on the server hosting a potentially vulnerable application.
  • SSRF: a hacker can use the application itself to send malicious requests from a trusted domain. In this case, the vulnerability does not depend on external factors but directly on the application’s code.

What are the few preventive measures against SSRF attacks?

Once we’ve seen how the vulnerability works, maybe you want to know how to prevent it.
There are several preventive measures that can be taken to protect against Server-Side Request Forgery (SSRF) attacks:

  1. Input validation and sanitization: Ensure that all user-supplied input is properly validated and sanitized before using it to construct a request.
  2. Limit the scope of requests: Limit the scope of requests that the web application can make to internal systems and services. For example by limiting the IP addresses or domain names that the application can connect to.
  3. Network monitoring: Monitor network traffic to detect and block suspicious activity. This can include monitoring for unexpected connections to internal systems, or for large volumes of requests to a single destination.
  4. Use of security scanner: Use a security scanner to identify the vulnerability and penetration test the web application to identify the vulnerability and fix them.
  5. Provide training for developers: Provide training for developers to ensure that they understand the risks associated with SSRF and know how to properly validate and sanitize user-supplied input.

It’s important to note that SSRF is a complex vulnerability that can be difficult to detect and prevent.
So an IT worker has to stay up-to-date with the latest security practices and tools to protect against these types of attacks.

Server-Side Request Forgery attack by example

In this short hands-on, we will perform a very simple exploit on a badly coded server. This is not a real situation but can help a lot to understand the vulnerability.

What I’m going to do is prepare our Kali Machine in VirtualBox in a way that we have two servers running:

  • The first one is the vulnerable application (inside the project’s main directory).
  • The second one is a server that shows the content of a secret directory and is only visible from localhost (runs inside a secret directory).

Now let’s create our directories and create the following structure in your lab:

directory tree

If you have doubts you can find the whole code in the GitHub repository.

The SSRF-vulnerable server

Before writing the code of the vulnerable server we need the IP address, so let’s type on a terminal:

ifconfig

ip address

After taking the IP we can write the code of a vulnerable application.
For example, if it was 192.168.1.116 this will be the related code to run a server in LAN on port 8000.


from flask import Flask, request
import requests


app = Flask(__name__)

my_ip = '192.168.1.116'

@app.route("/")
def index():
    url = request.args.get("url")
    response = requests.get(url)
    return response.text


if __name__ == "__main__":
    app.run(host=my_ip, port=8000)

Let’s comment a bit on this code, even if it’s self-explanatory.

This application has a single endpoint that takes a URL as a query parameter and makes a GET request to that URL using the requests library. The response from the request is then returned to the user.

In this way, we can force the webserver to show us what it can see, even if we don’t have the rights.

We can run the application by typing in our terminal:

python vulnerable_application.py

ssrf vulnerable server

If you don’t have flask installed, you should do it with the command:

pip install flask

The vulnerability in this application is that it allows an attacker to craft a URL that points to an internal service but let’s see how to do it.

The Internal Service

I want to make this introduction as simple as possible, so to run an internal service I’m going to use a python internal module: http.server.
We want to run it on localhost on port 8888:

  1. Open a terminal or command prompt.
  2. Navigate to the directory “supersecretdir” that contains the flag.
  3. Type the following command to start the server:
python -m http.server 8888
Secret server run

Exploit SSRF

It’s time to exploit, and if everything is clear, you could do it autonomously. However, I’m going to show the steps:

  • Move on your host machine
  • Open the browser
  • Try to connect to this URL: http://192.168.1.116:8000?url=http://localhost:8888

And that’s what we get!

ssrf exploited successfully

So we got access to an internal server that we couldn’t normally access!
As we expected the exploit is working fine!

Conclusion

We have seen just a simple proof of concept even though in real-world scenarios, the vulnerability could be more complex to exploit and prevent. However, I hope this article has been interesting and has provided value to those looking to learn more about Server Side Request Forgery (SSRF).

With the increasing prevalence of web applications and cloud infrastructure, it is more important than ever to be aware of the potential security vulnerabilities posed by SSRF attacks. By understanding the basics of SSRF, its impact, and how to prevent it, we can better protect our systems and data from malicious actors. Remember, prevention is always better than reaction, and by taking proactive steps towards securing our web applications, we can avoid potentially disastrous consequences down the line.

If you found this article informative, then I invite you to follow my blog for more insights on cybersecurity.
Stay up-to-date on the latest trends and techniques to protect your systems from potential threats. Don’t hesitate to leave a comment or share this article with others who may find it useful.
Thank you for reading and we look forward to seeing you on the blog!

Tags: application securitycybersecurityssrfvulnerable applicationweb application securityweb exploitationweb securityxss
Previous Post

Hack File Inclusion in DVWA: A Full Walkthrough

Next Post

Substitution ciphers? An overview of the basics

Next Post
Substitution ciphers? An overview of the basics

Substitution ciphers? An overview of the basics

You might also like

Cryptographic functions

Cryptographic Hash Functions in Python: Secure Your Data Easily

November 3, 2024
Malware Obfuscation Techniques: All That You Need To Know

Malware Obfuscation Techniques: All That You Need To Know

March 25, 2024
How To Do Process Enumeration: An Alternative Way

How To Do Process Enumeration: An Alternative Way

March 4, 2024
How To Do DLL Injection: An In-Depth Cybersecurity Example

How To Do DLL Injection: An In-Depth Cybersecurity Example

February 8, 2024
Process Injection By Example: The Complete Guide

Process Injection By Example: The Complete Guide

January 24, 2024
How To Build Your Own: Python String Analysis for Malware Insights

How To Build Your Own: Python String Analysis for Malware Insights

November 10, 2023

StackZero

StackZero is a specialized technical blog dedicated to the realm of cybersecurity. It primarily provides insightful articles and comprehensive tutorials designed to educate readers on developing security tools. The blog encompasses a broad spectrum of subjects, starting from the foundational principles of cryptography and extending to more sophisticated areas such as exploitation and reverse engineering. This makes StackZero an invaluable resource for both beginners and professionals in the field of cybersecurity.
The blog covers a wide range of topics, from the basics of cryptography to the more advanced topics of exploitation and reverse engineering.

Tags

application security blind sqli blind sql injection bruteforce c cesar cipher command injection cryptography ctf cybersecurity debugging dom-based xss dvwa ethical-hacking ethical hacking exploitation file inclusion gdb hacking injection javascript malware malware analysis malware evasion network-security pentesting lab picoctf pico ctf python reflected xss reverse engineering sql sqli sql injection static analysis stored xss substitution substitution cipher vulnerable application web application security web exploitation web security windows windows api xss
  • About Me
  • Contacts
  • HomePage
  • Opt-out preferences
  • Privacy Policy
  • Terms and Conditions

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Manage Cookie Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
View preferences
{title} {title} {title}
No Result
View All Result
  • Homepage
  • Cryptography and Privacy
  • Ethical Hacking
  • Reverse Engineering
  • Contacts
  • About Me