Angr CTF 从入门到入门(1)

article/2025/11/8 10:33:57

angr是一个很厉害的二进制分析工具,能够实现很多自动化的逆向工作。最近正在学习,在看文档的时候发现了有个angrCTF的实战可以练习angr。

angrCTF:http://angr.oregonctf.org/
angr文档:https://docs.angr.io/

这篇文章会是这个系列的第一篇文章。写这个系列也是因为我本身是逆向新手,记忆力还不行,估计过几天就忘记这个工具怎么用了。故在此记录。第一篇就只记录angrCTF的前三道题,感觉这三道题比较类似。

另外,还发现了一个大佬写的关于这方面的教程,贴在这里(因为我写的太垃圾了):

https://github.com/ZERO-A-ONE/AngrCTF_FITM

00_angr_find

题目告诉我们00_angr_find这个二进制文件是一个输入正确密码,然后打印Good Job.否则输出Try Again.的小程序。我们的目标就是去找到这个密码。只要将下面的python代码处的???填好即可。

# Before you begin, here are a few notes about these capture-the-flag
# challenges.
#
# Each binary, when run, will ask for a password, which can be entered via stdin
# (typing it into the console.) Many of the levels will accept many different
# passwords. Your goal is to find a single password that works for each binary.
#
# If you enter an incorrect password, the program will print "Try again." If you
# enter a correct password, the program will print "Good Job."
#
# Each challenge will be accompanied by a file like this one, named
# "scaffoldXX.py". It will offer guidance as well as the skeleton of a possible
# solution. You will have to edit each file. In some cases, you will have to
# edit it significantly. While use of these files is recommended, you can write
# a solution without them, if you find that they are too restrictive.
#
# Places in the scaffoldXX.py that require a simple substitution will be marked
# with three question marks (???). Places that require more code will be marked
# with an ellipsis (...). Comments will document any new concepts, but will be
# omitted for concepts that have already been covered (you will need to use
# previous scaffoldXX.py files as a reference to solve the challenges.) If a
# comment documents a part of the code that needs to be changed, it will be
# marked with an exclamation point at the end, on a separate line (!).import angr
import sysdef main(argv):# Create an Angr project.# If you want to be able to point to the binary from the command line, you can# use argv[1] as the parameter. Then, you can run the script from the command# line as follows:# python ./scaffold00.py [binary]# (!)path_to_binary = argv[1]  # :stringproject = angr.Project(path_to_binary)# Tell Angr where to start executing (should it start from the main()# function or somewhere else?) For now, use the entry_state function# to instruct Angr to start from the main() function.initial_state = project.factory.entry_state()# Create a simulation manager initialized with the starting state. It provides# a number of useful tools to search and execute the binary.simulation = project.factory.simgr(initial_state)# Explore the binary to attempt to find the address that prints "Good Job."# You will have to find the address you want to find and insert it here. # This function will keep executing until it either finds a solution or it # has explored every possible path through the executable.# (!)print_good_address = ???  # :integer (probably in hexadecimal)simulation.explore(find=print_good_address)# Check that we have found a solution. The simulation.explore() method will# set simulation.found to a list of the states that it could find that reach# the instruction we asked it to search for. Remember, in Python, if a list# is empty, it will be evaluated as false, otherwise true.if simulation.found:# The explore method stops after it finds a single state that arrives at the# target address.solution_state = simulation.found[0]# Print the string that Angr wrote to stdin to follow solution_state. This # is our solution.print (solution_state.posix.dumps(sys.stdin.fileno()))else:# If Angr could not find a path that reaches print_good_address, throw an# error. Perhaps you mistyped the print_good_address?raise Exception('Could not find the solution')if __name__ == '__main__':main(sys.argv)

下面这个angr程序就是去找到一个地址能够打印Good Job.。然后angr就会一直执行,直到找到一个满足条件的输入或者遍历完所有可执行路径。

然后问题来了,如何才能找到打印Good Job.的地址呢?一开始卡了半天,后面我试着用ida打开00_angr_find文件,生成了二进制程序的控制流图。
在这里插入图片描述
然后试着将有Good Job.这个基本块的地址放到问号里,结果输出一个字符串,再将这个字符串在angrCTF网站输进去,显示成功。真的是非常的amazing!
在这里插入图片描述

01_angr_avoid

万事开头难,搞定了第一个后,第二个就很简单了。python代码如下。主要是由于二进制非常大,我们需要设置一些angr避免执行的地址,加快符号执行的速度。

import angr
import sysdef main(argv):path_to_binary = argv[1]project = angr.Project(path_to_binary)initial_state = project.factory.entry_state()simulation = project.factory.simgr(initial_state)# Explore the binary, but this time, instead of only looking for a state that# reaches the print_good_address, also find a state that does not reach # will_not_succeed_address. The binary is pretty large, to save you some time,# everything you will need to look at is near the beginning of the address # space.# (!)print_good_address =  ???will_not_succeed_address = ???simulation.explore(find=print_good_address, avoid=will_not_succeed_address)if simulation.found:solution_state = simulation.found[0]print (solution_state.posix.dumps(sys.stdin.fileno()))else:raise Exception('Could not find the solution')if __name__ == '__main__':main(sys.argv)

