|
文章目录1.工具准备1.1.requests代码包1.2.BurpSuite抓包工具2.操作过程2.1.一个简单的请求2.1.1.Burp获取响应2.1.2.转发获取响应2.2.构造GET类型URL参数2.3.构造请求头部2.4.构造POST类型payload数据2.4.1.urlencoded格式2.4.2.json格式本文主要讲解常用API接口如何使用python实现。API:ApplicationProgrammingInterface,是应用程序开发接口的缩写,意思是一些预设好的函数或方法,这些预设好的函数或方法允许第三方程序通过网络来调用数据或提供基于数据的服务。1.工具准备Python运行环境:python3.10.2+PyCharm1.1.requests代码包使用pipinstallrequests安装此代码包,pip安装教程1.2.BurpSuite抓包工具BurpSuite安装教程,参考1.2章节设置http协议代理为127.0.0.1,端口8080,完成后开启Intercept劫持功能2.操作过程2.1.一个简单的请求使用python发现一个简单的GET请求,并且通过BurpSuite抓取其请求importrequests#URLhost='www.testcode.com'port='9201'url='http://'+host+':'+port#配置Burp截取配置proxies={'http':'http://127.0.0.1:8080'}#发送请求reponse=requests.get(url=url,proxies=proxies)print(reponse.text)1234567891011121314代码运行后,会在BurpSuite上回显一条http请求,即为该代码构造的http请求:将此请求放入重放或者点击代理的forwafd转发即可看到其具体响应内容。2.1.1.Burp获取响应使用BurpSuite的Repeater重放功能就可以直接查看到Response响应。2.1.2.转发获取响应使用Proxy代理模块中的Forward转发可以将请求发送到服务侧,即可在原软件获得Response响应。2.2.构造GET类型URL参数importrequestshost='www.testcode.com'port='9201'url='http://'+host+':'+port#构造参数params={'username':'test','passwd':'aaabbbccc1234567890'}proxies={'http':'http://127.0.0.1:8080'}reponse=requests.get(url=url,proxies=proxies,params=params)print(reponse.text)1234567891011121314151617执行结果:像这种/username=test&passwd=aaabbbccc1234567890的参数格式称之为urlencoded格式,主要是以键值对key=value和拼接字符&组合成http请求参数。2.3.构造请求头部参考文章:HTTP协议头部字段梳理importrequestshost='www.testcode.com'port='9201'url='http://'+host+':'+portparams={'username':'test','passwd':'aaabbbccc1234567890'}#构造头部字段,注意大小写!headers={'User-Agent':'Mozilla/5.0(Macintosh;IntelMacOSX10_15_7)AppleWebKit/537.36(KHTML,likeGecko)Chrome/120.0.0.0Safari/537.36','Referer':'www.aaa.bbb.ccc.com'}proxies={'http':'http://127.0.0.1:8080'}reponse=requests.get(url=url,proxies=proxies,params=params,headers=headers)print(reponse.text)12345678910111213141516171819202122执行结果:2.4.构造POST类型payload数据importrequestshost='www.testcode.com'port='9201'url='http://'+host+':'+portparams={'username':'test','passwd':'aaabbbccc1234567890'}headers={'User-Agent':'Mozilla/5.0(Macintosh;IntelMacOSX10_15_7)AppleWebKit/537.36(KHTML,likeGecko)Chrome/120.0.0.0Safari/537.36','Referer':'www.aaa.bbb.ccc.com'}#设置payloadpayload='''ToveJaniDon'tforgetmethisweekend!'''proxies={'http':'http://127.0.0.1:8080'}#修改类型为POST,设置payload对象编码为UTF-8(中文编码)reponse=requests.post(url=url,proxies=proxies,params=params,headers=headers,data=payload.encode('utf-8'))print(reponse.text)123456789101112131415161718192021222324252627282930313233执行结果:2.4.1.urlencoded格式urlencoded格式解释详见本文2.2章节内容importrequestshost='www.testcode.com'port='9201'url='http://'+host+':'+portparams={'username':'test','passwd':'aaabbbccc1234567890'}headers={'User-Agent':'Mozilla/5.0(Macintosh;IntelMacOSX10_15_7)AppleWebKit/537.36(KHTML,likeGecko)Chrome/120.0.0.0Safari/537.36','Referer':'www.aaa.bbb.ccc.com'}payload={'key1':'value1','key2':'value2'}proxies={'http':'http://127.0.0.1:8080'}reponse=requests.post(url=url,proxies=proxies,params=params,headers=headers,data=payload)print(reponse.text)1234567891011121314151617181920212223242526执行结果:2.4.2.json格式参考文章:JSON数据格式与格式化操作详解importjsonimportrequestshost='www.testcode.com'port='9201'url='http://'+host+':'+portparams={'username':'test','passwd':'aaabbbccc1234567890'}headers={'User-Agent':'Mozilla/5.0(Macintosh;IntelMacOSX10_15_7)AppleWebKit/537.36(KHTML,likeGecko)Chrome/120.0.0.0Safari/537.36','Referer':'www.aaa.bbb.ccc.com'}payload={'key1':'value1','key2':{'key3':'value3','key4':{'key5':'测试'}}}proxies={'http':'http://127.0.0.1:8080'}#payload数据使用json.dumps转化为str,ensure_ascii=False代表关闭非ASCII字符转义#若不关闭非ASCII字符转义,则"测试"回显为转义后的ASCII字符"\u6d4b\u8bd5",两者意思一致reponse=requests.post(url=url,proxies=proxies,params=params,headers=headers,data=json.dumps(payload,ensure_ascii=False).encode())print(reponse.text)12345678910111213141516171819202122232425262728293031323334
|
|