Linux新版内核下内存取证分析附CTF题

k333 2022-10-08 10:01:00
CTF

背景

最近做了一道国际赛SEKAI的取证题, 题目使用LiME对内核为5.15.0-43-genericUbuntu制作内存镜像, 需要自行制作Symbols 进行分析. 出题人让我们自行探索新版Linux内核下的取证策略. 尽管官方Volatility2提供制作profileVolatility3提供制作Symbols的方案, 但在实际应用中, 均出现较多目前互联网尚未解决的问题. (具体详见GitHub关于VolatilityIssues.). 下文主要内容是基于官方文档、题目、实操步骤, 提供在新版Linux下的制作Symbols的解决方案.

Untitled.png

题目附件:链接:https://pan.baidu.com/s/1db4qgJHxlQKYmtYoNVVahQ 密码:mqh1

入手

题目提供标识LiME制作内存镜像文件, 可以利用banner中关键词搜索“Linux Version”找到相关的内核版本. 从而再进一步选择Volatility2Profile或者Volatility3Symbols.

Untitled 1.png

Untitled 2.png

Linux version 5.15.0-43-generic (buildd@lcy02-amd64-076) (gcc (Ubuntu 11.2.0-19ubuntu1) 11.2.0, GNU ld (GNU Binutils for Ubuntu) 2.38) #46-Ubuntu SMP Tue Jul 12 10:30:17 UTC 2022 (Ubuntu 5.15.0-43.46-generic 5.15.39)

或者可以直接使用Volatility3的plugin - banners.Banners,会自动探测.

Untitled 3.png

通过内核版本和Ubuntu在原始数据中搜索可以发现该OS的具体版本Ubuntu 22.04.

当前互联网上尚未有Ubuntu22.04 内核为 5.15.0-43-generic 相关具体解题实现. 但是内核为5.15.0-43-generic, 也可以运行在Ubuntu20.04Ubuntu18.04 下文将通过Volatility3提供解决方案.

Linux 5.15.0-43-generic 制作Profile或Symbols常见问题背景

  • Volatility2

    首先按照官方步骤, 对于Ubuntu22.04 无法正常编译出dwarf文件, 在相关issue的解决方案使用dwarf制作出来的策略在运行过程中 出现DW_AT_data_member_location报错信息.

Untitled 4.png

由于Volatility2的方案在Ubuntu22.04尚未人成功实现, 需要从vol源码角度和Linux内核方向进行修改.本文不再介绍该种思路.

但是对于指定的系统生成profile, 提供以下使用docker快速生成策略的解决方案. 可应用在其他场景. : 可用于出题对互联网尚未发布的策略文件, 使用“较新”系统进行出题.

