Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bin2json features #10

Open
wants to merge 44 commits into
base: oasis-lib
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
17c7612
added build prerequisite to other targets;
m4b Jul 11, 2015
06ec53e
added compute_unknown in ByteCoverage to fill in unknown values; adde…
m4b Jul 27, 2015
b7fb8f3
ported majority of mach parsing over to Mach.ml and completely remove…
m4b Jul 27, 2015
7b65cb4
got mach to a stable build point again and moved most of logic out of…
m4b Jul 28, 2015
bb217aa
fixed Goblin.Export printers; mimicing GoblinSymbols now; added sorti…
m4b Jul 28, 2015
a694011
implemented sorting for mach imports;
m4b Jul 28, 2015
b56a681
moved sorting into getters for imports and exports;
m4b Jul 28, 2015
ea4c2e2
added mach printer and to_string;
m4b Jul 28, 2015
d63dce7
formatting;
m4b Jul 28, 2015
6b815c2
renamed segment64 to section;
m4b Jul 28, 2015
ca238b4
added todo in ByteCoverage and new kind marker;
m4b Jul 28, 2015
07e2059
added rdr coverage printer in config, rdr exe and printer for elf; to…
m4b Jul 28, 2015
b6ac5db
fixed byte coverage to now include overlapping layers; sketched out a…
m4b Jul 29, 2015
fcba03a
added mach coverage module with preliminary header computations;
m4b Jul 29, 2015
bc4adfd
added initial mach segment coverage computer; realized mach load comm…
m4b Jul 29, 2015
1ec5f86
removed todo;
m4b Jul 29, 2015
33fd02e
broken commit; middle of fixing/breaking load commands;
m4b Jul 30, 2015
dae8041
added loadcommandtypes;
m4b Jul 30, 2015
a49930f
mach load command work is finished; settled on a record with cmd; cmd…
m4b Jul 31, 2015
eb095e2
implemented passable byte converage algorithm; it scans bytes, marks …
m4b Jul 31, 2015
9f0121f
added comments for bytecoverage;
m4b Jul 31, 2015
71fe131
implemented a more granular mach coverage; for small binaries, becaus…
m4b Aug 2, 2015
b700009
added more granular dyldinfo details; added nlist todo in mach covera…
m4b Aug 2, 2015
c7c9320
formatting;
m4b Aug 2, 2015
a539db8
added location of brokenness comment for scanning a go program /usr/l…
m4b Aug 3, 2015
dd85379
fixed mach size calculation regression;
m4b Aug 4, 2015
fd8cd2c
fixed longstanding bug with calculating slide sectors; can now comple…
m4b Aug 4, 2015
94556c3
Merge branch 'mach-fix' of github.com:m4b/rdr into mach-fix
m4b Aug 4, 2015
3de1f1e
added is64 to elf; added more flags to program types; added PT append…
m4b Aug 9, 2015
1840b56
using oasis makefile;
m4b Aug 17, 2015
52a8196
updated readme;
m4b Aug 17, 2015
ebde9fe
updated oasis version;
m4b Aug 17, 2015
f7683b8
version increment, fix opam, other details; added binary removal and …
m4b Aug 17, 2015
e98dd4a
added setup.ml;
m4b Aug 17, 2015
25ee232
removed unused garbage in leb128 and removed whitespace;
m4b Aug 17, 2015
c73ffa4
using oasis2opam;
m4b Aug 17, 2015
b572ec1
updated setup;
m4b Aug 17, 2015
fe44185
added dylib and mllib;
m4b Aug 17, 2015
63fb290
added moar mlldylibs;
m4b Aug 17, 2015
0ae2445
added src mldylib and mllib;
m4b Aug 17, 2015
7478b01
updated to 2.0.1, added meta, etc.;
m4b Aug 17, 2015
44c1e3f
implemented Rdr level object parser; added/fixed ELF specific byte co…
m4b Aug 17, 2015
153ec0c
merge master with rdr updates
m4b Sep 2, 2015
3de9933
added macro-generated load command file; updated oasis and removed no…
m4b Sep 5, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,4 @@ darwin/*
*.docdir

setup.data
setup.log

META
*.mldylib
*.mllib
setup.log
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# Welcome
# Rdr 2.0 - Welcome

[![Floobits Status](https://floobits.com/m4b/rdr.svg)](https://floobits.com/m4b/rdr/redirect)

**UPDATE**:

> `rdr` is now version 2.0, which adds a new "byte coverage" algorithm for returning understood sections/segments with respect to a binary. Check it out with `rdr -c <path to binary>`.
> Moreover, `rdr` is now a binary _and_ a library, which you all can link against and use in your own projects, if you so desire. [elf2json](http://github.com/m4b/elf2json) is a new program demonstrating this.

Welcome to the `rdr` project.

`rdr` is an OCaml tool/library for doing cross-platform analysis of binaries. I typically use it for looking up symbol names, finding the address offset, and then running `gdb` or `lldb` to mess around (you should be using both if you even know what you're doing).
Expand Down
75 changes: 56 additions & 19 deletions _oasis
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
OASISFormat: 0.4
Plugins: META (0.4), DevFiles (0.4)
Name: rdr
Version: 1.2
Version: 2.0.1
Synopsis: Lightweight, cross platform binary parsing and analysis library with no dependencies
Authors: m4b
Homepage: http://github.com/m4b/rdr
Maintainers: <[email protected]>
License: BSD-3-clause
Description:`rdr` is an OCaml tool/library for doing cross-platform analysis of binaries,
by printing headers, locating entry points, showing import and export
symbols, their binary offsets and size, etc.

It also features a symbol map which allows fast lookups for arbitrary
symbols, and their associated data, on your system
(the default search location are binaries in /usr/lib).

The latest release also makes `rdr` a package which you can link against
and use in your own projects.

See the README at http://github.com/m4b/rdr for more details.

Features:

* 64-bit Linux and Mach-o binary analysis
* Searchable symbol-map of all the symbols on your system, including binary
offset, size, and exporting library
* Print imports and exports of binaries
* Make pretty graphs, at the binary or symbol map level
* Byte Coverage algorithm which marks byte sequences as understood (or not)
and provides other meta-data


Library "utils"
Path: lib/utils
BuildTools: ocamlbuild
FindLibParent: rdr
FindLibName: utils
CompiledObject: best
Modules:
RdrUtils,
Binary,
Expand All @@ -19,47 +45,38 @@ Library "utils"
ByteCoverage,
Generics

Library "goblin"
Path: lib/goblin
BuildTools: ocamlbuild
FindLibParent: rdr
FindLibName: goblin
Modules:
Goblin,
GoblinSymbol,
GoblinExport,
GoblinImport
BuildDepends:
rdr.utils

Library "mach"
Path: lib/mach
BuildTools: ocamlbuild
FindLibParent: rdr
FindLibName: mach
CompiledObject: best
Modules:
Mach,
MachBindOpcodes,
MachCpuTypes,
MachFat,
MachLoadCommand,
MachLoadCommandTypes,
MachConstants,
MachExports,
MachHeader,
MachImports,
MachSegment64,
MachNlist,
MachSection,
MachSymbolTable,
MachRebaseOpcodes,
MachVersion
MachVersion,
MachCoverage,
MachLoadCommandMacro
BuildDepends:
rdr.utils,
rdr.goblin
rdr.utils

Library "elf"
Path: lib/elf
FindLibParent: rdr
FindLibName: elf
BuildTools: ocamlbuild
CompiledObject: best
Modules:
Elf,
ElfHeader,
Expand All @@ -73,10 +90,29 @@ Library "elf"
BuildDepends:
rdr.utils

Library "goblin"
Path: lib/goblin
BuildTools: ocamlbuild
FindLibParent: rdr
FindLibName: goblin
CompiledObject: best
Modules:
Goblin,
GoblinSymbol,
GoblinExport,
GoblinImport,
GoblinMach,
GoblinElf
BuildDepends:
rdr.utils,
rdr.mach,
rdr.elf

Library "rdr"
Path: lib
FindLibName: rdr
BuildTools: ocamlbuild
CompiledObject: best
Modules:
LibRdr
BuildDepends:
Expand All @@ -89,6 +125,7 @@ Library "rdrutils"
Path: src
BuildTools: ocamlbuild
Install: false
CompiledObject: best
Modules:
Config,
Command,
Expand Down
11 changes: 6 additions & 5 deletions _tags
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# OASIS_START
# DO NOT EDIT (digest: 561c0ff1381a99f143e3c3b7c8381ca7)
# DO NOT EDIT (digest: 28b9a39932677973ac2e4a14f90407c6)
# Ignore VCS directories, you can use the same kind of rule outside
# OASIS_START/STOP if you want to exclude directories that contains
# useless stuff for the build process
Expand All @@ -16,16 +16,17 @@ true: annot, bin_annot
"_darcs": not_hygienic
# Library utils
"lib/utils/utils.cmxs": use_utils
# Library goblin
"lib/goblin/goblin.cmxs": use_goblin
<lib/goblin/*.ml{,i,y}>: use_utils
# Library mach
"lib/mach/mach.cmxs": use_mach
<lib/mach/*.ml{,i,y}>: use_goblin
<lib/mach/*.ml{,i,y}>: use_utils
# Library elf
"lib/elf/elf.cmxs": use_elf
<lib/elf/*.ml{,i,y}>: use_utils
# Library goblin
"lib/goblin/goblin.cmxs": use_goblin
<lib/goblin/*.ml{,i,y}>: use_elf
<lib/goblin/*.ml{,i,y}>: use_mach
<lib/goblin/*.ml{,i,y}>: use_utils
# Library rdr
"lib/rdr.cmxs": use_rdr
<lib/*.ml{,i,y}>: use_elf
Expand Down
94 changes: 94 additions & 0 deletions lib/LibRdr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,97 @@ module Elf = Elf
module Mach = Mach
module Goblin = Goblin
module Utils = RdrUtils

module Object = struct
type t = | Mach of bytes | Elf of bytes | PE of bytes | Unknown of string * string
let get ?verbose:(verbose=false) filename =
let ic = open_in_bin filename in
if (in_channel_length ic < 4) then
(* 4 bytes, less than any magic number we're looking for *)
begin
close_in ic;
Unknown (filename, "less than 4 bytes")
end
else
(* BEGIN Binary cases *)
let magic = Input.input_i32be ic in
if (verbose) then
Printf.printf "opening %s with magic: 0x%x\n" filename magic;
(* MACH FAT *)
if (magic = Mach.Fat.kFAT_MAGIC) (* cafe babe *) then
let nfat_arch = Input.input_i32be ic in
if (nfat_arch > 4) then (* hack to avoid java class file errors which have same magic num *)
begin
close_in ic;
Unknown (filename, "mach fat too many archs (probably a java class file)")
end
else
let sizeof_arch_bytes = nfat_arch * Mach.Fat.sizeof_fat_arch in
let fat_arch_bytes = Bytes.create sizeof_arch_bytes in
really_input ic fat_arch_bytes 0 sizeof_arch_bytes;
let offset =
Mach.Fat.get_x86_64_binary_offset fat_arch_bytes nfat_arch in
match offset with
| Some (offset, size) ->
seek_in ic offset;
let magic = Input.input_i32be ic in
if (magic = Mach.Header.kMH_CIGAM_64) then
begin
seek_in ic offset;
let binary = Bytes.create size in
really_input ic binary 0 size;
close_in ic;
Mach binary
end
else
begin
close_in ic; Unknown (filename, "mach fat has no 64 bit binaries")
end
| None ->
close_in ic;
Printf.eprintf "<Rdr.Object> ERROR, bad binary: %s\n" filename;
Unknown (filename, "mach fat has no binaries")
(* backwards cause we read the 32bit int big E style *)
(* MACH *)
else if (magic = Mach.Header.kMH_CIGAM_64) then
begin
seek_in ic 0;
let binary = Bytes.create (in_channel_length ic) in
really_input ic binary 0 (in_channel_length ic);
close_in ic;
Mach binary
end
(* ELF *)
else if (magic = Elf.Header.kMAGIC_ELF) then
begin
seek_in ic 0;
let binary = Bytes.create (in_channel_length ic) in
really_input ic binary 0 (in_channel_length ic);
close_in ic;
if (Elf.Header.check_64bit binary) then
Elf binary
else
Unknown (filename, "elf binary is not 64-bit")
end
(* PE *)
(*
else if (magic = Elf.Header.kMAGIC_ELF) then
begin
seek_in ic 0;
let binary = Bytes.create (in_channel_length ic) in
really_input ic binary 0 (in_channel_length ic);
close_in ic;
if (Elf.Header.check_64bit binary) then
Elf binary
else
Unknown filename
end
*)
else
begin
close_in ic;
if (verbose) then
Printf.printf "ignoring binary: %s\n" filename;
Unknown (filename, "unknown magic number")
end
end
59 changes: 59 additions & 0 deletions lib/META
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# OASIS_START
# DO NOT EDIT (digest: 19245a18a78bafcbdce567c9476d0a71)
version = "2.0.1"
description =
"Lightweight, cross platform binary parsing and analysis library with no dependencies"
requires = "rdr.goblin rdr.utils rdr.mach rdr.elf"
archive(byte) = "rdr.cma"
archive(byte, plugin) = "rdr.cma"
archive(native) = "rdr.cmxa"
archive(native, plugin) = "rdr.cmxs"
exists_if = "rdr.cma"
package "utils" (
version = "2.0.1"
description =
"Lightweight, cross platform binary parsing and analysis library with no dependencies"
archive(byte) = "utils.cma"
archive(byte, plugin) = "utils.cma"
archive(native) = "utils.cmxa"
archive(native, plugin) = "utils.cmxs"
exists_if = "utils.cma"
)

package "mach" (
version = "2.0.1"
description =
"Lightweight, cross platform binary parsing and analysis library with no dependencies"
requires = "rdr.utils"
archive(byte) = "mach.cma"
archive(byte, plugin) = "mach.cma"
archive(native) = "mach.cmxa"
archive(native, plugin) = "mach.cmxs"
exists_if = "mach.cma"
)

package "goblin" (
version = "2.0.1"
description =
"Lightweight, cross platform binary parsing and analysis library with no dependencies"
requires = "rdr.utils rdr.mach rdr.elf"
archive(byte) = "goblin.cma"
archive(byte, plugin) = "goblin.cma"
archive(native) = "goblin.cmxa"
archive(native, plugin) = "goblin.cmxs"
exists_if = "goblin.cma"
)

package "elf" (
version = "2.0.1"
description =
"Lightweight, cross platform binary parsing and analysis library with no dependencies"
requires = "rdr.utils"
archive(byte) = "elf.cma"
archive(byte, plugin) = "elf.cma"
archive(native) = "elf.cmxa"
archive(native, plugin) = "elf.cmxs"
exists_if = "elf.cma"
)
# OASIS_STOP

Loading