How to Build a SQL Injection Scanner in Python?
In general terms, SQLi is the most prevalent and dangerous code insertion technique. An SQLi attack is meant to send malicious SQL commands to the database server. The most common attack goal is bulk extraction of knowledge. Attackers can dump database tables with many thousands of customer records. Depending on the environment, SQL injection also can be exploited to switch or delete data, execute arbitrary OS commands, or launch denial-of-service (DoS) attacks.
Building SQL Injection Scanner in Python
Using the below approach we will extract the web forms first because SQL injection is carried through user input. Then, we will check whether a web page has SQL errors in it, this will be useful when checking for SQL injection attacks and finally, we will test it on HTML forms.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
For this, we will require requests and BeautifulSoup package.
- Import module
- Now, initialize the session of HTTP and set the latest user agent for your browser
- Now we shall extract the HTML web forms.
- For this, first, we will write a function that upon giving a URL, will make a request to that page and will extract all the HTML form tags from it
- Then return these tags as a list.
- We can then use this list afterward.
- Now we will check that whether the obtained page has any SQL vulnerabilities or not from its response output.
- If it has any syntax error, the page is vulnerable. Although there are a lot of database errors we will search with limited database errors that is Oracle and SQL Server Errors, because these two are mostly used.
- Now we will apply this search approach for all the forms in the HTML web page for the error
- Our script is ready, and we will now test it.
- We will pass the URL upon which we have to detect SQL injection.
- So we will pass the URL argument through the command line.
[+] Detected 0 forms on https://www.geeksforgeeks.org/python-programming-language/.