UnicornPlayground
Simple solutions for two small challenges from PicoCTF2018. The idea was to play with Unicorn Engine and emulate the provided assembly code.
Install / Use
/learn @nahueldsanchez/UnicornPlaygroundREADME
Playing with Unicorn, Capstone and Keystone engine.
Solving PicoCTF 2018 assembly-0 and assembly-1 challenges
Installing unicorn engine (CPU emulator framework) on Ubuntu 18.04.1 LTS
Steps based on 1
- Install libglib2.0-dev package by running:
sudo apt-get install libglib2.0-dev - Clone Unicorn-Engline's repository:
git clone https://github.com/unicorn-engine/unicorn.git - Open the unicorn directory:
cd unicorn - Run
./make.sh - Export LD_LIBRARY_PATH=$LD_LIBRARY_PATH: </path_where_libunicorn.so.1_is_stored>
- Excute
./samples/sample_all.sh#Samples should run without problems
At this point we've installed unicorn engine's core. Let's install the Python binding:
- Create
virtualenvand activate it - Run
cd bindings/python - Run
make - Run
python setup.py install - Excute
./sample_x86.py#Samples should run without problems
Now we've everything installed.
Installing keystone engine (The Ultimate Assembler Framework) on Ubuntu 18.04.1 LTS
As a prerequisite cmake must be installed in the target system.
- Clone keystone-engine repository:
git clone https://github.com/keystone-engine/keystone.git - Open the keystone directory by running:
cd keystone/ - Create a new directory:
mkdir build - Open the directory:
cd build/ - Excute
../make-share.sh - Run export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<path_to_keystone>/build/llvm/lib/
- Test if the installation was successful.
- Go to kstoolL
cd kstool - ./kstool x32 "add eax, ebx", should return add eax, ebx = [ 01 d8 ]
Now, we'll install the Python bindings:
- create a
virtualenvand activate it (Or better, use the previously created virtualenv) - Run:
pip install keystone-engine - To test it, run
pythonand try to import keystone:from keystone import *
Installing Capstone engine (The Ultimate Disassembler) on Ubuntu 18.04.1 LTS
- Clone Capstone.Capstone repository:
git clone https://github.com/aquynh/capstone.git - Open the directory:
cd capstone - Run:
./make.sh - export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:</path_where_libcapstone.so.4_is_stored>
- We've installed Capstone successfully. Let's test it.
- Go to the Capstone installation directory
./test subdirectory. - Execute some tests; everything should run flawlessly.
Now, we'll install the Python binding:
- Create a
virtualenvand activate it (Or better, use the previously created virtualenv) - Run
pip install capstone - To test it, run
pythonand execute:from capstone import *
Setting up the virtualenv
I've added the following lines to the "activate" file to avoid executing them every time I have to use the tools:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:</path_where_libcapstone.so.4_is_stored>
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<path_to_keystone>/build/llvm/lib/
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:</path_where_libunicorn.so.1_is_stored>
Solving the challenges
Once we've everything installed we'll be able to "run" the provided assembly code and luckily answer the following question: What's stored in EAX at the end of the execution?
To answer that —and justify installing all the tools too :stuck_out_tongue_winking_eye: — I've created two Python scripts that perform two tasks:
- Take assembly code and translate it to machine code.
- Take the previously obtained machine code and emulates it using Unicorn Engine
Once the emulation is completed the scripts print the value of the EAX register.
You can find two folders:
picoCTF2018_assembly-0andpicoCTF2018_assembly-1
which contain the Python scripts to solve both challenges. I've added some hooks to understand how to use this functionality as it may be useful for other tasks.
I've also had to modify the provided assembly a little bit to be able to emulate it.
I've performed the following changes:
:ballot_box_with_check: added some push instructions at the beginning to pass the arguments to the functions
:ballot_box_with_check: added some fake return address (push 0x00) to align the stack
:ballot_box_with_check: removed the ret instruction in assembly-1 as the emulation tried to continue once all the provided code was already executed.
References
[1] https://github.com/unicorn-engine/unicorn/wiki/quick-start
Related Skills
node-connect
338.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
338.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.4kCommit, push, and open a PR
