angr-doc/examples/fauxware at master · angr/angr-doc
sudo apt-get install python3-dev libffi-dev build-essential virtualenvwrapper
mkvirtualenv --python=$(which python3) angr && pip install angr
virtualenvwrapper์์ python ๊ฐ์๋จธ์ ์ ๋ง๋ค๊ณ ๊ฑฐ๊ธฐ์ angr๋ฅผ ์ค์นํด์ ์ฌ์ฉํด๋ ๋๊ณ , ๊ทธ๋ฅ pip install angr๋ก ์ค์นํด์ ์ฌ์ฉํด๋ ๋๋ค.
deactivate # exit
workon angr # execute
import angr
angr๋ python์ผ๋ก ๋ง๋ค์ด์ก๊ธฐ ๋๋ฌธ์ python์์ angr๋ฅผ importํ๋ฉด angr API ๋ฑ์ ์ฌ์ฉํ ์ ์๋ค.
faux firmware์ ์์ค์ฝ๋๋ฅผ ๋ถ์ํด๋ณด์.
main() ํจ์
int main(int argc, char **argv)
{
char username[9];
char password[9];
int authed;
username[8] = 0;
password[8] = 0;
printf("Username: \n");
read(0, username, 8);
read(0, &authed, 1);
printf("Password: \n");
read(0, password, 8);
read(0, &authed, 1);
authed = authenticate(username, password);
if (authed) accepted();
else rejected();
}
accepted() ํจ์, rejected() ํจ์
int accepted()
{
printf("Welcome to the admin console, trusted user!\n");
}
int rejected()
{
printf("Go away!");
exit(1);
}
๋ ํจ์๋ ๋จ์ํ ๋ฌธ์์ด์ ์ถ๋ ฅํด์ฃผ๋๋ฐ, ๋ ํจ์ ํธ์ถ ํ ์ข ๋ฃ๋๋ค.
authenticated() ํจ์
char *sneaky = "SOSNEAKY";
int authenticate(char *username, char *password)
{
char stored_pw[9];
stored_pw[8] = 0;
int pwfile;
// evil back d00r
if (strcmp(password, sneaky) == 0) return 1;
pwfile = open(username, O_RDONLY);
read(pwfile, stored_pw, 8);
if (strcmp(password, stored_pw) == 0) return 1;
return 0;
}
sneaky ๊ฐ์ด "SOSNEAKY"์ธ๊ฑธ ์๊ธฐ ๋๋ฌธ์ ์์์ ์ฌ์ฉ์๊ฐ ๋ฐฑ๋์ด์ธ sneaky๋ฅผ ํตํด password๊ฐ ์ธ์ฆ๋๋๋ก ๋ง๋ ๋ค.
angr์ ๋ฐ์ด๋๋ฆฌ ๋ก๋ํ๊ธฐ
project = angr.Project('fauxware', auto_load_libs=False)
‘fauxware’ ๋ฐ์ด๋๋ฆฌ ๋ถ์ ์ํด angr.Project() API ์ฌ์ฉํ์ฌ angr Project๋ฅผ ์์ฑํ๋ค.
state ์์ฑ
state = project.factory.entry_state()
๋ฐ์ด๋๋ฆฌ entrypoint์์ ํ๋ก๊ทธ๋จ ์ํ๋ฅผ SimState ๊ฐ์ฒด๋ก ๋ฆฌํดํ๋ค.
Simulation Managers ์ฌ์ฉ1
simulation manager๋ ์ํ๋ค์ stash๋ก ๊ตฌ์ฑํ๊ณ filter, merge, move์ ์์ ์ ํ ์ ์๋ค.
๋ํดํธ stash๋ active stash๋ก, ์๋ก์ด simulation manager๋ฅผ ์ด๊ธฐํํ๋ฉด ์ํ๊ฐ active stash๋ก ๋ค์ด๊ฐ๋ค.
simulation manager๋ ํ basic block์์ ๋ชจ๋ ์ํ๋ฅผ ์ฒ๋ฆฌํ๋ฉด stash์ ๋ฃ๋๋ค.
>>> simgr = proj.factory.simgr(state)
>>> simgr.run()
<SimulationManager with 3 deadended>
symbolic branch ์กฐ๊ฑด์ธ ์ํ๊ฐ ๋ฐ๊ฒฌ๋๋ฉด, ๋ ๋ถ๊ธฐ์ ๋ํ successor ์ํ๊ฐ stash์ ๋ค์ด๊ฐ๊ณ ๋ ์ํ์ ๋ํด ์คํ์ ์งํํ ์ ์๋ค. run() ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ ๋ ์ด์ ์ฒ๋ฆฌํ ๊ฒ์ด ์์ ๋๊น์ง ์งํํ ์๋ ์๋ค.
>>> simgr.move(from_stash='deadended', to_stash='authenticated', filter_func=lambda s:b'Welcome' in s.posix.dumps(1))
<SimulationManager with 1 deadended, 2 authenticated>
deadended state๋ exit ์์คํ ์ฝ ๋ฑ์ผ๋ก ์ธํด ์ํ๊ฐ successor๋ฅผ ๋ง๋๋๋ฐ ์คํจํ ๊ฒ์ผ๋ก active stash์์ ์ ๊ฑฐ๋๊ณ deadended stash์ ๋ค์ด๊ฐ๋ค. ๋ฐ๋ผ์ ์ฃผ์ด์ง ๋ฐ์ด๋๋ฆฌ์์ ์ ์ ์ข ๋ฃ๋ ๋น์ ์ ์ข ๋ฃ๋ exit() ์์คํ ์ฝ์ด ํธ์ถ๋๋ฉด์ ์ข ๋ฃ๋๋ฏ๋ก ๋ ์ํ ๋ชจ๋ deadended stash์ ์์ ๊ฒ์ด๋ค.
deadended stash์์ ์ํ๊ฐ ๊ฐ์ง๋ ๋ฌธ์์ด๋ค์ ์ดํด๋ณด๋ฉด, dumps(1)์ authenticated ์กฐ๊ฑด์ผ ๋ ์ถ๋ ฅ๋๋ ๋ฌธ์์ด ๋๋ authenticated๊ฐ ์๋ ๋ ์ถ๋ ฅ๋๋ ๋ฌธ์์ด์ด ์๋ค. ๋ฐ๋ผ์ deadended stash ๋ด ์ํ ์ค์์ dumps(1)๋ก ๊ตฌํ ๋ฌธ์์ด์ด ‘Welcome’์ ํฌํจํ๋ฉด authenticated stash๋ก ์ฎ๊ธด๋ค.
move() ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ ์ถ๋ ฅ์ ์ฃผ์ด์ง ๋ฌธ์์ด์ ๊ฐ์ง๊ณ ์์ผ๋ฉด from_stash์์ to_stash๋ก ์ํ๋ฅผ ์ฎ๊ธธ ์ ์๋ค.
3๊ฐ์ deadended stash์ ์๋ ์ํ ์ค ‘Welcome’ ๋ฌธ์์ด์ ์ถ๋ ฅ์ ํฌํจํ๋ ์ํ 2๊ฐ๊ฐ authenticated stash๋ก ์ด๋์ํจ๋ค.
>>> for s in simgr.deadended + simgr.authenticated:
... print(hex(s.addr))
...
0x746a70
0xa00058
0xa00058
>>> simgr.one_deadended
<SimState @ 0x746a70>
>>> simgr.mp_authenticated
MP([<SimState @ 0xa00058>, <SimState @ 0xa00058>])
one_์ stash ์ด๋ฆ ์์ ์ฌ์ฉํ๋ฉด stash ๋ด ์ฒซ ๋ฒ์งธ state๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋ค.
mp_์ stash ์ด๋ฆ ์์ ์ฌ์ฉํ๋ฉด stash ๋ด ๋ชจ๋ ์ํ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋ค.
>>> simgr.mp_authenticated.posix.dumps(0)
MP([b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00SOSNEAKY\\x00', b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\\x04\\x10\\x10\\x80\\x80\\x80\\x00\\x00'])
stash ๋ด ์ํ์ ๋ํ ๋ด์ฉ์ ๊ตฌํ๋ค.
#!/usr/bin/python
import angr
# load binary into angr project
project = angr.Project('fauxware', auto_load_libs=False)
# program state in entrypoint
state = project.factory.entry_state()
# Create Simulation Manager
simgr = project.factory.simgr(state)
# step until nothing left to step
simgr.run()
print(simgr)
print(simgr.deadended)
for i in range(len(simgr.deadended)):
str = simgr.deadended[i].posix.dumps(1)
if b'Welcome' in str:
print(simgr.deadended[i], end=' ')
print(str)
Simulation Manager ์ฌ์ฉ2
>>> import angr
>>> p = angr.Project('fauxware')
>>> state=p.factory.entry_state()
>>> simgr=p.factory.simgr(state)
>>> simgr.run(until=lambda sm:len(sm.active) > 1)
<SimulationManager with 2 active>
>>> simgr.active[0].posix.dumps(1)
b'Username: \\nPassword: \\n'
>>> simgr.active[0].posix.dumps(2)
b''
>>> simgr.active[0].posix.dumps(0)
b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00SOSNEAKY\\x00'
>>> simgr.active[1].posix.dumps(0)
b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00@@\\x10@\\x01@\\x02\\x00\\x00'
>>> simgr.active[1].posix.dumps(1)
b'Username: \\nPassword: \\n'
>>> for i in range(len(simgr.active)):
... if b'SOSNEAKY' in simgr.active[i].posix.dumps(0):
... r = simgr.active[i].posix.dumps(0)
...
>>> print(r)
b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00SOSNEAKY\\x00'
>>> assert b'SOSNEAKY' in simgr.active[1].posix.dumps(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>> import sys
>>> sys.stdout.buffer.write(simgr.active[0].posix.dumps(0))
SOSNEAKY18
GoogleCTF 2016 unbreakable_0 (0) | 2022.05.11 |
---|---|
[DefCamp CTF 2015 Quals] Entry Language (Reverse 100) (0) | 2022.05.11 |
[QWBCTF 2018] core writeup (kernel exploit) (0) | 2022.03.22 |
[0CTF 2018] baby kernel 2(kernel exploit, double fetch, race condition) (0) | 2021.11.11 |
[CISCN CTF 2017] babydriver (kernel exploit, kUAF) (0) | 2021.11.11 |