# angr

Useful tools

```python
# Standand command handler for lldb in python
def lldb_cmd(cmd):
    return_obj = lldb.SBCommandReturnObject()
    ci = debugger.GetCommandInterpreter()
    ci.HandleCommand(cmd, return_obj)
    return print(return_obj.GetOutput())

import lldb
```

## Solution

{% code overflow="wrap" %}

```shell
# RE workflow, feel free to use your preferred tooling
$ file name_file
# crackme0x02: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.9, not stripped

$ ldd crackme0x02
        linux-gate.so.1 (0xf7ed6000)
        libc.so.6 => /lib32/libc.so.6 (0xf7c00000)
        /lib/ld-linux.so.2 (0xf7ed8000)
$ strings -min_legth name_file
#ví dụ strings -12 crackme0x02
```

{% endcode %}

* Ví dụ trên ta thấy check file này thì đây là file có dạng `dynamically linked` - là dạng có sử dụng các thư viện bên ngoài để tối ưu hóa, nó cần sử dụng các thành phần bên ngoài để chạy chính xác. Thông thường các thành phần bên ngoài này là các thư viện bình thường, có chứa các chức năng phổ biến như mở tệp tạo ô cắm mạng,...
* Mặt khác nếu tĩnh thì file đó sẽ có chứa tất cả các thư viện bao gồm. Nó là cho file đó lớn hơn, nhưng di động hơn (ví dụ khi sử dụng hệ thống khác).
* Mẹo để xem các phụ thuộc cơ bản thì ta sử dụng tiện ích công cụ LDDTREE (ldd)

### Setting up the environment - lldb

```python
BIN_PATH = "crackme0x02"
arch = 'i386'

debugger =  lldb.SBDebugger.Create()
target = debugger.CreateTargetWithFileAndArch(BIN_PATH, arch)
```

Ví dụ hiển thị code assembly:

```
lldb_cmd('disasemble -F intel -n main')

lldb_cmd('x/20bc 0x804857F')
```

### Set up environment with angr

```python
import angr
```

### Load the binary as a new project

```python
project = Project('path_to_binary')
```

### Store the entry state (lưu trữ trạng thái nhập)

```python
state = project.factory.entry_state()
```

Hoặc đôi khi cũng là điểm bắt đầu của path (start of the path)

```python
path = project.factory.entry_state()
```

Hoặc đôi khi the symbolic execution group can be called a pathgroup, which is just:

```python
pathgroup = project.factory.simgr()
```

### Walk through the binary until it reaches a branch.

```
pathgroup.run(
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://viettaliii.gitbook.io/home/education/reverse-engineering/symbolic-analysis/angr-and-symbolic-execution/angr.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
