为什么 sqlmap 源码看起来那么费劲?
最近在看sqlmap的源码,参考了乌云上的源码分析,可是越看越混乱,想提升在python编程方面的能力,该怎么办?
回复内容:
谢邀,是这样的:1. 首先你应该搞懂它的架构体系,模块与分层,设计模式等。说白了,你得懂软件工程。
我为什么会在技能表里特别推荐读它的源码,绝不仅它的 SQL 注射能力一流,还因为它的软件工程牛逼到不行。你可以想象下,能打造如此稳定的框架工具已经不是件简单的事。如果你深入下去就会发现里面处处是软件工程那些优秀思想,关于这点比起曾经的 w3af 不知道要牛逼多少倍,后来 w3af 卧薪尝胆搞了次重写,才有现在的地位。
这个软件工程举些具体的点吧,比如并发,如果你连什么是并发,什么是线程、锁、条件、信号、超时、异常等概念都不懂;再复杂点并发里进程、线程、协程的差异是什么;再再复杂点 Python GIL、垃圾回收、性能陷阱;再再再复杂点 Python 那些内置模块、第三方模块的 ugly 点的 hack,看到这些源码你估计会哭死…哦,对了,如果你并不熟 Python,那么那些 Pythonic 的技巧估计也会让你怀疑自己、怀疑世界…
说的题外话:如果你把 sqlmap 当产品来读,还能读出更多东西;如果你把 sqlmap 当作开源社区运作模式来读,又能读出更多东西。如果不是因为它的开源运作、产品、软件工程、SQL 注射的一流,我们也不会持续用它并提交贡献代码与 Bugs 反馈。
正因为这种优秀,你要读的东西可多了,当然难!
2. 要吃透 sqlmap,还有一个非常关键的是,熟练 SQL 注射各类技术与技巧,熟练基于 SQL 的后续渗透技巧,sqlmap 支持几乎所有主流数据库的注射。
这又是一大难题。如果你不熟悉,你绝对想不到它为什么那样发包,为什么那样处理…
哦,对了,如果你并不是一名渗透手,你绝对难以思考到为什么 sqlmap 集成了那一堆技巧。
3. 虽然有人写了些小而美的工具来比拼 sqlmap 的一些不足,但还是向 sqlmap 致敬,他们在不断改进。
以上是我从事黑客工程化数年经验的一些回馈。不用怕,啃久了自然熟。
最后,./sqlmap.py -h 开启一个纵深知识领域吧:)
-----------
补充下,这就是为什么我们有些源码风格类似 sqlmap,吸收好的。
计划在我公众号(lazy-thought)里展开写写 sqlmap 和一些优秀的 Python 黑客工具,仅计划而已,感兴趣那关注吧。 最近,一直在做内部的分布式集群扫描器,有机会集中精力的阅读了一下SQLMap代码,在完全兼容SQLMap Payloads的基础上重新实现了SQL扫描器。
SQLMap做为一款工具是非常优秀的,但不适合做集群并发,所以要重写。
从软件设计上来说,层次是很清晰的,但具体的代码实现上有不太优雅的地方,尤其是regex用的太多(我不太喜欢用regex)。
包括几个方面:
1、和所有的程序一样,启动的时候要先装在配置和初始化系统变量,其中重要的两个配置是payloads 和 dbms相关的信息;
payloads中分为两部分: boundary 和 testing 。。很重要
2、对payloads的管理,当给出一个url,如何获得污染的url,这个过程比较复杂
3、网络控制部分;
4、http 响应分析,这个部分和 如何污染关系很大;
5、逻辑判断,涉及到很多算法
一般过程:
1、先猜测dbms,具体实现就是用一段可以导致Database出错的参数注入进去,根据响应输出来找到对应的dbms,很可能找不到,就盲注入。。。
2、进行不同的payloads注入,主要包括以下几个:
payload的注入很有意思,根据参数值的类型(数值,字符串),有些请求还要进行SQL转意,比如: Error-Based/UNION Query,原因很简单,避免干扰对输出的分析。比如 :CONCAT(0x20,,)这样的函数,如果被执行了,就是一个字符串。
2.1 Error-based : 一般分析不到dbms,基本上也无法实现Error-based,分析原理很简单,看响应输出是否有特定的字符串;
2.2 Boolean-based: 通过成对的逻辑真和逻辑假(仅是可能)构造请求,根据每次的响应输出,对比内容的差异范围。。。
2.3 QUNION Query: 在枚举列的时候那个算法很精妙;
2.4 Timer-based: 根据多次的rt,经过方差算法,推断,这个受网络影响比较大,在获取dbname 处的算法和Boolean-based一样,算法也是很精妙的,其实就是枚举+二分法。
随便简单写了一些,希望对你有帮助。SQLMap是个很好的工具,但没有那么复杂,只要花点时间阅读代码,可以搞懂的。
对了,还需要了解各种数据库知识,里面涉及到很多数据库相关的知识。。。 @余弦 副总裁的答案已经很详细了,老夫虽常偏责他,但也颇为认同他的回答并已赞之。
总的来说,如果你只是想用 sqlmap,那么多看看多试试
python sqlmap.py -hh

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