代码中的第一个问号是表示打印good job的地址,第二个问号填的是打印try again的地址。还是和刚才一样,用ida打开二进制文件,会发现这次二进制文件比较大,不好生成可视化的控制流图。不过不影响。我们可以直接手撕汇编代码。
在这里插入图片描述
点击ida左侧的窗口,定位到main函数,在main函数上方有这样一段代码。打印try again的地址是804860A,所以设置avoid的地址是804860A,另外因为在将good job放在栈后,就跳转到了804861A。所以盲猜打印good job的地址为804861A。

最后也非常的amazing,竟然又给我蒙对了。
在这里插入图片描述

02_angr_find_condition

能够搜索到达某个指令的状态虽然很有用,但是在某些情况瞎,我们可能无法知道某个特定指令的地址(比如我这个菜鸡就不会熟练使用ida)。这个例子里就是教我们如何找到一个打印Good Job.的状态。这里改动的代码在is_successful(state)should_abort(state)这两个函数的返回语句处。主要思想就是当判断输出的值是否有Good Job. ,有的话就是成功了,否则如果是Try Again就需要终止。

这样就减少了打开IDA去分析指令地址的步骤,实现了自动化逆向的过程,很赞!

# It is very useful to be able to search for a state that reaches a certain
# instruction. However, in some cases, you may not know the address of the
# specific instruction you want to reach (or perhaps there is no single
# instruction goal.) In this challenge, you don't know which instruction
# grants you success. Instead, you just know that you want to find a state where
# the binary prints "Good Job."
#
# Angr is powerful in that it allows you to search for a states that meets an
# arbitrary condition that you specify in Python, using a predicate you define
# as a function that takes a state and returns True if you have found what you
# are looking for, and False otherwise.import angr
import sysdef main(argv):path_to_binary = argv[1]project = angr.Project(path_to_binary)initial_state = project.factory.entry_state()simulation = project.factory.simgr(initial_state)# Define a function that checks if you have found the state you are looking# for.def is_successful(state):# Dump whatever has been printed out by the binary so far into a string.stdout_output = state.posix.dumps(sys.stdout.fileno())# Return whether 'Good Job.' has been printed yet.# (!)return state.solver.is_true("Good Job." in str(stdout_output))  # :boolean# Same as above, but this time check if the state should abort. If you return# False, Angr will continue to step the state. In this specific challenge, the# only time at which you will know you should abort is when the program prints# "Try again."def should_abort(state):stdout_output = state.posix.dumps(sys.stdout.fileno())return state.solver.is_true("Try again." in str(stdout_output))  # :boolean# Tell Angr to explore the binary and find any state that is_successful identfies# as a successful state by returning True.simulation.explore(find=is_successful, avoid=should_abort)if simulation.found:solution_state = simulation.found[0]print (solution_state.posix.dumps(sys.stdin.fileno()))else:raise Exception('Could not find the solution')if __name__ == '__main__':main(sys.argv)

结果自然也让我非常amazing!solved!

在这里插入图片描述


http://chatgpt.dhexx.cn/article/iYR8tFvn.shtml

相关文章

angr的windows安装——萌新

window10中符号执行(angr)的安装——小白篇 最近做题的时候遇到了一些需要使用angr来完成的题目,之后在网上搜了好久发现大多是Linux以及其它系统的angr安装教程。windows系统的安装方法几乎没有。身为一个萌新,猜测同类们应该也会…

Angr 安装

1、安装必要的软件环境 sudo apt-get install python3-dev libffi-dev build-essential virtualenvwrapper 2、开始正式安装angr mkvirtualenv --python$(which python3) angr && pip install angr 3、遇到 mkvirtualenv: command not found 问题 在终端执行以下命…

Angr 01 avoid 忽略的条件

01_angr_avoid无法反编译主函数,模块比较多,加上avoid过滤条件之后,就没那么慢了。 import angr …

图解angr中两种CFG的区别

angr里提供两种CFG的生成,一种是CFGFast,一种是CFGEmulated。这两种究竟有什么不同呢? 本文主要是用图来说明下这个问题。可能回答的不是很完整。 CFGFast这种CFG生成的比较快,但是没有考虑上下文关系。比如函数A调用了printf函…

Angr 02_angr_find_condition 根据输出状态条件搜索正确输入

explore的find和avoid支持函数作为参数,根据函数返回值来判断是否成功。Angr把状态传入了函数,从而判断成功与否。 import angr import sysdef main(argv):path_to_binary argv[1]project angr.Project(path_to_binary)initial_state project.factor…

angr的安装

ubuntu要先安装pip,ANGR官方给出的为pip命令pip install angr; 就算将pip改为apt-get 也不行。 借鉴Ubuntu安装pip的安装方式,安装pip,然后运行pip install angr,结束。 angr安装完后,要安装图形生成插件…

angr 学习笔记

