逆向工程框架——Ghidra的简单使用

背景

Ghidra是由NSA开发的软件逆向工程(SRE)框架。 它有助于分析恶意代码和病毒等恶意软件,并可以让网络安全专业人员更好地了解其网络和系统中的潜在漏洞。

美国国家安全局在2019年3月举行的2019年RSA会议上首次公开演示时,将Ghidra作为开放源代码发布给公众。

GitHub地址:

https://github.com/NationalSecurityAgency/ghidra.git

前置环境

宣称可以在以下三个环境运行(暂时支持64位的系统)

  • Microsoft Windows 7 or 10 (64-bit)
  • Linux(64-bit, CentOS 7 is preferred)
  • macOS (OS X) 10.8.3+ (Mountain Lion or later)

使用

创建项目

windows直接双击ghidraRun.bat打开(linux和mac可以执行ghidraRun脚本)

首先我们创建一个project(上图我是已经新建了一个叫做test的项目)

选择Non-Shared Project (另一个选择Shared Project是会在本地监听一个端口,方便分享)

接下来填好路径和项目名就可以了

反编译程序

导入要分析的二进制文件,都选择默认选项就行

之后你导入的二进制文件就会出现在project下面(我的项目名是test)

双击对应的文件即可开始分析该文件,下面以Reverse-org.exe为例,双击后选择分析

然后选择分析选项

那怎么找到main函数呢 我们可以找到左边的functions,找到entry(当然有些程序直接有main,或者_start等关键字)

通过Filter搜索可以更快一点,双击即可再右边看到entry代码

查看反编译,对逆向熟悉的就知道下面这个就是main函数了

双击即可跳到面函数处,反编译窗口下拉即可看到main的代码

看函数的流程图可以点这个

最后说说现在想到的一些快捷键

  • g 跳到对应的地址
  • f 创建函数
  • ; 添加注释
  • L 重命名变量名,函数名

总结

试了下,发现几点

  • 可以对mips架构的程序进行反编译,这个比ida要好(官方说支持各种处理器指令集,试了下SPARC架构也可以,真牛逼!!!)
  • 反编译的c++代码好像更加简洁
  • 支持多平台,这个不错
  • 更重要的,开源,免费

缺点:不熟悉操作,暂时感觉没有ida方便,没发现有调试功能

参考链接


认识ARM64汇编

之前说过学习汇编就是学习寄存器和指令,查看代码请连接真机。

寄存器

arm64汇编中寄存器是64bit的,使用X[n]表示,低32位以w[n]表示

64位架构中有3164位的通用寄存器。

可以通过register read查看

继续阅读认识ARM64汇编

.align 5之类的知识

经常会看到 arm-linux 汇编中有如下的指令:

.align n 它的含义就是使得下面的代码按一定规则对齐。

.align n 指令的对齐值有两种方案:n 或 2^n 。各种平台最初的汇编器一般都不是gas ,采取方案12 的都很多,gas 的目标是取代原来的汇编器,必然要保持和原来汇编器的兼容,因此在gas 中如何解释.align 指令会显得有些混乱,原因在于保持兼容。arm-linux 是按照 2^n 的方案对齐的,需要说明的是这个对齐和ld-script 里的对齐不同,不是一会事。下面的英文就不同平台的对齐进行了说明:版本2.11.92.0.12gasinfo(Mandrake 8.2 上的) 这样说:

The way the required alignment is specified varies from system to system. For the a29k, hppa, m68k, m88k, w65, sparc, and Hitachi SH, and i386 using ELF format, the first expression is the alignment request in bytes. For example .align 8 advances the location counter until it is a multiple of 8. If the location counter is already a multiple of 8, no change is needed.

For other systems, including the i386 using a.out format, and the arm and strongarm, it is the number of low-order zero bits the location counter must have after advancement. For example `.align 3' advances the location counter until it a multiple of 8. If the location counter is already a multiple of 8, no change is needed.

从这段文字来看,ARM.align 5就是 25次方对齐,也就是 4 字节对齐,通过反汇编也可以看出对齐方式:

反汇编:

一些忠告:

In the future, everytime when you build an elf file, you need meantime created your map file. And then you will avoid mistakes like this align.

Also, please also pick up some linker script knowlege part. For embedded system, we frequently play the linker script to tune an image, for example, align some special section and so on for protection or/and cache purpose. wish helpful.

参考链接


.align 5之类的知识

王爽 汇编语言 课程设计2

附上BIOS内存分布图
386工作在实模式下内存情况分布图