bap

BinaryAnalysisPlatformMIT更新放缓

把 ARM、MIPS、PowerPC 等架构的机器码提升为统一中间表示,方便做逆向分析、指令语义研究和动态模拟。

Binary Analysis Platform

v2.5.0发布于 2022-07-11 20:58 UTC2,263star286fork同步于 2026-11-07 07:34 UTC

怎么上手

从 README 代码块原样提取,可能是其中一种安装方式
wget https://github.com/BinaryAnalysisPlatform/bap/releases/download/v2.5.0/{bap,libbap,libbap-dev}_2.5.0.deb
git clone [email protected]:BinaryAnalysisPlatform/bap.git && cd bap
硬件要求

README 没有说明硬件要求。

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

这个项目的说明还在生成中,先参考下面「最近发版」和 README。

最近发版

5 个版本v2.5.0
  1. v2.6.0-alphaDevelopment Buildpre-release2023-01-07 · 2023-01-07 01:19 UTC
    查看更新说明

    Commits

    • f995d28: allows bypassing checks when storing memory, uses it for relocations (#1567) (Ivan Gotovchits) #1567
  2. v2.5.0最新正式版2022-07-11 · 2022-07-11 20:58 UTC

    Features

    • #1390 adds the flattening pass to the library interface
    • #1389 adds insn-code to the Theory.Semantics clas
    • #1394 adds the Bitvec.modular function
    • #1395 adds LLVM 13/14 compatibility
    • #1408 adds support for mips64el target
    • #1409 adds the --print-missing option to print unlifed instructions
    • #1410 adds several new Primus Lisp primitives and new instructions
    • #1428 adds the monad choice interface to the knowledge base
    • #1429 refines the Theory.Target.matches and adds the matching function
    • #1434 adds arm unpredicated BL instruction
    • #1444 adds the x86/amd64 plt corrector component to the Primus base system
    • #1445 updates the Sub.compute_liveness function to handle SSA form
    • #1446 provides the new liveness analysis
    • #1452 implements pcode floating-point and special operators
    • #1457 adds optional join for Knowledge.Domain.mapping
    • #1461 enables v8.{1,2,3,4,5,6}a revisions for the aarch64 target
    • #1464 adds arbitrary-precision loopless clz and popcount to Primus Lisp
    • #1460 adds compatibility with Core_kernel >= 0.15
    • #1466 adds semantics for the x86 SSE floating-point instructions
    • #1469 adds the jump destination addresses/names to the assembly output
    • #1458 adds more aarch64 instructions
    • #1473 adds an --arm-features command-line option
    • #1476 implements the naming scheme for interrupts
    • #1479 reifies external subroutines and intrinsics into I
    • #1482 enables BIR subroutines without an explicit return
    • #1481 enables disabling the patterns plugin
    • #1483 implements floating-point intrinsic subroutines
    • #1488 adds compatibility with OCaml 4.14 and Core v0.15
    • #1489 adds some missing functionality to Primus Lisp POSIX
    • #1490 adds some missing C POSIX APIs
    • #1492 makes bit-twiddling operations easier to read and analyze
    • #1493 adds smart constructors and destructors to the C types library
    • #1491 adds semantics for the x86-64 popq instruction
    • #1497 extends the C.Abi library
    • #1498 adds the extended lvalue assignment to Primus Interpreter
    • #1499 makes BIL smart constructors smart
    • #1500 makes argument passing well-typed
    • #1503 reimplements C types printing functions
    • #1504 extends the demanglers library to the new targets infrastructure
    • #1505 rewrites x86 abi using the new infrastructure
    • #1511 implements some missing Thumb instructions
    • #1513 implements the x86_64 padd instructions
    • #1515 allows target overriding
    • #1516 adds armv8 BFM instructions
    • #1517 publishes Theory.Target.nicknames and extends Primus Contexts
    • #1519 extends Core Theory with target registration and lookup
    • #1520 adds the high-level calling convention specification language
    • #1521 reimplements x86 targets using the new infrastructure
    • #1522 reimplements ARM ABI and target specification
    • #1523 rewrites mips targets and abi
    • #1524 adds C data type layout
    • #1525 adds the pass by reference argument passing method
    • #1526 restructures powerpc targets and reimplements ppc32 eabi
    • #1529 makes the ABI processors usable programmatically

    Bug Fixes

    • #1391 fixes ARM/Thumb movt semantics
    • #1396 fixes the path plugin loader path handling
    • #1414 fixes the pc value in pc-relative thumb ldr
    • #1420 fixes the low-level Disasm_expert.Basic.create function
    • #1421 fixes the core-theory plugin semantics tags
    • #1426 fixes arm predication
    • #1438 reads correctly unqualified system names
    • #1439 fixes a bug in the KB update function, adds new functions
    • #1448 fixes an accidental dependency on the bap-traces internal module
    • #1449 fixes unconditional pop with return in thumb
    • #1455 fixes register assignments in p-code semantics
    • #1462 fixes the cast-signed Primus Lisp primitive
    • #1463 fixes the arithmetic modulus in Primus Lisp primitives
    • #1465 fixes handling of jmp terms in the flatten pass
    • #1467 fixes a sporadic internal error in the cache garbage collector
    • #1468 fixes the relocation symbolizer incorr
  3. v2.5.0-alphaDevelopment Buildpre-release2022-07-09 · 2022-07-09 01:01 UTC
    查看更新说明

    Commits

    • 5a71fb1: adds c layouts (#1524) (Ivan Gotovchits) #1524
    • 4b9932b: enables passing arguments by reference (#1525) (Ivan Gotovchits) #1525
    • 2af9d91: restructures powerpc targets and reimplements ppc32 eabi (#1526) (Ivan Gotovchits) #1526
    • 02c953b: fixes armv4t name that was missing the arm prefix (#1528) (Ivan Gotovchits) #1528
    • 411c733: turns x86 endbr instructions into nops by default (#1530) (Ivan Gotovchits) #1530
    • 7e3d406: adds patterns to recognize certain x86 endbr as function starts (#1531) (Ivan Gotovchits) #1531
    • c100c28: makes the ABI processors usable programmatically (#1529) (Ivan Gotovchits) #1529
    • 2b789c4: improves the main subroutine discovery within glibc runtime (#1532) (Ivan Gotovchits) #1532
    • 46c93ca: fixes calling convention for ms64 (#1533) (Ivan Gotovchits) #1533
    • baa9022: prevents knowledge conflicts on mangled names (#1535) (Ivan Gotovchits) #1535
  4. v2.4.02021-12-08 · 2021-12-08 16:06 UTC
    查看更新说明

    Features

    • #1325 adds armeb abi
    • #1326 adds experimental Ghidra disassembler and lifting backend
    • #1332 adds the flatten pass
    • #1341 adds context variables to the knowledge base
    • #1343 adds register aliases to the Core Theory
    • #1358 adds LLVM 12 support
    • #1360 extends the knowledge monad interface
    • #1363 adds forward-chaining rules and Primus Lisp methods
    • #1364 adds a generic byte pattern matcher based on Ghidra
    • #1365 adds support for the Thumb IT blocks
    • #1369 adds some missing t2LDR.-i12 instructions to the Thumb lifter

    Improvements

    • #1336 improves the main function discovery heuristics
    • #1337 adds more Primus Lisp stubs and fixes some existing
    • #1342 uses context variables to store the current theory
    • #1344 uses the context variables to store the Primus Lisp state
    • #1355 tweaks symbolization and function start identification facilities
    • #1353 improves arm-family support
    • #1356 stops proposing aliases as potential subroutine names
    • #1361 rewrites knowledge and primus monads
    • #1370 tweaks Primus Lisp' method resolution to keep super methods
    • #1375 error handling and performance tweaks
    • #1378 improves reification of calls in the IR theory (part I)
    • #1379 improves semantics of some ITT instructions
    • #1380 improves reification of calls in the IR theory (part II)

    Bug Fixes

    • #1328 fixes C.ABI.Args popn and align_even operators
    • #1329 fixes frame layout calculation in the Primus loader
    • #1330 fixes the address size computation in the llvm backend
    • #1333 fixes and improves label handling in the IR theor
    • #1338 fixes core:eff theory
    • #1340 fixes the Node.update for graphs with unlabeled nodes
    • #1347 fixes a knowledge base race condition in the run plugin
    • #1348 fixes endianness in the raw loader
    • #1349 short-circuits evaluation of terms in Bap_main.init
    • #1350 fixes variable rewriter and some Primus Lisp symbolic functions
    • #1351 fixes and improves aarch64 lifter
    • #1352 fixes several Primus Lisp stubs
    • #1357 fixes some T32 instructions that are accessing to PC
    • #1359 fixes handling of let-bound variables in flatten pass
    • #1366 fixes a bug in the cmp semantics
    • #1374 fixes handling modified immediate constants in ARM T32 encoding
    • #1376 fixes fresh variable generation
    • #1377 fixes the IR theory implementation

    Tooling

    • #1319 fixes the shared folder in deb packages
    • #1320 removes sudo from postinst and postrm actions in the deb packages
    • #1321 enables push flag in the publish-docker-image action
    • #1323 fixes the ppx_bap version in the dev-repo opam file
    • #1331 fixes the docker publisher, also enables manual triggering
    • #1327 fixes a typo in the ubuntu dockerfiles
    • #1345 fixes bapdoc
    • #1346 nightly tests are failing due to a bug upstream
  5. v2.3.02021-05-26 · 2021-05-26 23:59 UTC
    查看更新说明

    2.3.0

    Features

    • #1265 introduces BIL special encodings and publishes BIL CT parser
    • #1266 introduces the BIL code slot and a few convenience functions
    • #1268 adds a Primus Lisp fronted
    • #1273 rewrites the read-symbols plugin to enable multiple files support
    • #1274 updates the raw loader to enable better support for targets
    • #1275 overhauls target/value abstraction and introduces roles (4/n)
    • #1276 adds the --target parameter to disassemble and compare commands
    • #1277 enables writing program semantics (lifting) in Primus Lisp
    • #1278 moves to centralized and uniform path handling
    • #1286 adds namepspaces (packages) to Primus Lisp
    • #1287 adds riscv32 and riscv64 support
    • #1288 restores output for non-empty instructions
    • #1289 fixes the unit tracking mechanism
    • #1290 implements ABI description eDSL
    • #1291 adds armv8 (aarch64) lifter
    • #1293 implements the array to pointer conversion in C ABI processor
    • #1294 introduces bap dependencies command
    • #1296 uses ARM EABI for Thumb binaries
    • #1295 adds YAML as an alternative output of the specification command
    • #1297 adds the missing ux*, sx*, s?bz instructions
    • #1298 adds more thumb-specific instructions
    • #1304 adds the --show-knowledge option to bap mc
    • #1303 preserves the encoding name of the instruction
    • #1307 renames the core-theory package to just core
    • #1306 adds bap primus-lisp-documentation command
    • #1309 adds support for big-endian arm and thumb architectures

    Bug Fixes

    • #1263 fixes PE/COFF sections decoding
    • #1272 fixes two bugs in the Memory module
    • #1280 fixes, cleans, and optimizes KB AVL tree implementation
    • #1281 prunes empty segments
    • #1282 fixes recipes search paths
    • #1285 fixes a missing str dependency in the x86 plugin
    • #1308 fixes a bug in the method that computes C padding.
    • #1310 removes the old unit tracking module, fixes the modern one

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    Binary Analysis Platform

    License Join the chat at https://gitter.im/BinaryAnalysisPlatform/bap docs docs

    Table of contents

    Overview

    The Carnegie Mellon University Binary Analysis Platform (CMU BAP) is a suite of utilities and libraries that enables analysis of binary programs. BAP supports x86, x86-64, ARM, MIPS, PowerPC and new architectures can be added using plugins. BAP includes various analyses, standard interpreter, microexecution interpreter, and a symbolic executor. BAP features its own domain-specific language, Primus Lisp, that is used for implementing analyses, specifying verification conditions, modeling functions (writing stubs), and even interfacing with the SMT solver. The toolkit repository includes various examples of program analysis tools that could be implemented with BAP and can be used as the starting point (in addition to the tutorial) for implementing custom analyses. BAP can be used as a framework with a single bap utility that is extended with plugins or it can be used as a library embedded in a user application, which could be written in OCaml or, in any other language, using C bindings. We also provide some minimal support for Python to make it easier to start learning BAP.

    BAP was developed in CMU, Cylab and is sponsored by grants from the United States Department of Defense, Siemens, Boeing, ForAllSecure, and the Korea government, see sponsors for more information. BAP is used in various institutions and serves as a backbone for many interesting projects, some are highlighted below:

    Installation

    Using pre-build packages

    We provide binary packages packed for Debian and Red Hat derivatives. For other distributions we provide tgz archives. To install bap on a Debian derivative:

    wget https://github.com/BinaryAnalysisPlatform/bap/releases/download/v2.5.0/{bap,libbap,libbap-dev}_2.5.0.deb
    sudo dpkg -i {bap,libbap,libbap-dev}_2.5.0.deb
    

    From sources

    Our binary packages do not include the OCaml development environment. If you are going to write an analysis in OCaml you need to install BAP from the source code using either opam or by cloning and building this repository directly. The opam method is the recommended one. Once it is installed the following three commands should install the platform in a newly created switch.

    opam init --comp=4.14.1   # inits opam and install the OCaml compiler
    opam install bap          # installs bap and its dependencies
    eval $(opam env)`         # activates opam environment
    

    Or, if you already have a switch where you would like install bap, then just do

    opam install bap
    

    The opam install bap command will also try to install the system dependencies of BAP using your operating system package manager. If it fails due to a missing system dependency, try installing it manually and then repeat the opam install bap command. If it still doesn't work, do not hesitate to drop by our chat and seek help there. It is manned with friendly people that will be happy to help.

    The instruction above will get you the latest stable release of BAP. If you're interested in our rolling releases, which are automatically updated every time a commit to the master branch happens, then you can create a new switch that uses our testing repository

    opam switch create bap-testing --repos \
        default,bap=git+https://github.com/BinaryAnalysisPlatform/opam-repository#testing 4.14.1
    opam install bap
    

    After it is added, the bap repository will take precedence over the stable repository and you will get the freshly picked BAP packages straight from the farm.

    If you want to build BAP manually or just want to tackle with BAP internals, then you can clone this repository and build it manually. We suggest your starting with a fresh environment without BAP being installed, to prevent clashes, or even better to use a local switch, e.g.,

    git clone [email protected]:BinaryAnalysisPlatform/bap.git && cd bap
    opam switch create . --deps-only
    dune build && dune install
    

    The snippet above will clone bap, create a fresh local switch, install the necessary dependencies, including the system one, and, finally, build and install bap with dune. Alternatively, if you already have a switch where you want to build and install bap, you can use

    git clone [email protected]:BinaryAnalysisPlatform/bap.git && cd bap
    opam install . --deps-only
    dune build && dune install
    

    to install bap and its dependencies into the currently selected switch.

    The above command will build all packages available in bap, including experimental packages like Ghidra or Radare (included in the bap-extra) meta-package. To prepare development envionment that excludes these packages (or any other packages) just remove corresponding .opam files (don't worry, opam will regenerate them later), before creating a switch e.g.,

    rm bap-{extra,radare2,ghidra,primus-symbolic-executor,ida}.opam
    opam switch create . --deps-only
    dune build && dune install
    

    Note, if you don't have the llvm-config executable in the path, then run

    ocaml tools/configure.ml --with-llvm-config=<path-to-you-llvm-config>
    

    where <path-to-your-llvm-config> is usually llvm-config-<ver>, e.g., llvm-config-11.

    Using

    BAP, like Docker or Git, is driven by a single command-line utility called bap. Just type bap in your shell and it will print a message which shows BAP capabilities. The disassemble command will take a binary program, disassemble it, lift it into the intermediate architecture agnostic representation, build a control flow graph, and finally apply staged user-defined analysis in a form of disassembling passes. Finally, the --dump option (-d in short) will output the resulting program in the specified format. This is the default command, so you don't even need to specify it, e.g., the following will disassembled and dump the /bin/echo binary on your machine:

    bap /bin/echo -d
    

    Note, that unlike objdump this command will build the control flow graph of a program. If you just want to dump each instruction of a binary one after another (the so-called linear sweep disassembler mode), then you can use the objdump command, e.g.,

    bap objdump /bin/echo --show-{insn=asm,bil}
    

    If your input is a blob of machine code, not an executable, then you can use the raw loader, e.g.,

    bap objdump /bin/echo --loader=raw --raw-base=0x400000 --show-{insn=asm,bil}
    

    The raw loader takes a few parameters, like offsets, lengths, and base addresses, which makes it a swiss-knife that you can use as a can opener for formats that are not known to BAP. The raw loader works for all commands that open files, e.g., if the raw loader is used together with the disassemble command, BAP will still automatically identify function starts and build a suitable CFG without even knowing where the code is in the binary,

    bap /bin/echo --loader=raw --raw-base=0x400000 -d
    

    If you would like to play manually with bytes, e.g., type the instruction encoding manually and see how BAP disassembles it and what semantics it has, then mc is the command you're looking for. It is named for the corresponding utility in LLVM and stands for machine code and has the same interface as the objdump command except that it takes an ASCII encoding of instruction instead of a binary file, e.g.,

    bap mc --show-{insn=asm,bil} -- 48 83 ec 08
    

    or

    bap mc --show-{insn=asm,bil} "\x48\x83\xec\x08"
    

    It recognizes a few input formats (including llvm-mc is using for its -show-encoding option). Consult the documentation for more detailed information.

    Extending

    Writing your own analysis

    BAP is a plugin-based framework and if you want to develop a new analysis you can write a plugin, build it, install, and it will work with the rest of the BAP without any recompilation. There are many extension points that you could use to add new analysis, change existing, or even build your own applications. We will start with a simple example, that registers a disassembling pass to the disassemble command. Suppose that we want to write an analysis that estimates the ratio of jump instructions to the total number of instructions in the binary. We will start by creating an empty file named jmp.ml in an empty folder (the folder name doesn't matter). Next, using our favorite text editor we will put the following code into it:

    open Core_kernel
    open Bap_main
    open Bap.Std
    
    let counter = object
      inherit [int * int] Term.visitor
      method! enter_term _ _ (jmps,total) = jmps,total+1
      method! enter_jmp _ (jmps,total) = jmps+1,total
    end
    
    let main proj =
      let jmps,total = counter#run (Project.program proj) (0,0) in
      printf "ratio = %d/%d = %g\n" jmps total (float jmps /. float total)
    
    let () = Extension.declare @@ fun _ctxt ->
       Project.register_pass' main;
       Ok ()
    

    Now we can build, install, and run our analysis using the following commands:

    bapbuild jmp.plugin
    bapbundle install jmp.plugin
    bap /bin/echo --pass=jmp
    

    Let's briefly go through the code. The counter object is a visitor that has the state consisting of a pair of counters. The first counter keeps track of the number of jmp terms, and the second counter is incremented every time we enter any term. The main function just runs the counter and prints the output. We declare our extension use the Extension.declare function from the Bap_main library. An extension is just a function that receives the context (which could be used to obtain configuration parameters). In this function, we register our main function as a pass using the Project.register_pass function.

    A little bit more complex example, as well as an example that uses Python, can be found in our tutorial.

    Building a plugin with dune

    You can also build and install bap plugins using dune. For that, you need to define a library and use the plugin stanza that uses this library. Below is the template dune file,

    (library
     (name FOO)
     (public_name OUR-FOO.plugin)
     (libraries bap bap-main))
    
    (plugin
     (name FOO)
     (package OUR-FOO)
     (libraries OUR-FOO.plugin)
     (site (bap-common plugins)))
    

    Eveything that is capitalized in the above snippet is a placeholder that you shall substitute with appropriate private and public names for your plugin. Notice, that the .plugin extension is not necessary, but is percieved as a good convention.

    Interactive REPL

    BAP also ships an interactive toplevel utility baptop. This is a shell-like utility that interactively evaluates OCaml expressions and prints their values. It will load BAP libraries and initialize all plugins for you, so you can interactively explore the vast world of BAP. The baptop utility can also serve as a non-interactive interpreter, so that you can run your OCaml scripts, e.g., baptop myscript.ml or you can even specify it using sha-bang at the top of your file, e.g., #!/usr/bin/env baptop. We built baptop using UTop, but you can easily use any other OCaml toplevel, including ocaml itself, just load the bap.top library, e.g., for vanilla ocaml toplevel use the following directives

    #use "topfind";;
    #require "bap.top";;
    

    Learning

    We understand that BAP is huge and it is easy to get lost. We're working constantly on improving documentation ensuring that every single function in BAP API is thoroughly documented. But writing higher-level guidelines in the form of manuals or tutorials is much harder and very time consuming, especially given how different the goals of our fellow researchers and users. Therefore we employ a backward-chaining approach and prefer to answer real questions rather than prematurely trying to address all possible questions. We will be happy to see you in your chat that features searchable, indexed by Google, archive.

    We are writing, occasionally, to our blog and wiki and are encouraging everyone to contribute to both of them. You can also post your questions on stackoverflow or discuss BAP on the OCaml board. We also have a cute discord channel, which has much less traffic than our gitter.

    Contributing

    BAP is built by the community and we're welcome all contributions from authors that are willing to share them under the MIT license. If you don't think that your analysis or tool suits this repository (e.g., it has a limited use, not fully ready, doesn't meet our standards, etc), then you can consider contributing to our bap-plugins repository that is a collection of useful BAP plugins that are not mature enough to be included in the main distribution. Alternatively, you can consider extending our toolkit with your tool.

    Of course, there is no need to submit your work to one of our repositories. BAP is a plugin-based framework and your code could be hosted anywhere and have any license (including proprietary). If you want to make your work available to the community it would be a good idea to release it via opam.

    Sponsors

    • ForAllSecure

    • Boeing

    • DARPA VET Project

    • Siemens AG

    • Institute for Information & communications Technology Promotion(IITP) grant funded by the Korea government(MSIT) (No.2015-0-00565, Development of Vulnerability Discovery Technologies for IoT Software Security)

    Please, contact us if you would like to become a sponsor or are seeking a deeper collaboration.