Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aarch64: add instructions #1419

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added examples/script/aarch64/basic
Binary file not shown.
28 changes: 28 additions & 0 deletions examples/script/aarch64/basic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// gcc -g -static -o basic basic.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char* argv[], char* envp[]){
unsigned int cmd;

if (read(0, &cmd, sizeof(cmd)) != sizeof(cmd))
{
printf("Error reading stdin!");
exit(-1);
}

if (cmd > 0x41)
{
printf("Message: It is greater than 0x41\n");
}
else
{
printf("Message: It is less than or equal to 0x41\n");
}

return 0;
}


71 changes: 71 additions & 0 deletions examples/script/aarch64/basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env python3

import os
import struct
import sys

from manticore.native import Manticore

# Examples:
# printf "\x41\x00\x00\x00" | PYTHONPATH=. ./examples/script/aarch64/basic.py
# printf "++\x00\x00" | PYTHONPATH=. ./examples/script/aarch64/basic.py
# printf "++++" | PYTHONPATH=. ./examples/script/aarch64/basic.py
# printf "ffffff" | PYTHONPATH=. ./examples/script/aarch64/basic.py

DIR = os.path.dirname(__file__)
FILE = os.path.join(DIR, 'basic')
STDIN = sys.stdin.readline()

# Avoid writing anything to 'STDIN' here. Do it in the 'init' hook as that's
# more flexible.
m = Manticore(FILE, concrete_start='', stdin_size=0)


@m.init
def init(state):
state.platform.input.write(state.symbolicate_buffer(STDIN, label='STDIN'))


# Hook the 'if' case.
@m.hook(0x4006bc)
def hook_if(state):
print('hook if')
state.abandon()


# Hook the 'else' case.
@m.hook(0x4006cc)
def hook_else(state):
print('hook else')
# See how the constraints are affected by input.
print_constraints(state, 6)

w0 = state.cpu.W0

if isinstance(w0, int): # concrete
print(hex(w0))
else:
print(w0) # symbolic

solved = state.solve_one(w0)
print(struct.pack("<I", solved))


# Hook 'puts' in the 'else' case.
@m.hook(0x4006d4)
def hook_puts(state):
print('hook puts')
cpu = state.cpu
print(cpu.read_string(cpu.X0))


def print_constraints(state, nlines):
i = 0
for c in str(state.constraints).split('\n'):
if i >= nlines:
break
print(c)
i += 1


m.run()
Binary file added examples/script/aarch64/hello42
Binary file not shown.
8 changes: 8 additions & 0 deletions examples/script/aarch64/hello42.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// gcc -g -static -o hello42 hello42.c
#include <stdio.h>

int main()
{
puts("hello");
return 42;
}
50 changes: 50 additions & 0 deletions examples/script/aarch64/hello42.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env python3

import os

from manticore.native import Manticore

# Modified 'count_instructions.py' to demonstrate execution of a
# statically-linked "Hello, world!" AArch64 binary.

DIR = os.path.dirname(__file__)
FILE = os.path.join(DIR, 'hello42')

if __name__ == '__main__':
m = Manticore(FILE)

with m.locked_context() as context:
context['count'] = 0

@m.hook(None)
def explore(state):
with m.locked_context() as context:
context['count'] += 1

if state.cpu.PC == 0x406f10: # puts
s = state.cpu.read_string(state.cpu.X0)
assert s == 'hello'
print(f'puts argument: {s}')

elif state.cpu.PC == 0x40706c: # puts result
result = state.cpu.X0
assert result >= 0
print(f'puts result: {result}')

elif state.cpu.PC == 0x415e50: # exit
status = state.cpu.X0
syscall = state.cpu.X8
assert syscall == 94 # sys_exit_group
print(f'exit status: {status}')

def execute_instruction(self, insn, msg):
print(f'{msg}: 0x{insn.address:x}: {insn.mnemonic} {insn.op_str}')

m.subscribe('will_execute_instruction', lambda self, state, pc, insn:
execute_instruction(self, insn, 'next'))
m.subscribe('did_execute_instruction', lambda self, state, last_pc, pc, insn:
execute_instruction(self, insn, 'done'))

m.run(procs=1)

print(f"Executed {m.context['count']} instructions")
Loading