The Wayback Machine - https://web.archive.org/web/20241005034917/https://www.geeksforgeeks.org/use-sqlmap-test-website-sql-injection-vulnerability/
Open In App

How to use SQLMAP to test a website for SQL Injection vulnerability

Last Updated : 02 May, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

This article explains how to test whether a website is safe from SQL injection using the SQLMAP penetration testing tool. 

What is SQL Injection?

SQL Injection is a code injection technique where an attacker executes malicious SQL queries that control a web application’s database. With the right set of queries, a user can gain access to information stored in databases. SQLMAP tests whether a ‘GET’ parameter is vulnerable to SQL Injection. 

For example, Consider the following php code segment: 

$variable = $_POST['input'];
mysql_query("INSERT INTO `table` (`column`) VALUES ('$variable')");

If the user enters “value’); DROP TABLE table;–” as the input, the query becomes 

INSERT INTO `table` (`column`) VALUES('value'); DROP TABLE table;--')

which is undesirable for us, as here the user input is directly compiled along with the pre-written sql query. Hence the user will be able to enter an sql query required to manipulate the database. 

Where can you use SQLMAP?

If you observe a web url that is of the form http://testphp.vulnweb.com/listproducts.php?cat=1, where the ‘GET’ parameter is in bold, then the website may be vulnerable to this mode of SQL injection, and an attacker may be able to gain access to information in the database. Furthermore, SQLMAP works when it is php based. 

A simple test to check whether your website is vulnerable would be to replace the value in the get request parameter with an asterisk (*). For example, 

http://testphp.vulnweb.com/listproducts.php?cat=* 

If this results in an error such as the error given above, then we can conclusively say that the website is vulnerable. 

 Installing sqlmap

SQLMAP comes pre-installed with kali Linux, which is the preferred choice of most penetration testers. However, you can install sqlmap on other debian based linux systems using the command 

 sudo apt-get install sqlmap 

Usage

In this article, we will make use of a website that is designed with vulnerabilities for demonstration purposes: 

 http://testphp.vulnweb.com/listproducts.php?cat=1 

As you can see, there is a GET request parameter (cat = 1) that can be changed by the user by modifying the value of cat. So this website might be vulnerable to SQL injection of this kind. 
To test for this, we use SQLMAP. To look at the set of parameters that can be passed, type in the terminal, 

 sqlmap -h 

The parameters that we will use for the basic SQL Injection are shown in the above picture. Along with these, we will also use the –dbs and -u parameter, the usage of which has been explained in Step 1. 
Using SQLMAP to test a website for SQL Injection vulnerability: 

  • Step 1: List information about the existing databases 
    So firstly, we have to enter the web url that we want to check along with the -u parameter. We may also use the –tor parameter if we wish to test the website using proxies. Now typically, we would want to test whether it is possible to gain access to a database. So we use the –dbs option to do so. –dbs lists all the available databases. 
 sqlmap -u http://testphp.vulnweb.com/listproducts.php?cat=1 --dbs 

  • We get the following output showing us that there are two available databases. Sometimes, the application will tell you that it has identified the database and ask whether you want to test other database types. You can go ahead and type ‘Y’. Further, it may ask whether you want to test other parameters for vulnerabilities, type ‘Y’ over here as we want to thoroughly test the web application. 

  • We observe that there are two databases, accurate and information_schema  
  • Step 2: List information about Tables present in a particular Database 
    To try and access any of the databases, we have to slightly modify our command. We now use -D to specify the name of the database that we wish to access, and once we have access to the database, we would want to see whether we can access the tables. For this, we use the –tables query. Let us access the accurate database. 
 sqlmap -u http://testphp.vulnweb.com/listproducts.php?cat=1 
-D acuart --tables 

Tables

  • In the above picture, we see that 8 tables have been retrieved. So now we definitely know that the website is vulnerable. 
  • Step 3: List information about the columns of a particular table 

    If we want to view the columns of a particular table, we can use the following command, in which we use -T to specify the table name, and –columns to query the column names. We will try to access the table ‘artists’. 

 sqlmap -u http://testphp.vulnweb.com/listproducts.php?cat=1 
-D acuart -T artists --columns 

Columns

  • Step 4: Dump the data from the columns 
    Similarly, we can access the information in a specific column by using the following command, where -C can be used to specify multiple column name separated by a comma, and the –dump query retrieves the data 
 sqlmap -u http://testphp.vulnweb.com/listproducts.php?cat=1
-D acuart -T artists -C aname --dump 

  • From the above picture, we can see that we have accessed the data from the database. Similarly, in such vulnerable websites, we can literally explore through the databases to extract information  

Prevent SQL Injection

SQL injection can be generally prevented by using Prepared Statements . When we use a prepared statement, we are basically using a template for the code and analyzing the code and user input separately. It does not mix the user entered query and the code. In the example given at the beginning of this article, the input entered by the user is directly inserted into the code and they are compiled together, and hence we are able to execute malicious code. For prepared statements, we basically send the sql query with a placeholder for the user input and then send the actual user input as a separate command. 
Consider the following php code segment. 

