安全开发 1 Python介绍与入门 2 使用Python实现端口扫描 3 使用Python编写漏洞PoC 4 使用Python实现一个简单的XSS检测工具 5 使用Python 实现一个域名/IP 扫描工具

1、Python简介

Python是一种计算机程序设计语言。是一种动态的、面向对象的脚本语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越来越多被用于独立的、大型项目的开发。

Python是一种解释型脚本语言,可以应用于以下领域:

  • Web 和 Internet开发
  • 科学计算和统计
  • 教育
  • 桌面界面开发
  • 软件开发

2 使用Python实现端口扫描

1、扩展阅读

1、常见端口扫描类型及原理:[常见的端口扫描类型及原理 - whatday的专栏 - CSDN博客](https://blog.csdn.net/whatday/article/details/45504961)

2、NMap Python 库:[python-nmap · PyPI](https://pypi.org/project/python-nmap/)

3、Socket模块文档:[18.1. socket — Low-level networking interface — Python 3.6.7rc1 documentation](https://docs.python.org/3.6/library/socket.html)

4、Scapy 官方文档:https://scapy.readthedocs.io/en/latest/usage.html#starting-scapy

2、python代码:

安全开发
1 Python介绍与入门
2 使用Python实现端口扫描
3 使用Python编写漏洞PoC
4 使用Python实现一个简单的XSS检测工具
5 使用Python 实现一个域名/IP 扫描工具

3 使用Python编写漏洞PoC

1、python requests库用法整理

参考链接:

https://blog.csdn.net/fnmsd/article/details/79531403

requests库是python中功能强大的HTTP请求库,可帮助使用者自动进行参数及表单的URL编码。并通过urllib3进行了自动实现Keep-alive和HTTP连接池。可以帮开发者将很多手工实现的地方变为自动。

安全开发
1 Python介绍与入门
2 使用Python实现端口扫描
3 使用Python编写漏洞PoC
4 使用Python实现一个简单的XSS检测工具
5 使用Python 实现一个域名/IP 扫描工具

2、编写poc和exploit的几款常用工具介绍

1.pwntools

pwntools是一个CTF框架和漏洞利用开发库,用Python开发,由rapid设计,旨在让使用者简单快速的编写exploit。

pwntools对Ubuntu 12.04和14.04的支持最好,但是绝大多数的功能也支持Debian, Arch, FreeBSD, OSX, 等等。

sudo pip install pwntools即可安装

如果安装过程中提示缺少相应的库,应该都可以很容易的google到解决方法。

安装完成后执行以下命令来检测是否成功:

 安全开发
1 Python介绍与入门
2 使用Python实现端口扫描
3 使用Python编写漏洞PoC
4 使用Python实现一个简单的XSS检测工具
5 使用Python 实现一个域名/IP 扫描工具

如果执行结果和上面相同,则说明安装成功,pwn模块现在可以使用了。

2.zio

pwntools和zio两者均是用python开发的exp编写工具,同时方便了远程exp和本地exp的转换 sudo pip install zio即可安装

安全开发
1 Python介绍与入门
2 使用Python实现端口扫描
3 使用Python编写漏洞PoC
4 使用Python实现一个简单的XSS检测工具
5 使用Python 实现一个域名/IP 扫描工具

3、POC编写流程

参考链接:

https://poc.evalbug.com/chapter1/1.html

  1. 根据漏洞详情,寻找受影响版本程序。
  2. 如果是一些 CMS, 可以去 对应的官网下载历史版本程序,有些漏洞作者不会提具体版本号,那你就要根据漏洞作者提交的时间来判断了。当然除了官方网站之外,还可以去 github 上寻找源码,这些个官网都喜欢把历史版本的程序删除掉,善用 github 上的 tag, branch, release 功能,上面有你要的所有版本。
  3. 搭建对应漏洞靶场
  4. 这里其实没什么好说的,下载了人家的源码之后,在你的虚拟机里面配置好环境就行了,还能顺便学一下运维的一些皮毛知识。
  5. 手动复现漏洞
  6. 根据漏洞详情,手动将整个流程走一遍,熟悉下复现条件,比如使用 GET 还是 POST 请求,需不需要登陆,返回的页面会是什么样子的,我提交不同的参数,会不会出现其它结果,如果漏洞不存在的话,会出现什么样的结果......
  7. 磨刀不误砍柴功,熟悉了这些之后,后面编码实现简直就是分分钟的事,因为一开始我们就说过了,PoC 就是一小段代码。
  8. 写代码
  9. 手动复现了一次漏洞之后,分析漏洞证明步骤,根据自己的实际测试情况一步一步写代码,用我的话来说,写代码,其实就是用程序模拟人工操作的每一个步骤,如果你上一步认真做了,这一步只是经验问题,本教程将在后面丰富你在这方面的阅历。
  10. 测试 PoC
  11. 其它测试和编码基本都是在同时进行的,为什么要单独提出来讲呢,我们在写 PoC 的时候,除了在存在漏洞的靶机上测试我们的代码,还要在不存在漏洞的站点测试,一般为说,一个优秀的 PoC 在后期测试的时候要求对 10000 个目标测试,误报不能超过 10 个。

4 使用Python实现一个简单的XSS检测工具

1、脚本源码下载:

https://sobug.quip.com/-/blob/PXFAAA2twBF/GbRE9HgO7kxEaRhbxD7PGA?name=domain_scan.tar.gz

2、脚本内容:

 1 #!/usr/bin/env python
 2 # coding=utf-8
 3 
 4 import re
 5 import sys
 6 import signal
 7 import codecs
 8 import publicsuffix
 9 import threading
10 import threadpool
11 from lib.data import kb
12 from lib.scan import ip_to_domain, get_icp_data, brute_subdomain,
13                      dns_zone_transfer, domain_to_ip
14 
15 MAX_DEPTH = 2
16 #PSL_FILE = publicsuffix.fetch()
17 PSL_FILE = codecs.open('public_suffix_list.dat', encoding='utf8')
18 P_DOMAIN = publicsuffix.PublicSuffixList(PSL_FILE)
19 
20 
21 def check_ip(ip):
22     compile_ip = re.compile('^((25[0-5]|2[0-4]d|[01]?dd?).){3}(25[0-5]|2[0-4]d|[01]?dd?)$')
23     return True if compile_ip.match(ip) else False
24 
25 
26 def monitor():
27     task_pool = threadpool.ThreadPool(10)
28     while len(kb.task_list_ip) or len(kb.task_list_domain) or threading.activeCount() > 1:
29         tip_msg = 'Ip task length: %s, Domain task length: %s.' % (len(kb.task_list_ip), len(kb.task_list_domain))
30         tip_msg += 'Ip result length: %s, Domain result length: %s.' % (len(kb.result_ip), len(kb.result_domain))
31         tip_msg += 'Workder Num: %s' % threading.activeCount()
32         print(tip_msg, end='
')
33 
34         if len(kb.task_list_ip):
35             ip, depth = kb.task_list_ip.pop()
36             if ip not in kb.task_done_ip and depth <= MAX_DEPTH:
37                 kb.task_done_ip.add(ip)
38                 wr = threadpool.WorkRequest(ip_to_domain, None, {'ip': ip, 'depth': depth})
39                 task_pool.putRequest(wr)
40 
41         if len(kb.task_list_domain):
42             domain, depth = kb.task_list_domain.pop()
43             root_domain = P_DOMAIN.get_public_suffix(domain)
44 
45             if root_domain not in kb.task_done_domain and depth <= MAX_DEPTH:
46                 kb.task_done_domain.add(root_domain)
47 
48                 wr = threadpool.WorkRequest(get_icp_data, None, {'root_domain': root_domain, 'depth': depth})
49                 task_pool.putRequest(wr)
50                 wr = threadpool.WorkRequest(brute_subdomain, None, {'root_domain': root_domain, 'depth': depth})
51                 task_pool.putRequest(wr)
52                 wr = threadpool.WorkRequest(dns_zone_transfer, None, {'root_domain': root_domain, 'depth': depth})
53                 task_pool.putRequest(wr)
54                 wr = threadpool.WorkRequest(domain_to_ip, None, {'domain': root_domain, 'depth': depth})
55                 task_pool.putRequest(wr)
56 
57             if domain != root_domain and domain not in kb.task_done_domain and depth <= MAX_DEPTH:
58                 kb.task_done_domain.add(domain)
59                 wr = threadpool.WorkRequest(domain_to_ip, None, {'domain': root_domain, 'depth': depth})
60                 task_pool.putRequest(wr)
61         try:
62             task_pool.poll()
63         except:
64             pass
65 
66     if task_pool.dismissedWorkers:
67         task_pool.joinAllDismissedWorkers()
68 
69 
70 def write_result():
71     print('Get %s domain and %s ip' % (len(kb.result_domain), len(kb.result_ip)))
72     with open('ip_result.txt', 'w') as f:
73         for ip in kb.result_ip:
74             f.write('%s
' % ip)
75 
76     with open('domain_result.txt', 'w') as f:
77         for domain in kb.result_domain:
78             f.write('%s
' % domain)
79 
80 
81 if __name__ == '__main__':
82     signal.signal(signal.SIGTERM, write_result)
83     signal.signal(signal.SIGINT, write_result)
84 
85     target = sys.argv[1]
86 
87     if check_ip(target):
88         kb.task_list_ip.add((target, 0))
89     else:
90         kb.task_list_domain.add((target, 0))
91 
92     print('Start scan with target %s ...' % target)
93 
94     t = threading.Thread(target=monitor, args=())
95     t.setDaemon(True)
96     t.start()
97     t.join()
98 
99     write_result()

3、XSS漏洞自动化验证

参考链接:

http://sec-redclub.com/archives/651/

XSS漏洞检测

绝大多数Web漏洞扫描器都可以检测出某一网站是否存在XSS漏洞,网上也能找到很多专用的XSS检测工具,具体的原理及检测过程不再赘述。有兴趣可查看极客学院的课程《XSS检测与防御》

XSS自动化验证

验证的前提是已经确认某页面存在XSS漏洞,并有成功触发的POC(Web漏洞扫描器都会返回吧?)。

验证的思路如下:

  • 判断该POC的发送方式:GET和POST
  • 若为GET,直接发送即可触发
  • 若为POST,拆分POC,本地构造输入框,提交POST请求
  • 触发后自动截图

4、开源XSS脚本检测工具

参考链接:

http://www.nsoad.com/Security-tools/20161103/tools-683.html

简介

anti-XSS采用Python编写,除了必要的系统库和报告生成库,没有引入任何第三方库。通过迭代和二次开发,anti-XSS不仅可以在远端对目标站点进行XSS脚本的探测扫描,同时还可以部署在服务器上监控Web环境。

目前发布的版本为anti-XSS Pre-Release v0.1-alpha,相信经过一段时间的测试和开发,Release V1.0能够很快发布。

Requirements

Python 2.7.x

Reportlab

安装

你可以点击这里下载zip 格式的安装包

当然最好是通过Git下载并按要求来安装anti-XSS

$ git clone https://git@github.com:lewangbtcc/anti-XSS.git anti-XSS
$ cd anti-XSS
$ pip install -r requirements.txt

anti-XSS可以运行在安装有Python2.7的任何平台上

使用

基本的选项和使用帮助:

python anti-XSS.py -h

或者

python anti-XSS.py --help

5 使用Python 实现一个域名/IP 扫描工具

扩展阅读:

DNS域传送漏洞:[DNS域传送漏洞的收集、检测与利用 - LandGrey-On the way to become a hacker - CSDN博客]

( https://blog.csdn.net/c465869935/article/details/53444117 )

[DNS域传送漏洞(一) | 李劼杰的博客]( http://www.lijiejie.com/dns-zone-transfer-1/ )

http://www.lijiejie.com/dns-zone-transfer-2/

Python 线程池: https://blog.csdn.net/hehe123456zxc/article/details/52258431

DNS域传送漏洞

DNS协议支持使用axfr类型的记录进行区域传送,用来解决主从同步的问题。如果管理员在配置DNS服务器的时候没有限制允许获取记录的来源,将会导致DNS域传送漏洞。

服务器会监听TCP和UDP的53端口,DNS协议同时支持从这两个端口进行数据传输。因为涉及到1024以下的端口号,所以运行命令可能需要root权限。

在Linux下,我们可以使用dig命令来发送dns请求。比如,我们可以用dig @192.168.53.128 www.baidu.com获取域名www.baidu.com在目标dns服务器上的A记录:

 安全开发
1 Python介绍与入门
2 使用Python实现端口扫描
3 使用Python编写漏洞PoC
4 使用Python实现一个简单的XSS检测工具
5 使用Python 实现一个域名/IP 扫描工具

发送axfr类型的dns请求:dig @192.168.53.128 -t axfr vulhub.org

安全开发
1 Python介绍与入门
2 使用Python实现端口扫描
3 使用Python编写漏洞PoC
4 使用Python实现一个简单的XSS检测工具
5 使用Python 实现一个域名/IP 扫描工具

可见,我获取到了vulhub.org的所有子域名记录,这里存在DNS域传送漏洞。

我们也可以用nmap script来扫描该漏洞:nmap --script dns-zone-transfer.nse --script-args "dns-zone-transfer.domain=vulhub.org" -Pn -p 53 your-ip

安全开发
1 Python介绍与入门
2 使用Python实现端口扫描
3 使用Python编写漏洞PoC
4 使用Python实现一个简单的XSS检测工具
5 使用Python 实现一个域名/IP 扫描工具

注:以上大多转自破壳笔记学习资料,欢迎大家前来报名学习