sqlmap源码阅读之optparse

来源:互联网 发布:php 常用魔术方法 编辑:程序博客网 时间:2024/05/01 20:36

sqlmap.py中命令行参数适配

今天抽空来看看命令行参数的适配,以下是sqlmap.py中的代码

        # Store original command line options for possible later restoration        cmdLineOptions.update(cmdLineParser().__dict__)        initOptions(cmdLineOptions)
这个cmdLineOptions是AttrDict的一个实例,至于AttrDict之前有介绍过。

update方法是AttrDict的父类Dict的方法,没有重写,接受一个dict或者mapping object为参数,更新调用者,此处即cmdLineOptions。

cmdLineParser().__dict__

__dict__这个参数之前也有介绍过,不过是Object的属性,其中包含各种与Object相关的属性键值对。这里cmdLineParser()是一个函数,那么一个函数的__dict__是怎么回事呢???

其实,不是这样子的,cmdLineParser()这里其实是一个匿名返回对象,可以理解为dictTem = cmdLineParser(), update(dictTem.__dict__)。不过,如果把__dict__去掉,代码会报错的。测试了下返回值的类型(print dictTem.__class__),发现返回值是optparse.Values不符合参数类型,所以加__dict__转化为dict

接下来深入下cmdLineParser()这个函数,它用到了一个module[optparse],具体资料可以查阅python-doc之optparse,大体来说,就是一个封装了的用于解决编程中命令行参数的获取、动作等一系列自动化地模块,比较强大(想起当年自己还苦逼地自己写这些....)

基本上这样一个模式:

parser = OptionParser(usage=usage)......parser.add_option("--hh", dest="advancedHelp",                   action="store_true",                   help="Show advanced help message and exit")......(args, _) = parser.parse_args(argv)
具体参考上面的python-doc(里面这个args,就是返回值,哈哈)。

至于那个args的类型,pydoc里有如下解释:

As you can see, most actions involve storing or updating a value somewhere.<a target=_blank class="reference internal" href="https://docs.python.org/2/library/optparse.html?highlight=optparse#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre"><span class="highlighted">optparse</span></span></tt></a> always creates a special object for this,conventionally called<tt class="docutils literal"><span class="pre">options</span></tt> (it happens to be an instance of <tt class="xref py py-class docutils literal"><span class="pre"><span class="highlighted">optparse</span>.Values</span></tt>).  Option arguments (and various other values) are stored as attributes of this object,according to the <a target=_blank class="reference internal" href="https://docs.python.org/2/library/optparse.html?highlight=optparse#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a> (destination) option attribute


ok,接下来看这一句代码:

initOptions(cmdLineOptions)
由上文知道cmdLineOptions包含了用户从命令行输入的一些参数,大概类似于["url":"www.xxx.com", "code": "none", .......]之类的。

具体来看看这个函数initOptions()

def initOptions(inputOptions=AttribDict(), overrideOptions=False):    if IS_WIN:        coloramainit()    _setConfAttributes()    _setKnowledgeBaseAttributes()    _mergeOptions(inputOptions, overrideOptions)

其中包含3个函数,里面那个if语句,大意是判断平台,给输出添加颜色之类的,不知道怎么弄的,居然没找到source code

_setConfAttributes()这个函数是初始化conf变量的

_setKnowledgeBaseAttributes()这个函数是初始化kb变量的

************

(话说至今没搞懂作者的那个kb的注释神码意思)

# object to share within function and classes resultskb = AttribDict()
************
接下来那个_mergeOptions(inputOptions, overrideOptions),这里inputOptions就是刚才那个cmdLineOptions

这里偷个懒,直接贴作者的注释

Merge command line options with configuration file and default options.
结果就是经过compare比较,发现此函数运行后conf有变化。所以没有深究。


3.至于sqlmap.conf这个文件,是作者提供给使用者可以把想关信息写入到配置文件中,无需从命令行输入参数的一种方式。

用-c参数指定配置文件名称,sqlmap.py本身并没有用到sqlmap.conf这个文件。


备注:

阅读python代码,可以随意改,遇到不懂的,不知道结果的,直接改代码,然后print~~

不过还是怀念rails里面的那个rails console可以把程序运行环境加载进去,直接终端下查看程序变量内容,更方便些~话说,好久没碰rails了,都有点忘了

0 0
原创粉丝点击