Module: PWN::Plugins::BinaryParser
- Defined in:
- lib/pwn/plugins/binary_parser.rb
Overview
ELF/PE/Mach-O headers, sections, symbols, imports/exports, relocations via Metasm loaders.
Class Method Summary collapse
- .authors ⇒ Object
- .diff(opts = {}) ⇒ Object
- .elf_resolve(opts = {}) ⇒ Object
- .exports(opts = {}) ⇒ Object
- .help ⇒ Object
- .imports(opts = {}) ⇒ Object
- .info(opts = {}) ⇒ Object
- .load_exe(opts = {}) ⇒ Object
- .relocations(opts = {}) ⇒ Object
- .required_bins ⇒ Object
- .sections(opts = {}) ⇒ Object
- .symbols(opts = {}) ⇒ Object
- .triage(opts = {}) ⇒ Object
Class Method Details
.authors ⇒ Object
139 140 141 |
# File 'lib/pwn/plugins/binary_parser.rb', line 139 public_class_method def self. "AUTHOR(S):\n 0day Inc. <[email protected]>\n" end |
.diff(opts = {}) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/pwn/plugins/binary_parser.rb', line 121 public_class_method def self.diff(opts = {}) a = (opts[:a] || opts[:old]).to_s b = (opts[:b] || opts[:new]).to_s raise 'ERROR: a and b are required' if a.empty? || b.empty? return { error: 'radiff2 missing', hint: 'pwn setup --profile re', a: a, b: b } unless PWN::Plugins::PreflightChecker.bin?(name: 'radiff2') stdout, stderr, status = Open3.capture3('radiff2', '-C', a, b) funcs = stdout.lines.filter_map do |ln| next unless ln.include?(' | ') cols = ln.split('|').map(&:strip) { a: cols[0], similarity: cols[1].to_f, b: cols[2], changed: cols[1].to_f < 100 } end funcs = funcs.sort_by { |r| r[:similarity].to_f } { a: a, b: b, functions: funcs, stderr: stderr, exit: status.exitstatus } end |
.elf_resolve(opts = {}) ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/pwn/plugins/binary_parser.rb', line 85 public_class_method def self.elf_resolve(opts = {}) path = opts[:path].to_s raise ArgumentError, 'path is required' if path.empty? raise "ERROR: file not found: #{path}" unless File.file?(path) data = File.binread(path) raise ArgumentError, 'not an ELF' unless data.start_with?("\x7fELF".b) resolve_elf(bytes: data, path: path) end |
.exports(opts = {}) ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/pwn/plugins/binary_parser.rb', line 65 public_class_method def self.exports(opts = {}) exe = load_exe(opts) if exe.respond_to?(:export) Array(exe.export&.exports).map { |e| e.respond_to?(:name) ? e.name : e.to_s } else [] end rescue StandardError [] end |
.help ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/pwn/plugins/binary_parser.rb', line 143 public_class_method def self.help puts "USAGE: # List host binaries this module expects to be installed. #{self}.required_bins # Run load exe and return its result #{self}.load_exe( path: 'required - filesystem path to read or write' ) # Run info and return its result #{self}.info # Run sections and return its result #{self}.sections # Run symbols and return its result #{self}.symbols( limit: 'optional - limit value consumed by #symbols' ) # Run imports and return its result #{self}.imports # Run exports and return its result #{self}.exports # Run relocations and return its result #{self}.relocations( limit: 'optional - limit value consumed by #relocations' ) # Resolve ELF symbols, GOT, and PLT into queryable hashes. #{self}.elf_resolve( path: 'required - filesystem path of the ELF' ) # Structured binary triage JSON plus an artifact store handle. #{self}.triage( path: 'required - filesystem path of the binary to triage', session_id: 'optional - artifact session id (defaults to triage)' ) # Diff two binaries via radiff2 -C (changed functions first). #{self}.diff( a: 'required - filesystem path of the first binary', b: 'required - filesystem path of the second binary', old: 'optional - alias for a', new: 'optional - alias for b' ) # Print the AUTHOR(S) string for this module. #{self}.authors " constants.sort end |
.imports(opts = {}) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/pwn/plugins/binary_parser.rb', line 56 public_class_method def self.imports(opts = {}) exe = load_exe(opts) if exe.respond_to?(:imports) Array(exe.imports).map(&:to_s) else symbols(opts).map { |s| s[:name].to_s }.grep(/plt|imp/i).first(100) end end |
.info(opts = {}) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/pwn/plugins/binary_parser.rb', line 26 public_class_method def self.info(opts = {}) exe = load_exe(opts) { class: exe.class.name, cpu: exe.cpu.class.name, entrypoint: (exe.optheader.entrypoint if exe.respond_to?(:optheader) && exe.optheader.respond_to?(:entrypoint)), format: format_name(exe: exe) } end |
.load_exe(opts = {}) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/pwn/plugins/binary_parser.rb', line 18 public_class_method def self.load_exe(opts = {}) path = opts[:path].to_s raise 'ERROR: path is required' if path.empty? raise "ERROR: file not found: #{path}" unless File.file?(path) Metasm::AutoExe.decode_file(path) end |
.relocations(opts = {}) ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/pwn/plugins/binary_parser.rb', line 76 public_class_method def self.relocations(opts = {}) exe = load_exe(opts) return [] unless exe.respond_to?(:relocations) Array(exe.relocations).first((opts[:limit] || 100).to_i).map(&:to_s) rescue StandardError [] end |
.required_bins ⇒ Object
14 15 16 |
# File 'lib/pwn/plugins/binary_parser.rb', line 14 public_class_method def self.required_bins [] end |
.sections(opts = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/pwn/plugins/binary_parser.rb', line 36 public_class_method def self.sections(opts = {}) exe = load_exe(opts) Array(exe.sections).map do |sec| { name: (sec.name if sec.respond_to?(:name)), size: (sec.size if sec.respond_to?(:size)), virtaddr: (sec.virtaddr if sec.respond_to?(:virtaddr)) } end end |
.symbols(opts = {}) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/pwn/plugins/binary_parser.rb', line 47 public_class_method def self.symbols(opts = {}) exe = load_exe(opts) return [] unless exe.respond_to?(:symbols) Array(exe.symbols).first((opts[:limit] || 200).to_i).map do |sym| { name: (sym.name if sym.respond_to?(:name)), value: (sym.value if sym.respond_to?(:value)) } end end |
.triage(opts = {}) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/pwn/plugins/binary_parser.rb', line 96 public_class_method def self.triage(opts = {}) path = opts[:path].to_s raise ArgumentError, 'path is required' if path.empty? raise "ERROR: file not found: #{path}" unless File.file?(path) sha = Digest::SHA256.file(path).hexdigest sid = (opts[:session_id] || 'triage').to_s cache_dir = File.join(Dir.home, '.pwn', 'cache', 'triage') FileUtils.mkdir_p(cache_dir) cache = File.join(cache_dir, "#{sha}.json") if File.file?(cache) cached = JSON.parse(File.read(cache), symbolize_names: true) spilled = persist_triage(body: cached, session_id: sid) return cached.merge(cached: true, handle: spilled[:handle], artifact: spilled) end body = inspect_binary(opts.merge(path: path, sha256: sha)) File.write(cache, JSON.pretty_generate(body)) facts_dir = File.join(Dir.home, '.pwn', 'engagements', 'default', 'binaries', sha) FileUtils.mkdir_p(facts_dir) File.write(File.join(facts_dir, 'facts.json'), JSON.pretty_generate(body)) spilled = persist_triage(body: body, session_id: sid) body.merge(artifact: spilled, handle: spilled[:handle], cached: false, facts: File.join(facts_dir, 'facts.json')) end |