# Docs

## Câu lệnh chạy

```bash
python ./file_python_angr [file_binary]
```

## Khai báo thư viện

```python
import angr
import sys
```

## Định nghĩa và gọi hàm main

{% code overflow="wrap" fullWidth="false" %}

```python
def main(argv):
    pass
    
if __name__ == "__main__":
    main(sys.argv)
```

{% endcode %}

## Tạo đối tượng Angr

```python
# khai báo file nhị phân thông qua path_to_binary
path_to_binary = ??? #:string
# Tạo đối tượng angr để phân tích
project = angr.Project(path_to_binary)
# or
project = angr.Project(argv[1])
```

## Xác định địa chỉ bắt đầu thực thi

> * Cho Angr biết nơi bắt đầu thực thi (nên được bắt đầu từ hàm main() hay ở đâu khác?)

{% code overflow="wrap" %}

```python
initial_state = project.factory.entry_state(add_options = { angr.options.SYMBOL_FILL_UNCONSTRAINED_MEMORY, angr.options.SYMBOL_FILL_UNCONSTRAINED_REGISTERS}
```

{% endcode %}

## Tạo trình quản lý mô phỏng (simgr)

> * Tạo trình quản lý mô phỏng được khởi tạo với địa chỉ bắt đầu mà ta đã thiết lập trước.

{% code overflow="wrap" %}

```python
simulation = project.factory.simgr(initial_state)
```

{% endcode %}

## Angr - Find

> * Khám phá tệp nhị phân để tìm địa chỉ mà mình muốn đến; Hàm này sẽ tiếp tục thực thi khi nó tìm thấy giải pháp hoặc nó.

{% code overflow="wrap" %}

```python
print_good_address = ??? #:integer (probably in hexadecimal)
simulation.explore(find=print_good_address)
```

{% endcode %}

## Kiểm tra xem có tìm thấy solution

> * Phương thức **simulation.explore()** sẽ thiết lập **simulation.found** thành một danh sách các trạng thái mà nó có thể tìm thấy để đạt được hướng dẫn mà chúng ta yêu cầu nó tìm kiếm.
> * Phương thức này sẽ dừng sau khi tìm thấy một trạng thái duy nhất đến địa chỉ đích.

```python
if simulation.found:
  solution_state = simulation.found[0]
  print(solution_state.posix.dumps(sys.stdin.fileno()).decode())
else:
  raise Exception('Could not find the solution')
```

## Angr - avoid

> * Đối với phương thức này thì sẽ cho biết thêm trạng thái không muốn đạt tới thông qua **avoid.** Mục đích nếu tập nhị phân lớn sẽ giúp tiết kiệm thời gian.

```python
simulation.explore(find=print_good_address, avoid=will_not_succeed_address)
```


---

# 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/challenges-angry/docs.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.
