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

Class Method Details

.authorsObject



147
148
149
# File 'lib/pwn/plugins/binary_parser.rb', line 147

public_class_method def self.authors
  "AUTHOR(S):\n  0day Inc. <[email protected]>\n"
end

.diff(opts = {}) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/pwn/plugins/binary_parser.rb', line 129

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

.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

.helpObject



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
199
200
# File 'lib/pwn/plugins/binary_parser.rb', line 151

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'
    )

    # Structured binary triage JSON plus an artifact path under ~/.pwn/artifacts/triage.
    #{self}.triage(
      path: 'required - filesystem path of the binary 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_binsObject



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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/pwn/plugins/binary_parser.rb', line 85

public_class_method def self.triage(opts = {})
  path = opts[:path].to_s
  inf = info(opts)
  secs = sections(opts)
  imps = Array(imports(opts)).first(40)
  exps = Array(exports(opts)).first(40)
  summary = {
    format: inf[:format],
    arch: inf[:cpu],
    bits: inf[:cpu].to_s[/\d+/],
    endian: inf[:endian] || inf[:ei_data],
    stripped: inf[:stripped],
    static: inf[:static],
    protections: {
      nx: inf[:nx],
      pie: inf[:pie],
      relro: inf[:relro],
      canary: inf[:canary],
      fortify: inf[:fortify]
    },
    sections: secs,
    imports: imps,
    exports: exps,
    strings_top: Array(inf[:strings]).first(20),
    entry: inf[:entry] || inf[:entrypoint],
    function_count: Array(symbols(opts)).length,
    notes: []
  }
  sha = Digest::SHA256.file(path).hexdigest
  dir = File.join(Dir.home, '.pwn', 'cache', 'triage')
  FileUtils.mkdir_p(dir)
  art = File.join(dir, "#{sha}.json")
  if File.file?(art)
    cached = JSON.parse(File.read(art), symbolize_names: true)
    return cached.merge(cached: true, artifact: art)
  end
  body = summary.merge(path: path, sha256: sha)
  File.write(art, 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))
  body.merge(artifact: art, cached: false, facts: File.join(facts_dir, 'facts.json'))
end