具体原理, 参照官方说明:[https://github.com/volatilityfoundation/volatility/wiki/Linux](https://github.com/volatilityfoundation/volatility/wiki/Linux)

```bash
# 建立模拟环境
cd ./volatility2/tools/linux
docker run -it --rm -v $PWD:/volatility ubuntu:20.04 /bin/bash
# 安装必须环境
apt update
apt install -y linux-headers-5.15.0-43-generic linux-image-5.15.0-43-generic dwarfdump build-essential vim zip
cd /volatility/tools/linux
/volatility/tools/linux# sed -i 's/$(shell uname -r)/5.15.0-43-generic/g' Makefile
/volatility/tools/linux# echo 'MODULE_LICENSE("GPL");' >> module.c
/volatility/tools/linux# make
/volatility/tools/linux# cd /
/volatility/tools/linux# zip Linux-5.15.0-43-generic.zip volatility/tools/linux/module.dwarf /boot/System.map-5.15.0-43-generic
/volatility/tools/linux# exit
mv Linux-5.15.0-43-generic.zip ./volatility2/volatility/plugins/overlays/linux

```

基于Volatility3对新版本Linux内核进行分析

生成Symbols原理

官网提供了相关说明

Creating New Symbol Tables - Volatility 3 2.4.0 documentation

重点:

  1. Linux and Mac symbol tables can be generated from a DWARF file using a tool called dwarf2json
  2. Currently a kernel with debugging symbols is the only suitable means for recovering all the information required by most Volatility plugins.
    目前使用内核debugging symbols 是生成symbols的唯一方法!!! 在dwarf2json中存在多种方向生成我们需要的json文件, 但是只有 - - elf的方案在vol3中可行,其他使用如Sysmap 解析出来的json都是不可行的. (在vol2中生成profile会用到sysmap)
./dwarf2json linux --elf /usr/lib/debug/boot/vmlinux-4.4.0-137-generic > output.json

从dwarf2json的命令中,我们如果想要生成提供给vol3的json文件, 需要提供"/usr/lib/debug/boot/"下的vmlinux文件, 在正常情况下, 系统并不会自带该文件, 在 /boot/ 目录下, 会提供经过压缩后的同名称文件, 但是并不可用, 想直接从压缩后的数据中, 提取出源码是很困难的一件事, 但是也不是不可能实现.

方案一: 基于apt安装vmlinux

⚠️  该方案需在指定容器中提供至少20G的容器空间

大多数情况下,我们会采用apt添加源之后安装的方法.

  1. 导入GPG key(此处直接使用网络提供的), 在许多vmlinux的帖文中, 很少提及该步骤, 或者对不同版本的GPG Key 导入没有明确说明.

    • Ubuntu 16.04 and higher:

      jsx sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C8CAB6595FDFF622

    • For older distributions:

      jsx sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys ECDCAD72428D7C01

  2. 添加源

    ```jsx
    echo "deb http://ddebs.ubuntu.com $(lsb_release -cs) main restricted universe multiverse" | sudo tee -a /etc/apt/sources.list.d/ddebs.list
    echo "deb http://ddebs.ubuntu.com $(lsb_release -cs)-updates main restricted universe multiverse" | sudo tee -a /etc/apt/sources.list.d/ddebs.list
    echo "deb http://ddebs.ubuntu.com $(lsb_release -cs)-proposed main restricted universe multiverse" | sudo tee -a /etc/apt/sources.list.d/ddebs.list

    apt-get update
    ```

  3. 安装指定的debugging system ,可以把uname -r 换成指定的版本 但是需要系统是可以解析的

    jsx sudo apt-get install linux-image-$(uname -r)-dbgsym

  4. 验证

Untitled 5.png

: 若出现多次错误等问题,可以删除附加源,重新添加即可.

```jsx
# remove old one
rm -f /etc/apt/sources.list.d/ddebs.list
```

方案二: 直接获取dbgsym文件安装在指定系统

⚠️  该方案需在指定容器中提供至少5G的内存空间

获取源文件网址:https://launchpad.net/ubuntu/+source/linux, 按需选择

$ git clone <https://github.com/volatilityfoundation/dwarf2json>
$ cd dwarf2json/
$ go build
wget <https://launchpad.net/ubuntu/+archive/primary/+files/linux-image-unsigned-5.15.0-48-generic-dbgsym_5.15.0-48.54_amd64.ddeb>
# 使用能适用该内核的操作系统 (ubuntu 18.04 / 20.04 / etc)
$ docker run -it --rm -v $PWD:/volatility ubuntu:18.04 /bin/bash
/# cd /volatility/
/volatility# dpkg -i linux-image-unsigned-5.15.0-48-generic-dbgsym_5.15.0-48.54_amd64.ddeb
/volatility# dpkg -i linux-image-unsigned-5.15.0-48-generic-dbgsym_5.15.0-48.54_amd64.ddeb
/volatility# ./dwarf2json linux --elf /usr/lib/debug/boot/vmlinux-5.15.0-48-generic > linux-image-5.15.0-48-generic.json 

$ cp linux-image-4.15.0-184-generic.json ./volatility3/volatility3/framework/symbols/linux

解析内存

目前, vol3支持的Linux插件如下

    banners.Banners     Attempts to identify potential linux banners in an
    linux.bash.Bash     Recovers bash command history from memory.
    linux.check_afinfo.Check_afinfo
    linux.check_creds.Check_creds
    linux.check_idt.Check_idt
    linux.check_modules.Check_modules
    linux.check_syscall.Check_syscall
    linux.elfs.Elfs     Lists all memory mapped ELF files for all processes.
    linux.keyboard_notifiers.Keyboard_notifiers
    linux.kmsg.Kmsg     Kernel log buffer reader
    linux.lsmod.Lsmod   Lists loaded kernel modules.
    linux.lsof.Lsof     Lists all memory maps for all processes.
    linux.malfind.Malfind
    linux.mountinfo.MountInfo
    linux.proc.Maps     Lists all memory maps for all processes.
    linux.psaux.PsAux   Lists processes with their command line arguments
    linux.pslist.PsList
                        Lists the processes present in a particular linux
    linux.pstree.PsTree
    linux.tty_check.tty_check

该题目的考点设置也比较清晰,没有套娃,制作出来指定的策略, 跑一下bash就能获得flag了

Untitled 6.png

小彩蛋1 - CTF续题

Untitled 7.png

该题考点涉及了区块链的助记词使用和python3.10 pyc的逆向, 考点新颖前沿. 有些问题是互联网尚未解决的问题, 以及如何应对, 这里简单的介绍一下

使用vol3很强大的插件linux.psaux.PsAux会发现

python vol.py -f ~/Documents/ctf/sakei/dump.mem  linux.psaux.PsAux

Untitled 8.png

Untitled 9.png

得到pyc文件

Untitled 10.png

在线网站、pycdc报错.

  • 通过报错修改pycdc源码绕过START_WITH_EXCEPTION, 再修改RERAISE操作码为空,重新编译执行
  • 使用pycdas查看bytecode, 通过bytecode, 人工恢复

py的源码如下:

import sys

try:
    password = sys.argv[1]
except:
    print("Usage: ./wallet password")
    exit()

words = []
with open("bip39list.txt", "r") as f:
    words = f.read().splitlines()

code = 0x26F4036773F33FD1BC4E55616472CD7F65086B670B2DD5B84BB4D16F02730E734F72E500
code = bin(code)[2:]
code = str(code.zfill((len(code)+(12-len(code)%12))))

mnemonic = []

for i in range(0, len(code), 12):
    mnemonic.append(words[int(code[i:(i+12)],2)-1])

print(" ".join(mnemonic))

# output:
evidence leopard solution layer legend danger orient project silver flower wrong path stove throw fortune report nuclear old target exact broom hawk toss paper

可以直接使用区块链地址生成算法的网站进行测试

https://iancoleman.io/bip39/#english

By default, all Ethereum wallets use the same derivation path m/44’/60’/0’/0, but if you are seeing unfamiliar addresses, try other derivation paths just in case you accidentally used a different path when creating your wallet. For more information about Derivation paths, see the 'HD wallet and Derivation paths' article.

将24个助记词和选择bip44即可看到生成的地址和私钥, 同时也可以直接使用助记词进行登陆eth查看私钥, 或者其他n多的方法. 总之有助记词, 确定地址, 即可确定私钥. 此处由于篇幅对区块链助记词的应用不做赘述.

Untitled 11.png

小彩蛋2 - WMCTF2022 1!5! (vol2 制作profile)

题目附件: https://github.com/wm-team/WMCTF2022

该题使用vol2制作策略进行分析.

kernel信息

Linux version 4.19.0-21-amd64 (debian-kernel@lists.debian.org) (gcc version 8.3.0 (Debian 8.3.0-6)) #1 SMP Debian 4.19.249-2 (2022-06-30)

这题当时是因为高版本的Linux和一个流量包合出的一道题, 其实对内存命令、进程方面的分析不多, 所以可以直接通过010 editor 直接找到我们需要的内容进行提取. 再完整做出来这道题的时候,我并没有使用到vol, 也有的师傅使用了autospy进行分析.
当然官方的WP严谨的使用了vol2进行制作profile, 官方使用了iso进行复现, 个人认为可能使用docker会更加的便捷.
具体制作的过程, 参考上文“背景”标题下的内容, 或参考官方wp, 本文不再赘述.

小彩蛋3 - 基于sysdig的Linux进程数据捕获和csysdig分析

Sysdig 通过安装到 Linux 内核并捕获系统调用和其他操作系统事件,在操作系统级别对物理和虚拟机进行测试。Sysdig 还可以为系统活动创建跟踪文件,类似于使用 tcpdump 和 Wireshark 等工具为网络所做的工作。这样,就可以在以后分析问题,而不会丢失重要信息。丰富的系统状态存储在跟踪文件中,因此可以将捕获的活动放到完整的上下文中。

项目地址: https://github.com/draios/sysdig

  • 背景

    近两个月来, 该类型题出现多次, 2022鹏程杯用来找mysql的登陆密码, 2022ByteCTF用于对黑客入侵网站的数据进行分析. 在实际生活犯罪过程中, 可能可以隐蔽的作为一种方案进行数据获取. 同时作为一种应急机制进行复现.该项目目前持续更新, 相关问题都能得到及时的解决.

    ByteCTF 2022 Find it题目附件:

    题目附件: 链接:https://pan.baidu.com/s/1IdEouiqUQlO9m8xedycxrA 密码:tlht

    题目描述, 大概是 黑客加密了一个文件, 找到它

Untitled 12.png

  • 分析

    使用csysdig即可分析加载, F4可以过滤指定字符串,也可以过滤用户名 www等

Untitled 13.png

题目具体细节, 本文不详细叙述, 相关文件都是按照原始数据保存在文件中, 可以直接使用010 editor打开后搜索关键信息

评论

K

k333

这个人很懒,没有留下任何介绍

twitter weibo github wechat

随机分类

网络协议 文章:18 篇
其他 文章:95 篇
事件分析 文章:223 篇
前端安全 文章:29 篇
Web安全 文章:248 篇

扫码关注公众号

WeChat Offical Account QRCode

最新评论

Yukong

🐮皮

H

HHHeey

好的,谢谢师傅的解答

Article_kelp

a类中的变量secret_class_var = "secret"是在merge

H

HHHeey

secret_var = 1 def test(): pass

H

hgsmonkey

tql!!!

目录