Binwalk 任意代码执行漏洞原理分析

admin 2023年5月12日08:54:57评论53 views字数 3698阅读12分19秒阅读模式




一、背景介绍 



Binwalk是用于搜索给定二进制镜像文件以获取嵌入的文件和代码的工具。具体来说,它被设计用于识别嵌入固件镜像内的文件和代码。Binwalk使用libmagic库,因此它与Unix文件实用程序创建的魔数签名兼容。Binwalk还包括一个自定义魔数签名文件,其中包含常见的诸如压缩/存档文件,固件头,Linux内核,引导加载程序,文件系统等的固件映像中常见文件的改进魔数签名。




二、漏洞介绍 



影响版本: 2.1.2b~2.3.2
Binwalk 提取固件时存在路径穿越问题,可导致任意代码执行。




 三、漏洞分析 



3.1 CVE-2022-4510
该漏洞允许远程攻击者在受影响的机器上执行任意代码,需要用户交互,使用者必须使用提取模式打开指定固件。
$ binwalk -e xx.bin


3.1.1 漏洞位置(unpfs.py)

def extractor(self, fname):
       fname = os.path.abspath(fname)
       out_dir = binwalk.core.common.unique_file_name(os.path.join(os.path.dirname(fname), "pfs-root"))
       try:
           with PFS(fname) as fs:
               # The end of PFS meta data is the start of the actual data
               data = binwalk.core.common.BlockFile(fname, 'rb')
               data.seek(fs.get_end_of_meta_data())
               for entry in fs.entries():
                   outfile_path = os.path.join(out_dir, entry.fname)# 漏洞位置
                   if not outfile_path.startswith(out_dir):
                       binwalk.core.common.warning("Unpfs extractor detected directory traversal attempt for file: '%s'. Refusing to extract." % outfile_path)
os.path.join函数解析路径并不能过滤危险字符。
Python 3.10.1 (tags/v3.10.1:2cd268a, Dec  6 2021, 19:10:37) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> os.path.join("/tmp","../etc/passwd")
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>> import os
>>> os.path.join("/tmp","../etc/passwd")
'/tmp/../etc/passwd'
通过构造恶意的PFS固件名(包含..符号),可以将解压的文件进行跨目录覆盖。

3.1.2 漏洞修复

               data = binwalk.core.common.BlockFile(fname, 'rb')
               data.seek(fs.get_end_of_meta_data())
               for entry in fs.entries():
                  - outfile_path = os.path.join(out_dir, entry.fname)
                  + outfile_path = os.path.abspath(os.path.join(out_dir, entry.fname))
                   if not outfile_path.startswith(out_dir):
                       binwalk.core.common.warning("Unpfs extractor detected directory traversal attempt for file: '%s'. Refusing to extract." % outfile_path)
                   else:
os.path.join 函数替换为os.path.abspath解决目录穿越问题,过滤掉..特殊字符。

3.2 CVE-2023-0591

ubireader_extract_file 存在目录穿越漏洞,当解压UBIFS文件系统攻击者可覆盖文件目录以外的程序。

3.2.1 漏洞位置(ubireader/ubifs/outpu.py)

def extract_files(ubifs, out_path, perms=False):
   inode = inodes[dent_node.inum]
   dent_path = os.path.join(path, dent_node.name)#漏洞位置

   if dent_node.type == UBIFS_ITYPE_DIR:
       try:
           if not os.path.exists(dent_path):
os.path.join函数解析路径并不能过滤危险字符。

3.2.2 漏洞修复

       - target_path = os.path.join(os.getcwd(), target, path)
       + target_path = os.path.realpath(os.path.join(os.getcwd(), target, path))

       + if not is_safe_path(target, target_path):
       +    print(f"Path traversal attempt to {target_path}, discarding.")
       +    continue
os.path.join 替换为os.path.realpath函数,解决路径穿越问题。

3.3 CVE-2023-0592

3.3.1 漏洞位置(src/scripts/jefferson)

      target_path = os.path.join(os.getcwd(), target, path)

       if not is_safe_path(target, target_path):
           print(f"Path traversal attempt to {target_path}, discarding.")
           continue

       for inode in dirent.inodes:
           try:
               if stat.S_ISDIR(inode.mode):
os.path.join函数解析路径并不能过滤危险字符。

3.3.2 漏洞修复

      - target_path = os.path.join(os.getcwd(), target, path)
      + target_path = os.path.realpath(os.path.join(os.getcwd(), target, path))

       if not is_safe_path(target, target_path):
           print(f"Path traversal attempt to {target_path}, discarding.")
           continue

       for inode in dirent.inodes:
           try:
               if stat.S_ISDIR(inode.mode):
os.path.join 替换为os.path.realpath函数,解决路径穿越问题。




 四、恶意插件 



Binwalk早期运营开始,用户就可以使用binwalk的API自定义自己插件,此时我们可以创建一个恶意插件执行任意代码执行操作。
创建自定义插件 ·ReFirmLabs/binwalk Wiki ·GitHub
import binwalk.core.plugin
import os
import shutil

class EvilExtractor(binwalk.core.plugin.Plugin):
   def init(self):
       os.system("nc -lvp 56643")
恶意插件不仅仅会出现在常见的Web应用中,常见的开源工具也都会采用此种模式来拓展工具的可实用性,丰富工具功能,但由此引发的安全危险需要注意。





 五、总结 



Binwalk作为物联网设备安全分析的重要工具,随着本身项目维护的衰落,对固件识别的模块主要通过插件的方式来解析,鉴于地方依赖性都在遭受可能存在不安全的代码或者存在恶意代码,对开源工具的使用造成了诸多威胁。


原文始发于微信公众号(山石网科安全技术研究院):Binwalk 任意代码执行漏洞原理分析

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2023年5月12日08:54:57
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   Binwalk 任意代码执行漏洞原理分析http://cn-sec.com/archives/1727066.html

发表评论

匿名网友 填写信息