### Python API Example for Triton Source: https://github.com/jonathansalwan/triton/blob/master/README.md Demonstrates basic usage of the Triton Python API, including context creation, register manipulation, instruction processing, and symbolic expression handling. Ensure Triton is installed before running. ```python from triton import * >>> # Create the Triton context with a defined architecture >>> ctx = TritonContext(ARCH.X86_64) >>> # Define concrete values (optional) >>> ctx.setConcreteRegisterValue(ctx.registers.rip, 0x40000) >>> # Symbolize data (optional) >>> ctx.symbolizeRegister(ctx.registers.rax, 'my_rax') >>> # Execute instructions >>> ctx.processing(Instruction(b"\x48\x35\x34\x12\x00\x00")) # xor rax, 0x1234 >>> ctx.processing(Instruction(b"\x48\x89\xc1")) # mov rcx, rax >>> # Get the symbolic expression >>> rcx_expr = ctx.getSymbolicRegister(ctx.registers.rcx) >>> print(rcx_expr) (define-fun ref!8 () (_ BitVec 64) ref!1) ; MOV operation - 0x40006: mov rcx, rax >>> # Solve constraint >>> ctx.getModel(rcx_expr.getAst() == 0xdead) {0: my_rax:64 = 0xcc99} >>> # 0xcc99 XOR 0x1234 is indeed equal to 0xdead >>> hex(0xcc99 ^ 0x1234) '0xdead' ``` -------------------------------- ### Install Triton Library and Headers Source: https://github.com/jonathansalwan/triton/blob/master/src/libtriton/CMakeLists.txt Installs the Triton library, its public headers, and related files to the specified installation directories. ```cmake # Install triton install( TARGETS triton EXPORT tritonTargets PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/triton" INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ) ``` -------------------------------- ### Install Triton using pip Source: https://github.com/jonathansalwan/triton/blob/master/README.md Install the Triton library using pip. This is the recommended method for most users. ```console pip install triton-library ``` -------------------------------- ### Get Python Path Information Source: https://github.com/jonathansalwan/triton/blob/master/README.md A Python script to retrieve and print system-specific Python installation paths, useful for manual configuration. ```python from sysconfig import get_paths info = get_paths() print(info) ``` -------------------------------- ### Install Package Configuration Files Source: https://github.com/jonathansalwan/triton/blob/master/src/libtriton/CMakeLists.txt Installs the generated tritonConfig.cmake and tritonConfigVersion.cmake files to the CMake package directory. ```cmake # Install tritonConfig.cmake and tritonConfigVersion.cmake install( FILES "${PROJECT_BINARY_DIR}/tritonConfig.cmake" "${PROJECT_BINARY_DIR}/tritonConfigVersion.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/triton" ) ``` -------------------------------- ### Install Triton using vcpkg Source: https://github.com/jonathansalwan/triton/blob/master/README.md Installs Triton using the vcpkg package manager. This involves cloning vcpkg, integrating it with the system, and then installing the Triton port. ```console $ git clone https://github.com/Microsoft/vcpkg.git $ cd vcpkg $ ./bootstrap-vcpkg.sh # ./bootstrap-vcpkg.bat for Windows $ ./vcpkg integrate install $ ./vcpkg install triton ``` -------------------------------- ### Install Python Autocompletion File Source: https://github.com/jonathansalwan/triton/blob/master/doc/CMakeLists.txt Installs the generated Triton Python autocompletion type stub file (`triton.pyi`) to the Python site-packages directory. ```cmake install( FILES ${CMAKE_CURRENT_BINARY_DIR}/triton_autocomplete/triton.pyi DESTINATION ${PYTHON_SITE_PACKAGES} ) ``` -------------------------------- ### Conditional Example Compilation Source: https://github.com/jonathansalwan/triton/blob/master/src/CMakeLists.txt Conditionally adds the examples subdirectory to the build. It is disabled on Windows due to linkage issues. ```cmake if(BUILD_EXAMPLES) if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Windows") # Disable examples for windows as linkage doesn't work. Exported function should # be marked as exported on windows. add_subdirectory(examples) else() enable_testing() add_test(DummyTest echo "Windows is awesome") endif() endif() ``` -------------------------------- ### Install Python Bindings Source: https://github.com/jonathansalwan/triton/blob/master/src/libtriton/CMakeLists.txt Installs the Python bindings for Triton, copying the shared library to the appropriate Python site-packages directory. ```cmake # Install Python bindings if(PYTHON_BINDINGS) add_custom_target(python-triton ALL COMMAND ${CMAKE_COMMAND} -E copy "$/${CMAKE_SHARED_MODULE_PREFIX}triton${CMAKE_SHARED_LIBRARY_SUFFIX}" "$/triton${PYTHON_SUFFIX}" DEPENDS triton ) if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) if (NOT DEFINED PYTHON_SITE_PACKAGES) execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from sysconfig import get_path; print(get_path('platlib'))" OUTPUT_VARIABLE PYTHON_SITE_PACKAGES OUTPUT_STRIP_TRAILING_WHITESPACE) endif() install(FILES $/triton${PYTHON_SUFFIX} DESTINATION ${PYTHON_SITE_PACKAGES}) else() execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from sys import version_info; print(f'lib/python{version_info[0]}.{version_info[1]}/site-packages')" OUTPUT_VARIABLE PYTHON_SITE_PACKAGES OUTPUT_STRIP_TRAILING_WHITESPACE) install(FILES $/triton${PYTHON_SUFFIX} DESTINATION ${CMAKE_INSTALL_PREFIX}/${PYTHON_SITE_PACKAGES}) endif() endif() ``` -------------------------------- ### Build Triton with LLVM and Bitwuzla Interfaces Source: https://github.com/jonathansalwan/triton/blob/master/README.md Compile Triton from source with LLVM and Bitwuzla interfaces enabled for enhanced functionality. Ensure LLVM is installed and configured. ```console $ cmake -DLLVM_INTERFACE=ON -DCMAKE_PREFIX_PATH=$(llvm-config --prefix) -DBITWUZLA_INTERFACE=ON .. ``` -------------------------------- ### Install Triton Targets Source: https://github.com/jonathansalwan/triton/blob/master/src/libtriton/CMakeLists.txt Installs the CMake targets file for Triton, making it available for other projects to find and use. ```cmake include(GNUInstallDirs) # Install tritonTargets.cmake install( EXPORT tritonTargets DESTINATION lib/cmake/triton NAMESPACE triton:: ) ``` -------------------------------- ### Python Script for Emulation and Constraint Solving Source: https://github.com/jonathansalwan/triton/blob/master/src/examples/python/ctf-writeups/NorthSec-2018-MarsAnalytica/README.md This Python script initializes Triton's symbolic engine, defines memory regions, hijacks specific PLT entries (like getchar), and starts emulation. It prints instructions containing symbolic variables and constructs a serial string. Use this script to emulate binaries and find valid inputs by solving symbolic constraints. ```python $ time python solve.py [+] Define registers [+] Define memory areas [+] Define memory 400000-e5e000 [+] Define memory e5e000-105d000 [+] Define memory 105d000-105e000 [+] Define memory 105e000-1080000 [+] Define memory 7ffff7da8000-7ffff7daa000 [+] Define memory 7ffff7daa000-7ffff7dd2000 [+] Define memory 7ffff7dd2000-7ffff7f3d000 [+] Define memory 7ffff7f3d000-7ffff7f94000 [+] Define memory 7ffff7f94000-7ffff7f95000 [+] Define memory 7ffff7f95000-7ffff7f99000 [+] Define memory 7ffff7f99000-7ffff7f9b000 [+] Define memory 7ffff7f9b000-7ffff7fa5000 [+] Define memory 7ffff7fc5000-7ffff7fc6000 [+] Define memory 7ffff7fc6000-7ffff7feb000 [+] Define memory 7ffff7feb000-7ffff7ff5000 [+] Define memory 7ffff7ff5000-7ffff7ff7000 [+] Define memory 7ffff7ff7000-7ffff7ff9000 [+] Define memory 7ffff7ff9000-7ffff7ffd000 [+] Define memory 7ffff7ffd000-7ffff7fff000 [+] Define memory 7ffffff42000-7ffffffff000 [+] Define memory ffffffffff600000-ffffffffff601000 [+] Hijack .plt @0x105E018 -> __free [+] Hijack .plt @0x105E020 -> __putchar [+] Hijack .plt @0x105E040 -> __getchar [+] Hijack .plt @0x105E058 -> __malloc [+] Hijack .plt @0x105E060 -> __fflush [+] Hijack .plt @0x105E029 -> __puts [+] Starting emulation [+] 100,000 instructions executed [+] 200,000 instructions executed [+] 300,000 instructions executed [+] 400,000 instructions executed [+] 500,000 instructions executed [+] 600,000 instructions executed [+] 700,000 instructions executed [+] Serial in construction: ",000007000$0P/c00@0" [+] Serial in construction: "0@`xP@-@``DAp'!``@3" [+] 800,000 instructions executed [+] 900,000 instructions executed [+] 1,000,000 instructions executed [+] Serial in construction: "`:@``A7P`B0``9!yE+@" [+] Serial in construction: "hP`MP(7!@c(BX3co[AM" [+] Serial in construction: "A)<;7A-`pa@0m&!zZ@`" [+] 1,100,000 instructions executed [+] 1,200,000 instructions executed [+] 1,300,000 instructions executed [+] Serial in construction: "b(S;$@yAtA00n=-`?%d" [+] Serial in construction: "e17~,C-^`C@sz1!|T:|" [+] 1,400,000 instructions executed [+] Serial in construction: "`0A{""`!W`##ppD7rqWx" [+] Serial in construction: "a=Bd"^yMg$-Y`0-kjPp" [+] Serial in construction: "o7Pz$d!>w(*oX'7]bH{" [+] Serial in construction: "o=:H5Tc_l83=zB!ylRt" [+] 1,500,000 instructions executed [+] Serial in construction: "{?@X+cc>h55MyA!s^DY" [+] Serial in construction: "q4Eo-eyMq-1dd0-leKx" [+] Serial in construction: "q4Eo-eyMq-1dd0-leKx" [+] Serial in construction: "q4Eo-eyMq-1dd0-leKx" [+] 1,600,000 instructions executed [+] Serial in construction: "q4Eo-eyMq-1dd0-leKx" [+] Serial in construction: "q4Eo-eyMq-1dd0-leKx" [+] End point reached [+] Final serial found: q4Eo-eyMq-1dd0-leKx [+] Emulation done python solve.py 136.84s user 0.56s system 99% cpu 2:17.53 total ``` -------------------------------- ### Compile Triton with CMake Source: https://github.com/jonathansalwan/triton/blob/master/src/examples/cmake/CMakeLists.txt Use this CMakeLists.txt configuration to find the Triton package, link its libraries, and add your executable. Ensure Triton is installed and discoverable by CMake. ```cmake cmake_minimum_required(VERSION 3.20) project(myproject) find_package(triton REQUIRED CONFIG) link_libraries(${TRITON_LIBRARIES}) add_executable(myproject myproject.cpp ) ``` -------------------------------- ### Build Triton Debug Version on Windows Source: https://github.com/jonathansalwan/triton/blob/master/README.md Builds the debug version of the Triton Python extension (`.pyd`) on Windows using `setup.py` and installs it. Sets environment variables for compiler and CMake paths. ```console > git clone https://github.com/JonathanSalwan/Triton.git > cd Triton > $env:COMPILER_DIR="C:/deps/llvm/llvm2116r/bin" > $env:CMAKE_PREFIX_PATH="C:/deps/llvm/llvm-project-21.1.6.src/install/lib/cmake/llvm;C:/code/cxx-common-cmake/build/install" > python_d -m build --wheel > python_d -m pip install (Get-ChildItem .\dist\triton_library*) ``` -------------------------------- ### List file details Source: https://github.com/jonathansalwan/triton/blob/master/src/examples/python/ctf-writeups/NorthSec-2018-MarsAnalytica/README.md Use ls -l to view file permissions, ownership, size, and modification date. ```bash $ ls -l 00400000-00e5e000.dump -rwxr-xr-x 1 jonathan jonathan 11M May 29 15:02 00400000-00e5e000.dump ``` -------------------------------- ### Triton CMakeLists.txt Configuration Source: https://github.com/jonathansalwan/triton/blob/master/CMakeLists.txt Sets up the minimum CMake version, project version, and includes CMake-dependent options for building Triton. ```cmake cmake_minimum_required(VERSION 3.20) set(TRITON_ROOT "${CMAKE_CURRENT_LIST_DIR}") # Read the build version file(READ ${TRITON_ROOT}/.build_number BUILD_NUMBER) set(VERSION_MAJOR 1) set(VERSION_MINOR 0) set(VERSION_BUILD ${BUILD_NUMBER}) # Used for write_basic_package_version_file() project(triton VERSION ${VERSION_MAJOR}.${VERSION_MINOR}) # Define cmake options include(CMakeDependentOption) option(ASAN "Enable the ASAN linking" OFF) option(BITWUZLA_INTERFACE "Use Bitwuzla as SMT solver" OFF) option(BUILD_SHARED_LIBS "Build a shared library" ON) option(GCOV "Enable code coverage" OFF) option(LLVM_INTERFACE "Use LLVM for lifting" OFF) option(MSVC_STATIC "Use statically-linked runtime library" OFF) option(Z3_INTERFACE "Use Z3 as SMT solver" ON) option(BOOST_INTERFACE "Use Boost as multiprecision library" OFF) option(PYTHON_BINDINGS_AUTOCOMPLETE "Generate an autocomplete stub file" OFF) ``` -------------------------------- ### Binary Execution with Found Input Source: https://github.com/jonathansalwan/triton/blob/master/src/examples/python/ctf-writeups/NorthSec-2018-MarsAnalytica/README.md This demonstrates the successful execution of the './MarsAnalytica' binary using the 'Citizen Access ID' derived from the emulation process. Upon providing the correct ID, access is granted. ```bash $ ./MarsAnalytica __ __ _ _ | \/ | /\ | | | _ | \ / | __ _ _ __ ___ / \ _ __ __ _| |_ _| |_ _ ___ __ _ | |\/| |/ _` | '__/ __| / /\ \ | '_ \ / _` | | | | | __| |/ __/ _` | | | | | (_| | | \__ \ / ____ \| | | | (_| | | |_| | |_| | (_| (_| | |_| |_|\__,_|_| |___//_/ \_\_| |_|\__,_|_|\__, |\__|_|\___\__,_| __/ | |___/ NSEC 2018 Citizen Access ID: q4Eo-eyMq-1dd0-leKx [+] ACCESS GRANTED! ** FLAG-l0rdoFb1Nq4EoeyMq1dd0leKx ** [-] Session Terminated ``` -------------------------------- ### Execute MarsAnalytica Binary Source: https://github.com/jonathansalwan/triton/blob/master/src/examples/python/ctf-writeups/NorthSec-2018-MarsAnalytica/README.md Run the MarsAnalytica binary to observe its behavior and identify the input required. This is the initial step in understanding the challenge. ```bash ./MarsAnalytica __ __ _ _ _ | \/ | /\ | | | | (_) | \ / | __ _ _ __ ___ / \ _ __ __ _| |_ _| |_ _ ___ __ _ | |\/| |/ _` | '__/ __| / /\ \ | '_ \ / _` | | | | | __| |/ __/ _` | | | | | (_| | | \__ \ / ____ \| | | | (_| | | |_| | |_| | (_| (_| | |_| |_|\__,_|_| |___//_/ \_\_| |_|\__,_|_|\__, |\__|_|\___\__,_| __/ | |___/ NSEC 2018 Citizen Access ID: test test test test [!] ACCESS DENIED - Invalid Citizen ID [-] Session Terminated ``` -------------------------------- ### Get Process ID with GDB Source: https://github.com/jonathansalwan/triton/blob/master/src/examples/python/ctf-writeups/NorthSec-2018-MarsAnalytica/README.md Retrieve the process ID (PID) of the currently running program within GDB. This PID is useful for interacting with the process via the /proc filesystem. ```bash gdb-peda$ getpid 15645 ``` -------------------------------- ### Clone Triton Repository and Build Source: https://github.com/jonathansalwan/triton/blob/master/README.md Standard procedure for cloning the Triton repository and building it from source on Linux and macOS. ```console $ git clone https://github.com/JonathanSalwan/Triton $ cd Triton $ mkdir build ; cd build $ cmake .. $ make -j3 $ sudo make install ``` -------------------------------- ### Find Boost Dependency Source: https://github.com/jonathansalwan/triton/blob/master/CMakeLists.txt Finds the Boost library starting from version 1.55.0. If Boost is found, it includes the Boost include directories and sets TRITON_BOOST_INTERFACE to ON. Otherwise, it sets it to OFF and prints a status message. ```cmake if(BOOST_INTERFACE) message(STATUS "Compiling with Boost headers") find_package(Boost 1.55.0) if (Boost_FOUND) include_directories("${Boost_INCLUDE_DIRS}") set(TRITON_BOOST_INTERFACE ON) else() message(STATUS "Boost headers not found, compiling with wide-integer headers") set(TRITON_BOOST_INTERFACE OFF) endif() else() message(STATUS "Compiling with wide-integer headers") set(TRITON_BOOST_INTERFACE OFF) endif() ``` -------------------------------- ### Info Register Executable and Test Source: https://github.com/jonathansalwan/triton/blob/master/src/examples/cpp/CMakeLists.txt Defines the 'info_reg' executable from 'info_reg.cpp', sets the C++ standard to 17, links against the Triton library, and adds it as a test case. ```cmake add_executable(info_reg info_reg.cpp) set_property(TARGET info_reg PROPERTY CXX_STANDARD 17) target_link_libraries(info_reg triton) add_test(InfoRegister info_reg) add_dependencies(check info_reg) ``` -------------------------------- ### Find and Configure LLVM Source: https://github.com/jonathansalwan/triton/blob/master/CMakeLists.txt Finds and configures the LLVM build environment. It handles different LLVM versions and configurations, including dynamic/static linking and specific compiler flags for newer versions. ```cmake if(LLVM_INTERFACE) message(STATUS "Compiling with LLVM") if(NOT DEFINED LLVM_INCLUDE_DIRS) find_package(LLVM REQUIRED CONFIG) message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") if(LLVM_LINK_LLVM_DYLIB) set(LLVM_LIBRARIES LLVM) else() set(LLVM_LIBRARIES ${LLVM_AVAILABLE_LIBS}) endif() # LLVM 18+ Linux prebuilts are compiled without exception handling and # propagate -fno-exceptions via INTERFACE_COMPILE_OPTIONS. Since those # options are applied after Triton's own compile flags in the compiler # command (last-flag-wins), they would override Triton's exceptions. Strip them. if(NOT MSVC AND LLVM_VERSION_MAJOR GREATER_EQUAL 18) foreach(_lib IN ITEMS LLVM ${LLVM_AVAILABLE_LIBS}) if(TARGET ${_lib}) get_target_property(_opts ${_lib} INTERFACE_COMPILE_OPTIONS) if(_opts) list(REMOVE_ITEM _opts -fno-exceptions -fno-rtti) set_target_properties(${_lib} PROPERTIES INTERFACE_COMPILE_OPTIONS "${_opts}") endif() endif() endforeach() endif() # LLVM 19+ Linux prebuilts ship static archives compiled with thin LTO. # They contain LLVM bitcode rather than ELF objects, which GNU ld cannot # link. lld handles thin-LTO archives transparently; force it here. # Note: only static-archive builds (not dylib) on LLVM 19+; older # distro-packaged LLVMs use plain ELF archives and may not have lld. if(CMAKE_SYSTEM_NAME MATCHES "Linux" AND NOT "$CACHE{LLVM_LINK_LLVM_DYLIB}" AND LLVM_VERSION_MAJOR GREATER_EQUAL 19) add_link_options(-B${LLVM_TOOLS_BINARY_DIR} -fuse-ld=lld) message(STATUS "Using lld for thin-LTO static archives") endif() endif() include_directories(${LLVM_INCLUDE_DIRS}) set(TRITON_LLVM_INTERFACE ON) endif() ``` -------------------------------- ### Configure Doxyfile Source: https://github.com/jonathansalwan/triton/blob/master/doc/CMakeLists.txt Configures the Doxyfile template, substituting variables, and writes the output to the build directory. ```cmake configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile IMMEDIATE @ONLY ) ``` -------------------------------- ### IR Executable and Test Source: https://github.com/jonathansalwan/triton/blob/master/src/examples/cpp/CMakeLists.txt Defines the 'ir' executable from 'ir.cpp', sets the C++ standard to 17, links against the Triton library, and adds it as a test case. ```cmake add_executable(ir ir.cpp) set_property(TARGET ir PROPERTY CXX_STANDARD 17) target_link_libraries(ir triton) add_test(IR ir) add_dependencies(check ir) ``` -------------------------------- ### Generate Project Documentation Source: https://github.com/jonathansalwan/triton/blob/master/doc/CMakeLists.txt Defines a custom CMake target 'doc' that depends on the documentation generation targets for various architectures and runs Doxygen to create the final documentation. ```cmake add_custom_target(doc COMMAND doxygen ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile DEPENDS gen_x86_doc_from_spec DEPENDS gen_aarch64_doc_from_spec DEPENDS gen_arm32_doc_from_spec DEPENDS gen_rv64_doc_from_spec DEPENDS gen_rv32_doc_from_spec ) ``` -------------------------------- ### Generate Visual Studio Solution for Triton on Windows Source: https://github.com/jonathansalwan/triton/blob/master/README.md Uses CMake to generate a Visual Studio solution file for building Triton on Windows. Specifies paths for Boost, Python, Z3, and Capstone libraries. ```console > git clone https://github.com/JonathanSalwan/Triton.git > cd Triton > mkdir build > cd build > cmake -G "Visual Studio 14 2015 Win64" \ -DBOOST_ROOT="C:/Users/jonathan/Works/Tools/boost_1_61_0" \ -DPYTHON_INCLUDE_DIRS="C:/Python36/include" \ -DPYTHON_LIBRARIES="C:/Python36/libs/python36.lib" \ -DZ3_INCLUDE_DIRS="C:/Users/jonathan/Works/Tools/z3-4.6.0-x64-win/include" \ -DZ3_LIBRARIES="C:/Users/jonathan/Works/Tools/z3-4.6.0-x64-win/bin/libz3.lib" \ -DCAPSTONE_INCLUDE_DIRS="C:/Users/jonathan/Works/Tools/capstone-5.0.1-win64/include" \ -DCAPSTONE_LIBRARIES="C:/Users/jonathan/Works/Tools/capstone-5.0.1-win64/capstone.lib" .. ``` -------------------------------- ### Simplification Executable and Test Source: https://github.com/jonathansalwan/triton/blob/master/src/examples/cpp/CMakeLists.txt Defines the 'simplification' executable from 'simplification.cpp', sets the C++ standard to 17, links against the Triton library, and adds it as a test case. ```cmake add_executable(simplification simplification.cpp) set_property(TARGET simplification PROPERTY CXX_STANDARD 17) target_link_libraries(simplification triton) add_test(Simplification simplification) add_dependencies(check simplification) ``` -------------------------------- ### Configure Package Configuration Files Source: https://github.com/jonathansalwan/triton/blob/master/src/libtriton/CMakeLists.txt Configures and generates the tritonConfig.cmake and tritonConfigVersion.cmake files for package management. ```cmake include(CMakePackageConfigHelpers) configure_package_config_file( Config.cmake.in "${PROJECT_BINARY_DIR}/tritonConfig.cmake" INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/triton" NO_SET_AND_CHECK_MACRO NO_CHECK_REQUIRED_COMPONENTS_MACRO ) write_basic_package_version_file("${PROJECT_BINARY_DIR}/tritonConfigVersion.cmake" COMPATIBILITY SameMajorVersion ) ``` -------------------------------- ### Block Executable and Test Source: https://github.com/jonathansalwan/triton/blob/master/src/examples/cpp/CMakeLists.txt Defines the 'block' executable from 'block.cpp', sets the C++ standard to 17, links against the Triton library, and adds it as a test case. ```cmake add_executable(block block.cpp) set_property(TARGET block PROPERTY CXX_STANDARD 17) target_link_libraries(block triton) add_test(TestBlock block) add_dependencies(check block) ``` -------------------------------- ### Continue execution in GDB Source: https://github.com/jonathansalwan/triton/blob/master/src/examples/python/ctf-writeups/NorthSec-2018-MarsAnalytica/README.md Use the 'c' command in GDB to continue program execution until the next breakpoint or watchpoint is hit. ```gdb gdb-peda$ c Continuing. ``` -------------------------------- ### Add Triton Library Subdirectory Source: https://github.com/jonathansalwan/triton/blob/master/src/CMakeLists.txt Includes the libtriton library into the build. This is a fundamental step for integrating Triton's core functionality. ```cmake add_subdirectory(libtriton) ``` -------------------------------- ### Find and Link Bitwuzla SMT Solver Source: https://github.com/jonathansalwan/triton/blob/master/CMakeLists.txt Configures the build to find and link against the Bitwuzla SMT solver if BITWUZLA_INTERFACE is enabled. It supports linking via targets or include directories. ```cmake if(BITWUZLA_INTERFACE) message(STATUS "Compiling with Bitwuzla SMT solver") find_package(BITWUZLA REQUIRED) if(TARGET Bitwuzla::bitwuzla) link_libraries(Bitwuzla::bitwuzla) elseif(DEFINED BITWUZLA_INCLUDE_DIRS) include_directories(${BITWUZLA_INCLUDE_DIRS}) else() message(FATAL_ERROR "Unexpected Bitwuzla package search outcome: neither target Bitwuzla::bitwuzla not variable BITWUZLA_INCLUDE_DIRS exists.") endif() set(TRITON_BITWUZLA_INTERFACE ON) endif() ``` -------------------------------- ### Constraint Executable and Test Source: https://github.com/jonathansalwan/triton/blob/master/src/examples/cpp/CMakeLists.txt Defines the 'constraint' executable from 'constraint.cpp', sets the C++ standard to 17, links against the Triton library, and adds it as a test case. ```cmake add_executable(constraint constraint.cpp) set_property(TARGET constraint PROPERTY CXX_STANDARD 17) target_link_libraries(constraint triton) add_test(Constraint constraint) add_dependencies(check constraint) ``` -------------------------------- ### Configure Python Site Packages Path Source: https://github.com/jonathansalwan/triton/blob/master/doc/CMakeLists.txt Determines the Python site-packages directory if it's not already defined, using `sysconfig.get_path('platlib')`. ```cmake if (NOT DEFINED PYTHON_SITE_PACKAGES) execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from sysconfig import get_path; print(get_path('platlib'))" OUTPUT_VARIABLE PYTHON_SITE_PACKAGES OUTPUT_STRIP_TRAILING_WHITESPACE) endif() ``` -------------------------------- ### Lift Triton Path Predicate to LLVM Source: https://github.com/jonathansalwan/triton/blob/master/src/examples/python/ctf-writeups/NorthSec-2018-MarsAnalytica/README.md Use this function to obtain the path predicate from the Triton context and lift it to LLVM IR. The `optimize` flag can be set to `True` for optimization. ```python def lifting2llvm(ctx): predicate = ctx.getPathPredicate() M = ctx.liftToLLVM(predicate, fname="mars_analytica", optimize=True) print(M) return ``` -------------------------------- ### Generate arm32 Documentation from Spec Source: https://github.com/jonathansalwan/triton/blob/master/doc/CMakeLists.txt Defines a custom CMake target to generate documentation for the arm32 architecture from its specification file using a Python script. ```cmake add_custom_target(gen_arm32_doc_from_spec COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/extract_doc.py ${TRITON_ROOT}/src/libtriton/includes/triton/arm32.spec "${CMAKE_CURRENT_BINARY_DIR}" DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/extract_doc.py DEPENDS ${TRITON_ROOT}/src/libtriton/includes/triton/arm32.spec ) ``` -------------------------------- ### Run Binary in GDB and Interrupt Source: https://github.com/jonathansalwan/triton/blob/master/src/examples/python/ctf-writeups/NorthSec-2018-MarsAnalytica/README.md Execute a binary within GDB and interrupt it using Ctrl+C when it waits for input. This allows you to access GDB's prompt and inspect the process state. ```bash $ gdb ./MarsAnalytica GNU gdb (Gentoo 11.2 vanilla) 11.2 [...] gdb-peda$ r Starting program: /home/jonathan/Works/Tools/Triton/src/examples/python/ctf-writeups/NorthSec-2018-MarsAnalytica/MarsAnalytica __ __ _ _ _ | \/ | /\ | | | | (_) | \ / | __ _ _ __ ___ / \ _ __ __ _| |_ _| |_ _ ___ __ _ | |\/| |/ _` | '__/ __| / /\ \ | '_ \ / _` | | | | | __| |/ __/ _` | | | | | (_| | | \__ \ / ____ \| | | | (_| | | |_| | |_| | (_| (_| | |_| |_|\__,_|_| |___//_/ \_\_| |_|\__,_|_|\__, |\__|_|\___\__,_| __/ | |___/ NSEC 2018 Citizen Access ID: ^C Program received signal SIGINT, Interrupt. [----------------------------------registers----------------------------------] RAX: 0xfffffffffffffe00 RBX: 0x7ffff7f99aa0 --> 0xfbad2288 RCX: 0x7ffff7ea369e (cmp rax,0xfffffffffffff000) RDX: 0x400 RSI: 0x105fad0 --> 0x0 RDI: 0x0 RBP: 0x7ffff7f965e0 --> 0x0 RSP: 0x7ffffff42cb8 --> 0x7ffff7e2a95c (test rax,rax) RIP: 0x7ffff7ea369e (cmp rax,0xfffffffffffff000) R8 : 0x0 R9 : 0x77 ('w') R10: 0x5d (']') R11: 0x246 R12: 0x7ffff7f9a780 --> 0xfbad2a84 R13: 0xd68 ('h\r') R14: 0x7ffff7f959e0 --> 0x0 R15: 0xd68 ('h\r') EFLAGS: 0x246 (carry PARITY adjust ZERO sign trap INTERRUPT direction overflow) [-------------------------------------code------------------------------------- ] 0x7ffff7ea3698:\ttest eax,eax 0x7ffff7ea369a:\tjne 0x7ffff7ea36b0 0x7ffff7ea369c:\tsyscall => 0x7ffff7ea369e:\tcmp rax,0xfffffffffffff000 0x7ffff7ea36a4:\tja 0x7ffff7ea3700 0x7ffff7ea36a6:\tret 0x7ffff7ea36a7:\tnop WORD PTR [rax+rax*1+0x0] 0x7ffff7ea36b0:\tsub rsp,0x28 [------------------------------------stack------------------------------------- ]0000| 0x7ffffff42cb8 --> 0x7ffff7e2a95c (test rax,rax) 0008| 0x7ffffff42cc0 --> 0x7ffff7ff7000 --> 0x7ffff7ff8220 --> 0x0 0016| 0x7ffffff42cc8 --> 0x7ffff7f965e0 --> 0x0 0024| 0x7ffffff42cd0 --> 0x7ffffff42cf0 --> 0x400da9 (push rbp) 0032| 0x7ffffff42cd8 --> 0x7ffff7f99aa0 --> 0xfbad2288 0040| 0x7ffffff42ce0 --> 0x7ffff7f965e0 --> 0x0 0048| 0x7ffffff42ce8 --> 0x7fffffffd2f8 --> 0x7fffffffe64f ("/home/jonathan/Works/Tools/Triton/src/examples/python/ctf-writeups/NorthSec-2018-MarsAnalytica/MarsAnalytica") 0056| 0x7ffffff42cf0 --> 0x400da9 (push rbp) [------------------------------------------------------------------------------] Legend: code, data, rodata, value Stopped reason: SIGINT 0x00007ffff7ea369e in ?? () ``` -------------------------------- ### Configure Triton Build with Python on macOS M1 Source: https://github.com/jonathansalwan/triton/blob/master/README.md Troubleshoots Python library detection issues on macOS M1 by explicitly setting Python executable, libraries, and include directories during the CMake configuration. ```console cmake -DCMAKE_INSTALL_PREFIX=/opt/homebrew/ \ -DPYTHON_EXECUTABLE=/opt/homebrew/bin/python3 \ -DPYTHON_LIBRARIES=/opt/homebrew/Cellar/python@3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/libpython3.10.dylib \ -DPYTHON_INCLUDE_DIRS=/opt/homebrew/opt/python@3.10/Frameworks/Python.framework/Versions/3.10/include/python3.10/ \ .. ``` -------------------------------- ### Generate x86 Documentation from Spec Source: https://github.com/jonathansalwan/triton/blob/master/doc/CMakeLists.txt Defines a custom CMake target to generate documentation for the x86 architecture from its specification file using a Python script. ```cmake add_custom_target(gen_x86_doc_from_spec COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/extract_doc.py ${TRITON_ROOT}/src/libtriton/includes/triton/x86.spec "${CMAKE_CURRENT_BINARY_DIR}" DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/extract_doc.py DEPENDS ${TRITON_ROOT}/src/libtriton/includes/triton/x86.spec ) ``` -------------------------------- ### Enable Static MSVC Runtime Source: https://github.com/jonathansalwan/triton/blob/master/src/libtriton/CMakeLists.txt Configures the MSVC runtime library to be statically linked for the 'triton' target when MSVC and MSVC_STATIC are enabled. ```cmake if(MSVC AND MSVC_STATIC) set_property(TARGET triton PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") endif() ``` -------------------------------- ### Find and Link Z3 SMT Solver Source: https://github.com/jonathansalwan/triton/blob/master/CMakeLists.txt Configures the build to find and link against the Z3 SMT solver if Z3_INTERFACE is enabled. It handles different linking methods based on Z3's availability. ```cmake if(Z3_INTERFACE) message(STATUS "Compiling with Z3 SMT solver") find_package(Z3 REQUIRED) message(STATUS "Z3 version: ${Z3_VERSION}") if(TARGET z3::libz3) link_libraries(z3::libz3) elseif(DEFINED Z3_INCLUDE_DIRS) include_directories(${Z3_INCLUDE_DIRS}) else() message(FATAL_ERROR "Unexpected Z3 package search outcome: neither target z3::libz3 not variable Z3_INCLUDE_DIRS exists.") endif() set(TRITON_Z3_INTERFACE ON) endif() ``` -------------------------------- ### Add ARM32 Crypto Tests Source: https://github.com/jonathansalwan/triton/blob/master/src/testers/CMakeLists.txt Adds multiple test cases for ARM32 crypto semantics with varying optimization levels. These tests are executed using a Python script. ```cmake add_test(NAME UnicornARM32Semantics30 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/arm32/crypto_test/crypto_test-thumb-O0-run.py) ``` ```cmake add_test(NAME UnicornARM32Semantics31 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/arm32/crypto_test/crypto_test-thumb-O1-run.py) ``` ```cmake add_test(NAME UnicornARM32Semantics32 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/arm32/crypto_test/crypto_test-thumb-O2-run.py) ``` ```cmake add_test(NAME UnicornARM32Semantics33 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/arm32/crypto_test/crypto_test-thumb-O3-run.py) ``` ```cmake add_test(NAME UnicornARM32Semantics34 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/arm32/crypto_test/crypto_test-thumb-Os-run.py) ``` ```cmake add_test(NAME UnicornARM32Semantics35 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/arm32/crypto_test/crypto_test-thumb-Oz-run.py) ``` -------------------------------- ### Define BUILDING_DLL for Shared Libraries on Windows Source: https://github.com/jonathansalwan/triton/blob/master/src/libtriton/CMakeLists.txt Defines the BUILDING_DLL preprocessor macro for the 'triton' target when building on Windows with shared libraries enabled. ```cmake if(WIN32 AND BUILD_SHARED_LIBS) target_compile_definitions(triton PRIVATE BUILDING_DLL) endif() ``` -------------------------------- ### Perform full memory and register dump in GDB Source: https://github.com/jonathansalwan/triton/blob/master/src/examples/python/ctf-writeups/NorthSec-2018-MarsAnalytica/README.md Use the 'fulldump' command (often provided by extensions like PEDA) in GDB to save the entire memory state and registers to a file. This is useful for later analysis or initializing an emulator. ```gdb gdb-peda$ fulldump Full dump saved into fulldump.dump ``` -------------------------------- ### Generate rv32 Documentation from Spec Source: https://github.com/jonathansalwan/triton/blob/master/doc/CMakeLists.txt Defines a custom CMake target to generate documentation for the rv32 architecture from its specification file using a Python script. ```cmake add_custom_target(gen_rv32_doc_from_spec COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/extract_doc.py ${TRITON_ROOT}/src/libtriton/includes/triton/riscv32.spec "${CMAKE_CURRENT_BINARY_DIR}" DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/extract_doc.py DEPENDS ${TRITON_ROOT}/src/libtriton/includes/triton/riscv32.spec ) ``` -------------------------------- ### Hooking Libc Executable and Test with LIEF and fmt Source: https://github.com/jonathansalwan/triton/blob/master/src/examples/cpp/CMakeLists.txt Conditionally defines the 'hooking_libc' executable if LIEF and fmt are found. It sets the C++ standard to 17 and links against Triton, LIEF, and fmt libraries, then adds it as a test case. ```cmake find_package(LIEF QUIET) find_package(fmt QUIET) if(LIEF_FOUND AND fmt_FOUND) message(STATUS "LIEF found") message(STATUS "FMT found") add_executable(hooking_libc hooking_libc.cpp) set_property(TARGET hooking_libc PROPERTY CXX_STANDARD 17) target_link_libraries(hooking_libc triton LIEF::LIEF fmt::fmt) add_test(TestHookingLibc hooking_libc) add_dependencies(check hooking_libc) endif() ``` -------------------------------- ### Inspect Process Memory Maps with GDB Source: https://github.com/jonathansalwan/triton/blob/master/src/examples/python/ctf-writeups/NorthSec-2018-MarsAnalytica/README.md View the memory map of a process using GDB and the /proc filesystem. This output details the memory regions, their permissions, and associated files. ```bash gdb-peda$ cat /proc/15645/maps 00400000-00e5e000 r-xp 00000000 00:00 0 00e5e000-0105d000 ---p 00000000 00:00 0 0105d000-0105e000 r--p 00000000 00:00 0 0105e000-01080000 rw-p 00000000 00:00 0 [heap] 7ffff7da8000-7ffff7daa000 rw-p 00000000 00:00 0 7ffff7daa000-7ffff7dd2000 r--p 00000000 103:04 9059496 /lib64/libc.so.6 7ffff7dd2000-7ffff7f3d000 r-xp 00028000 103:04 9059496 /lib64/libc.so.6 7ffff7f3d000-7ffff7f94000 r--p 00193000 103:04 9059496 /lib64/libc.so.6 7ffff7f94000-7ffff7f95000 ---p 001ea000 103:04 9059496 /lib64/libc.so.6 7ffff7f95000-7ffff7f99000 r--p 001ea000 103:04 9059496 /lib64/libc.so.6 7ffff7f99000-7ffff7f9b000 rw-p 001ee000 103:04 9059496 /lib64/libc.so.6 7ffff7f9b000-7ffff7fa5000 rw-p 00000000 00:00 0 7ffff7fc5000-7ffff7fc6000 r--p 00000000 103:04 9059538 /lib64/ld-linux-x86-64.so.2 7ffff7fc6000-7ffff7feb000 r-xp 00001000 103:04 9059538 /lib64/ld-linux-x86-64.so.2 7ffff7feb000-7ffff7ff5000 r--p 00026000 103:04 9059538 /lib64/ld-linux-x86-64.so.2 7ffff7ff5000-7ffff7ff7000 r--p 0002f000 103:04 9059538 /lib64/ld-linux-x86-64.so.2 7ffff7ff7000-7ffff7ff9000 rw-p 00031000 103:04 9059538 /lib64/ld-linux-x86-64.so.2 7ffff7ff9000-7ffff7ffd000 r--p 00000000 00:00 0 [vvar] 7ffff7ffd000-7ffff7fff000 r-xp 00000000 00:00 0 [vdso] 7ffffff42000-7ffffffff000 rw-p 00000000 00:00 0 [stack] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall] ``` -------------------------------- ### Generate rv64 Documentation from Spec Source: https://github.com/jonathansalwan/triton/blob/master/doc/CMakeLists.txt Defines a custom CMake target to generate documentation for the rv64 architecture from its specification file using a Python script. ```cmake add_custom_target(gen_rv64_doc_from_spec COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/extract_doc.py ${TRITON_ROOT}/src/libtriton/includes/triton/riscv64.spec "${CMAKE_CURRENT_BINARY_DIR}" DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/extract_doc.py DEPENDS ${TRITON_ROOT}/src/libtriton/includes/triton/riscv64.spec ) ```