附件,参考1,参考2 符号执行原理 基本概念 即初始参数用变量代替,模拟程序执行过程,维护执行到各个位置时的状态(用各个变量之间的代数关系表示)。 符号状态(Symbolic State) 当前…

利用angr获取CFG

静态二进制分析中,对于程序控制流图CFG的计算是很基础且重要的一步,很多的分析是要建立在CFG的基础上。angr作为二进制分析工具,当然提供了CFG功能,下面我们就来探索下要如何使用angr计算CFG,以及其中的坑。 angr中的…

Angr 内存符号化 05_angr_symbolic_memory

说明 通过查看下面的源码或IDA汇编&#xff0c;可知此例需要输入4个输入字符串&#xff0c;给每个输入字符串赋值给了user_input数组这块已经开辟的内存空间&#xff0c;我们可以监测user_input在内存中的值来获取最终的答案。 随机生成的源码 #include <stdio.h> #in…

Angr学习笔记

Angr学习笔记 前言 本文记录一下Angr的基本使用方法&#xff0c;主要是基于Github上的开源项目以及笔记AngrCTF_FITM整理&#xff0c;Angr在逆向方面确实用处比较大&#xff0c;特此记录一下。 什么是Angr angr是一个用于分析二进制文件的python框架。它专注于静态和符号分…

angr纠错和易错点

现在环境已经没有问题了。我运行了几个代码&#xff08;使用python3&#xff09;&#xff0c;都会出现不同的问题。 运行报错 解决方法&#xff1a; 添加b 运行报错&#xff1a; 解决方法&#xff1a; 删掉cast_to; 因为python2中str就是字节流 python3中不一样&#xff0c…

git本地分支管理

文章目录 一、master分支没有改变&#xff0c;合并其他分支二、master分支有改变&#xff0c;合并其他分支 作为开发人员&#xff0c;我们应该尽可能多地建立分支&#xff0c;在分支上进行开发&#xff0c;功能测试稳定后&#xff0c;再将分支上地代码合并到指定的分支 git bra…

Git 分支管理

Git 分支管理 一、主分支Master 首先&#xff0c;代码库应该有一个、且仅有一个主分支。所有提供给用户使用的正式版本&#xff0c;都在这个主分支上发布。 Git主分支的名字&#xff0c;默认叫做Master。它是自动建立的&#xff0c;版本库初始化以后&#xff0c;默认就是在主分…

git分支管理策略

git分支管理策略 1 总览 git 的分支整体预览图如下&#xff1a; 从上图可以看到主要包含下面几个分支&#xff1a; master&#xff1a;git默认主分支&#xff08;这里不作操作&#xff09;。 stable&#xff1a;稳定分支&#xff0c;替代master&#xff0c;主要用来版本发布。…

IDEA中使用Git功能和IDEA中的Git分支管理

IDEA中使用Git功能 IDEA中创建Git仓库 1、设置Git程序的路径 2、设置编译器的GitHub账号 3、新建项目 4、发布项目到GitHub IDEA向Git提交修改后的代码 1、实现git add 2、开始commit 3、实现git commit -m 4、实现git push origin master 项目成员使用IDEA的Git功…

【Git-10】Eclipse中Git分支管理

分支管理&#xff0c;是 Git 开发中的一个非常有效的团队开发策略。多个程序员并行开发&#xff0c;每个程序员可以定义各自的分支&#xff0c;在自己的分支上开发工程。再开发结束测试完毕后&#xff0c;再合并到主干工程中&#xff0c;一次性提交到远程。由其他程序员使用。 …

Git 分支管理最佳实践

Git 是目前最流行的源代码管理工具。大量的软件项目由 GitHub、Bitbucket 和 GitLab 这样的云服务平台或是私有的 Git 仓库来管理。在使用 Git 时通常会遇到的一个问题是采用何种分支管理实践&#xff0c;即如何管理仓库中作用不同的各类分支。和软件开发中的其他实践一样&…

Git分支管理办法,每个团队不一样,仅供参考!

原创不易&#xff0c;转载注明出处&#xff0c;喜欢就点个赞吧&#xff01; 网上有很多文章&#xff0c;很少有实际具体的流程图说明&#xff0c;我根据我们团队的情况&#xff0c;画了几张简图&#xff0c;仅供参考。 每个公司的代码管理方式都不一样 主要有两种 1.TrunkB…

Git分支管理与常用命令

一、分支管理 Git在创建分支仅是生成了一个指针又称快照&#xff08;有的工具则进行了物理拷贝&#xff09;&#xff0c;这样在使用起来就非常的方便。方便的同时问题随之而来&#xff0c;如果不加管理&#xff0c;那么就会到处开枝散叶&#xff0c;完全看不出主干。因此&…

复杂项目的版本管理及git分支管理建议

在复杂项目中&#xff0c;特别是多团队的快速迭代中&#xff0c;版本管理与分支管理&#xff0c;总是我们难以回避的问题&#xff0c;这里分享一下我们在团队中使用的规范&#xff0c;以及对应的每一步的步骤。 为什么需要GIT使用建议 团队开发中&#xff0c;遵循一个合理、清…