Fuzzing101实践之GIMP

6. fuzzing101 - 6 gimp
1. 目标环境配置
本次的目标程序是一个带有gui的可交互的程序,在构建编译上会比之前的软件稍微有一丢丢复杂。
首先要安装gimp会使用到的 gegl 0.2(generic graphics library),尝试使用源码编译:
# install dependenciessudo apt install build-essential libatk1.0-dev libfontconfig1-dev libcairo2-dev libgudev-1.0-0 libdbus-1-dev libdbus-glib-1-dev libexif-dev libxfixes-dev libgtk2.0-dev python2.7-dev libpango1.0-dev libglib2.0-dev zlib1g-dev intltool libbabl-dev# download and uncompresswget https://download.gimp.org/pub/gegl/0.2/gegl-0.2.0.tar.bz2tar xvf gegl-0.2.0.tar.bz2 && cd gegl-0.2.0# modify the source codesed -i 's/codec_cap_truncated/av_codec_cap_truncated/g' ./operations/external/ff-load.csed -i 's/codec_flag_truncated/av_codec_flag_truncated/g' ./operations/external/ff-load.c# build and install./configure --enable-debug --disable-glibtest --without-vala --without-cairo --without-pango --without-pangocairo --without-gdk-pixbuf --without-lensfun --without-libjpeg --without-libpng --without-librsvg --without-openexr --without-sdl --without-libopenraw --without-jasper --without-graphviz --without-lua --without-libavformat --without-libv4l --without-libspiro --without-exiv2 --without-umfpackmake -j$(nproc)sudo make install  
这里对于 gegl 这个图形库的编译安装我们不做过多介绍,这不是我们的重点,可以明确告知的是上面的库在编译时大概率会编译报错,导致一些库文件编译失败。所以,对于ubuntu 20.04以上版本(我使用的是22.04)可以直接 sudo apt install libgegl-0.4-0 来安装这个0.4版本的库。(尽量不在非fuzz阶段浪费时间)
然后,下载 gimp 2.8.16,并进行编译安装:
# download cd ..wget https://mirror.klaus-uwe.me/gimp/pub/gimp/v2.8/gimp-2.8.16.tar.bz2tar xvf gimp-2.8.16.tar.bz2 && cd gimp-2.8.16/# build and installcc=afl-clang-lto cxx=afl-clang-lto++ pkg_config_path=$pkg_config_path:/usr/local/lib/pkgconfig cflags=-fsanitize=address cxxflags=-fsanitize=address ldflags=-fsanitize=address ./configure --disable-gtktest --disable-glibtest --disable-alsatest --disable-nls --without-libtiff --without-libjpeg --without-bzip2 --without-gs --without-libpng --without-libmng --without-libexif --without-aa --without-libxpm --without-webkit --without-librsvg --without-print --without-poppler --without-cairo-pdf --without-gvfs --without-libcurl --without-wmf --without-libjasper --without-alsa --without-gudev --disable-python --enable-gimp-console --without-mac-twain --without-script-fu --without-gudev --without-dbus --disable-mp --without-linux-input --without-xvfb-run --with-gif-compression=none --without-xmc --with-shm=none --enable-debug --prefix=$home/desktop/fuzz/training/fuzzing_gimp/gimp-2.8.16/installafl_use_asan=1 make -j$(nproc)afl_use_asan=1 make install  
这里的编译选项有点多,第一次的时候尽可能保持一致,避免出错,如果要进行优化和改进,可根据实际需求来增删编译选项。
编译完成后检查软件是否可以正常运行,命令行和图形界面都检查一下。
2. afl++编译target
1. persistent mode
persistent mode 是 afl 提供的一种可以加快fuzz 执行速度的功能,详细原理我们在源码解析的文章中已经进行了深入的介绍,这里大家只需要简单理解成无需每次都进行 fork 操作,而只是在程序的某一特定位置进行循环 fuzz。
2. 修改源码
我们需要在源码中找合适的位置插入 persistent mode 的执行代码,对于本例而言,有两处可以插入。第一处是 app.c 文件:
第二处是 xcf.c 文件:
至于为什么选择这两个地方进行 fuzz ,就看大家对软件流程和功能的理解程度了。
我们这里执行时,两种方案都测试一下。第二种方案,通过打补丁的方式来修改源码,补丁如下:
--- ../xcf.c    2014-08-20 08:27:58.000000000 -0700+++ ./app/xcf/xcf.c    2021-10-11 13:02:42.800831192 -0700@@ -277,6 +277,10 @@   filename = g_value_get_string (&args->values[1]);+#ifdef __afl_compiler+  while(__afl_loop(10000)){+#endif+   info.fp = g_fopen (filename, rb);   if (info.fp)@@ -366,6 +370,12 @@   if (success)     gimp_value_set_image (&return_vals->values[1], image);+#ifdef __afl_compiler+  }+#endif++  exit(0);+   gimp_unset_busy (gimp);   return return_vals;  
需要注意的是,最后的 exit(0); 一定要有,否在程序会在 console 模式下卡住,导致 fuzz 的 test 都超时。
进行patch:
patch gimp-2.8.16/app/xcf/xcf.c -i persistent.patch  
3. 执行fuzz
测试用例我们用一个最简单的:
mkdir afl_in && cd afl_inwget https://github.com/antonio-morales/fuzzing101/blob/main/exercise%206/sampleinput.xcf  
这里还要注意,删除掉 gimp 的插件,这些插件可能会导致 gimp 运行失败:
rm ./install/lib/gimp/2.0/plug-ins/*  
最后开启 fuzz:
asan_options=detect_leaks=0,abort_on_error=1,symbolize=0 afl-fuzz -i './afl_in' -o './afl_out' -d -t 200 -m master -- ./gimp-2.8.16/install/bin/gimp-console-2.8 --verbose -d -f @@asan_options=detect_leaks=0,abort_on_error=1,symbolize=0 afl-fuzz -i './afl_in' -o './afl_out' -d -t 200 -s slave1 -- ./gimp-2.8.16/install/bin/gimp-console-2.8 --verbose -d -f @@


高温超导体新的际遇
艾默生变频器及PLC在恒液位控制中的应用
电磁铁电源JCP-10/120HA的技术参数
北京 6月16日-17日《产品EMC正向设计与检视》公开课火热报名中!
澳大利亚联邦银行Smarts Assets用区块链建立农业设备资产管理共享平台
Fuzzing101实践之GIMP
WiFi6的介绍和5G网络的对比
如何正确选择漆包线点焊机
SDS2000XHD系列示波器的数学运算
如果苹果公司真的要造车,麦格纳应该制造它
2022未来品牌评选出炉:李宁、小鹏汽车、Leader等上榜
汽车芯片为什么短缺什么时候能缓解
可穿戴设备下个方向是情感还是健康
2023年在线视频趋势:它们是什么以及它们如何让你受益(下)
颜色传感器DIY图解
维信诺宣布以全新品牌形象加速其在新型显示领域技术研发和产业化布局
爆了!GPT-4模型架构、训练成本、数据集信息都被扒出来了
VoxelMap++:在线LiDAR惯性里程计实现可合并的体素建图方法
怎样让质量流量计发挥高精度作用
航空工业陕飞正在全力科研生产任务的高质量发展