$db = new PDO('connection details');
$stmt = db->prepare("Select name from users where id = :id");
$stmt->execute(array(':id', $data));

In this code, the user input is not combined with the prepared statement. They are compiled separately. So even if malicious code is entered as user input, the program will simply treat the malicious part of the code as a string and not a command. 

Note: This application is to be used solely for testing purposes

Related Article 
Basic SQL injection and mitigation 

Reference:stackoverflow.com 

 



Similar Reads

Basic SQL Injection and Mitigation with Example
SQL injection is a code injection technique, used to attack data driven applications, in which malicious SQL statements are inserted into an entry field for execution (e.g. to dump the database contents to the attacker). SQL Injection can be used in a range of ways to cause serious problems. By levering SQL Injection, an attacker could bypass authe
4 min read
What is Apache Log4j Vulnerability?
On December 9th, 2021, the world was made aware of the single, biggest, most critical vulnerability as CVE-2021-44228, affecting the java based logging utility log4j. This vulnerability was reported to apache by Chen Zhaojun of the Alibaba cloud security team on 24th November 2021 and published in a tweet on 9th December 2021. Apache software found
4 min read
Spectre Security Vulnerability
What is Spectre security vulnerability? Spectre is a security vulnerability that affects all modern processors that use mechanisms such as branch prediction and speculative action. Spectre attacks involve inducing a victim to speculatively perform operations that would not occur during correct program execution and which leak the victim’s confident
4 min read
How to Use WordPress: Complete Guide to Make a Website Using WordPress
Have you ever wondered how to bring your ideas, your passion, or your business to life on the vast world of the internet?  Perhaps you’ve dreamt of creating your very own website but felt overwhelmed by the technical jargon and the seemingly endless options out there. Creating a website using WordPress is a relatively straightforward process.  Word
12 min read
10 Best Website Analytics Tools to Use in 2024
Website analytics tools are powerful resources that track website traffic, analyze user behavior, and optimize digital marketing efforts. These tools provide insights into what users are doing on your site—what pages they visit, how long they stay, and what actions they take. This information is essential for improving user experience, increasing c
12 min read
How to Prepare for eLitmus Hiring Potential Test (pH Test)?
eLitmus is one of the biggest online hiring platforms and a national-level assessment for all those out there looking out for entry-level engineering jobs. eLitmus pH test Scorecard is used by companies to shortlist students for Interviews in Off-Campus and On-Campus Placements. Candidates can also use this score to apply to all participating (arou
4 min read
SQL vs NoSQL: Which One is Better to Use
SQL and NoSQL are two fundamental types of database systems designed for different data management needs. SQL databases are known for their structured, table-based format and strong data integrity features while NoSQL databases offer flexibility and scalability making them suitable for handling large volumes of unstructured data. In this article, W
5 min read
Making your WordPress Website More Secure
Why security is Needed? Blogging now a days has become a hobby of so many people and Wordpress is the most opted platform for the same.By default, wordpress blogs employ a minimal level of security and also its files and/or plug-ins may often be outdated. These files are traceable and vulnerable to be hacked easily. Internet is not a secure place t
5 min read
8 Ways to Shipwreck Your Next Website Design
A website has a small time to capture the interest of users. A growing age of technology in which a lot of websites are available for a particular theme, so why should one choose the old and boring website. What your website needs a bit more than the information. It needs an attractive appearance that will bring it in the modern age and make it rea
5 min read
Top 7 Code Sharing Website For Developers
Building an application is quite challenging for developers especially when you are a new coder or you are a solo coder. Quite often developers get stuck in their projects due to some errors. Sometimes it's also difficult to build some specific features and there you just want readymade code snippets. In all these cases, you may need help from your
7 min read
Ways to track a Website
Whenever we visit any website, our web browser reveals our location, our search and browsing history, etc. These data further may be used by third parties. Tracking is generally used by various networks to build up a detailed profile for various purposes such as effecting political choices. There are many ways to track a website which includes: Ip
4 min read
How to Launch a WordPress Website using Amazon EC2 Server ?
What is Amazon web service (AWS)? It is a secure cloud services platform. It offers services like compute power, database storage, content delivery, and other functionality to help businesses scale and grow. What is Amazon EC2? Amazon Elastic Compute Cloud (EC2) is an Cloud Based Services IaaS (Infrastructure as a Service) type of cloud service tha
3 min read
How to Create a Google Calendar and Embed into your website ?
Google Calendar is used to quickly schedule meetings and events and also used to get reminders about upcoming activities. Calendar is designed for teams, so it is easy to share your schedule with others and create multiple calendars that you and your team can use together. Here are the steps to add a Google calendar to your website. Step 1: Go to t
1 min read
Important Reasons for Selecting Weebly for Building a Website
Weebly runs 40 million websites worldwide. More than twelve million registered users are associated with it. It is simple and easy to use website designing tools available on the web. You can develop and publish your site online in a short time with Weebly without holding technical expertise. Below are some reasons why people prefer to select to bu
5 min read
7 main components of a website
The following are the 7 main components of a website. Navigation : The navigation is one of most significant pieces of a site, as it causes clients to handily explore whole webpage and encourages web index to get a thought regarding structure of site. Complex navigation can without much of a stretch lead to bring down traffic and higher skip rates.
4 min read
7 Tips to Protect your Website from Hackers
Do you have a website? It could be a business website where you promote and provide all the details of your business. It could be a personal website where you teach a skill to students. It could even be a blog where you document what you are eating right now! Who knows?!! But the point is that if you have a website, it must be protected against hac
7 min read
Tips for Website Building - From Development to Monetization Phase
When we speak about web development, we usually cover a vast array of topics that include what technology stack we need to use to build an end-to-end website, how and where to host the website, how to maintain it and keep updating it regularly, various Search Engine Optimization (SEO) techniques to improve the ranking of the website in search engin
5 min read
Website Hosting and Premium Domain for Free
Let's look at how you can host your website for free and connect a domain name (premium) to that for free. Before proceeding further, make sure that you have a website ready to host since it will be helpful to try it out along the way. You will be needing at least an index.html file, which can be hosted. The article can be divided into two parts. T
8 min read
Things to Keep in Mind To Improve Your Website Performance
There are more than a billion websites available on the World Wide Web and the number still continues to grow every day. While there are websites that get a lot of hits thanks to their user-friendliness, there are also websites that make the users puzzled, and thus most of them never visit again. What was missing in those websites that made the use
7 min read
Top 12 jQuery Plugins For Enhancing a Website
JQuery plugins are a popular choice in front-end development for years. They help simplify work by providing features that otherwise would have taken a lot of time to learn and implement. As the years go by, many new plugins have been made to make websites more interactive with less work involved in their development. This has made the lives of man
4 min read
Common Website Navigation Mistakes You Should Avoid
Website navigation is basically a process of navigating website and is an essential part of website design. Therefore, website navigation is very important when it comes to explore brand. It should be easy for users or visitors to navigate through website easily. Whenever users visit website, one need to make sure that visitor or user should be abl
4 min read
How to Add Your Website to Google Analytics ?
Google provides some wonderful services to webmasters which definitely helps in managing a web application or a static website. It provides Google AdWords Keyword Planner, Google Search Console, PageSpeedTest by Google, Google AdSense, and also Google Analytics is one of those. After acquiring the Urchin Software Corp. in 2005, Google has launched
3 min read
What is a Website ?
A website is a collection of many web pages, and web pages are digital files that are written using HTML(HyperText Markup Language). To make your website available to every person in the world, it must be stored or hosted on a computer connected to the Internet round a clock. Such computers are known as a Web Server. The website's web pages are lin
5 min read
Key Things to Remember Before You Migrate Your Website To Another Technology
We all know that technology is evolving every day. Every day new technologies or frameworks are coming out in the market. Old ones are becoming obsolete and these new technologies are becoming the replacement of old ones. To survive in today's market companies are moving to these new platforms or tech stacks. A company can always migrate to a bette
6 min read
How to Deploy website on Netlify ?
You have your website ready and don't know how to reach the audience. Most of us coders create amazing websites using HTML, CSS, and JavaScript and just save them locally or on remote host services like GitHub and BitBucket. Now, what if you need non-programmers to view your work? This is why it becomes too important to deploy your web application
2 min read
Why Should You Build a CRM-Powered Website For Your Business?
As per the survey, currently, the total number of websites on the internet is 1.95 billion. But most businesses tend to have a simple and static website that provides core information about their products and services. These websites offer relatively less information as compared to advanced websites. These simple websites may lead to the loss of bu
5 min read
Why do you need a Responsive Website?
Mobile internet usage is growing at a steady pace, so much so that it is expected to overtake desktop browsing as early as 2016. It means that it has become more than necessary for website owners to think about channelling their work into designing mobile-friendly, handheld device-compliant websites. What is a Responsive Website?A responsive websit
5 min read
Top 15 AI Website Builders For UI UX Designers
Artificial intelligence (AI) website builders are a new breed of tools that have been created as a result of the confluence of AI and UI/UX design. These ground-breaking tools provide UI/UX designers the freedom to combine creativity with automation, transforming how websites are conceived, created, and maintained. Website builders give designers t
8 min read
How to Build a Website using Wix ?
Wix is an online drag and drop tools to create extremely easy website using website builders. For learning how to code and build a website on your own, here are few cloud based web development. In this article, we will exhibit creating a website using Wix and this is chosen just for the sake of illustrative purposes. How to Get Started? Getting sta
6 min read
10 Reasons Why SEO is Important for a Website
As someone has so morbidly (but correctly!) stated: "The best place to hide a dead body is on the second page of Google Search!!" Now don't be alarmed...this quote refers less to homicidal tendencies and more to the importance of Search Engine Optimization(SEO) in modern times. But now the question arises... What is SEO and why all the fuss?? Well,
5 min read
Article Tags :
Practice Tags :