用libFuzzer对Python模块进行模糊测试。

pyfuzzer的Python项目详细描述


buildstatuscoverage

关于

使用libFuzzer对python3.6+C扩展模块进行模糊测试。在

安装

clang 8或更高版本。在

$ apt install clang
$ pip install pyfuzzer

示例用法

你好世界

在测试 模块hello_world。在

^{pr2}$

打印找到新代码路径的函数调用。这个信息 通常在编写单元测试时很有用。在

$ pyfuzzer print_corpus
corpus/25409981b15b978c9fb5a5a2f4dab0c4b04e295f:
    tell(b'') = 5
corpus/a8a4e6c9abfd3c6cba171579190702ddc1317df0:
    tell(b'\xfd#') = b'Hello!'
corpus/80f87702ef9fbe4baf17095c79ff928b9fa1ea14:
    tell(b'\x00') = True
corpus/be3d1b7df189727b2cecd6526aa8f24abbf6df10:
    tell(b'\x00\xfd\x00') = 0
corpus/defd8787d638f271cd83362eafe7fdeed9fa4a8f:
    tell(None) raises:
    Traceback (most recent call last):
      File "/home/erik/workspace/pyfuzzer/pyfuzzer/mutators/utils.py", line 35, in print_callable
        res = obj(*args)
    TypeError: expected bytes, NoneType found

有关所有文件,请参阅hello_world。在

Hello world致命错误

与上一个示例类似,但在 tell()是用一个超过2个字节的bytes对象作为它的 第一个论点。在

$ cd examples/hello_world_fatal_error
$ pyfuzzer run hello_world.c
...
Fatal Python error: deallocating None

Current thread 0x00007f7ca99c2780 (most recent call first):
...

打印导致崩溃的函数调用。正如所料 第一个参数显然长于2个字节。在

$ pyfuzzer print_crashes
crash-1013ed88cd71fd14407b2bdbc17b95d7bc317c21:
    tell(b'\n\xbf+') = None

有关所有文件,请参阅hello_world_fatal_error。在

自定义mutator

测试模块时使用自定义赋值函数hello_world_mutatorhello_world。在

使用自定义赋值函数进行测试通常比使用 普通的。在

$ cd examples/hello_world_custom_mutator
$ pyfuzzer run -l max_total_time=1 -m hello_world_mutator.py hello_world.c
...

有关所有文件,请参阅hello_world_custom_mutator。在

变异子

mutator模块使用来自libFuzzer的数据来测试模块。A mutator模块必须实现函数setup(module),其中 module是正在测试的模块。它将返回一个mutator 实现方法^{tt8}的实例$ 和test_one_input_print(self, data),其中data是数据 由libFuzzer(作为bytes对象)生成。在

{8$tt8执行实际的模糊测试 test_one_input_print(self, data)打印语料库并崩溃。在

CRC-32算法的最小变异函数模糊测试看起来像 下面。它只调用crc_32(),其中只有data 争论。在

frompyfuzzer.mutators.genericimportprint_callableclassMutator:def__init__(self,module):self._module=moduledeftest_one_input(self,data):returnmodule.crc_32(data)deftest_one_input_print(self,data):print_callable(self._module.crc_32,[data])defsetup(module):returnMutator(module)

想法

  • 通过生成C代码添加对fuzz测试纯Python模块的支持 使用Cython。在

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何获取所有链接并使用SeleniumWebDriver逐个单击这些链接   java中数组列表的arraylist移位元素   java如何存储具有不同类型参数的元素   java不能使用List<Future<?>>当从不同位置调用时,在方法参数中   java我应该如何修复代码以避免测试代码出错?   安卓 PreferenceFragment java。lang.RuntimeException   java在运行emulator时发现错误   java取消多个AsyncTask回调的有效方法是什么?如果中途满足条件,这些回调将在将来返回?   有没有办法在运行时将方法动态分配给Java类?   java如何将propertyfile与GlassFish一起使用   如何在PHP中使用bcrypt对密码进行哈希处理?   Java和UDP web服务器   java JPA允许EntityListeners执行JPA操作吗?   java执行do while语句时不会产生错误   java无法在edittext中显示数据库中的数据   java Tomcat中活动会话的数量   java在何处使用@SuppressWarnings(“requestfactory”)   netty连接中的java HornetQ异常