|
6 | 6 | require "./src/extract-upx" |
7 | 7 | require "./src/extract-uefi" |
8 | 8 | require "./src/extract-7z" |
| 9 | +require "./src/extract-polyglot" |
| 10 | +require "./src/extract-zlib" |
9 | 11 |
|
10 | 12 | module VBiosFinder |
11 | 13 | class Main |
12 | | - def self.run file |
13 | | - puts "trying to extract #{file}" |
| 14 | + @extractions = [] |
| 15 | + @extractions << [:polyglot, "polyglot", "builtin module for polyglot files"] |
| 16 | + @extractions << [:zlib, "zlib", "builtin module for zlib data"] |
| 17 | + @extractions << [:innosetup, "innoextract", "required for Inno Installers"] |
| 18 | + @extractions << [:upx, "upx", "required for UPX executables"] |
| 19 | + @extractions << [:p7zip, "7z", "required for 7z (self-extracting) archives"] |
14 | 20 |
|
| 21 | + def self.extract file |
| 22 | + puts "trying to extract #{file}" |
15 | 23 | # Attempt all known extraction methods |
16 | | - extractions = [] |
17 | | - extractions << [:innosetup, "innoextract", "required for Inno Installers", file] |
18 | | - extractions << [:upx, "upx", "required for UPX executables", file] |
19 | | - extractions << [:p7zip, "7z", "required for 7z (self-extracting) archives", file] |
20 | | - extractions.each{|e| Extraction::attempt(*e)} |
| 24 | + @extractions.each{|e| Extraction::attempt(*e, file)} |
| 25 | + end |
| 26 | + |
| 27 | + def self.run file |
| 28 | + @extractions.select! do |sym, requires, reason, arg| |
| 29 | + reason.start_with?("builtin") || Utils::installed?(requires, reason) |
| 30 | + end |
| 31 | + |
| 32 | + files = Utils::get_new_files |
| 33 | + files << file |
| 34 | + |
| 35 | + while files.size > 0 |
| 36 | + files.each do |e| |
| 37 | + extract e |
| 38 | + end |
| 39 | + files = Utils::get_new_files |
| 40 | + end |
21 | 41 |
|
22 | | - # Try to find an UEFI bios image now |
23 | | - if Utils::installed?("UEFIDump", "required for UEFI images") && Test::uefi(file) |
24 | | - puts "found UEFI image".colorize(:green) |
25 | | - outpath = "#{Dir.pwd}/../output" |
26 | | - FileUtils.mkdir_p outpath |
27 | | - FileUtils.cp file, "#{outpath}/bios_#{File.basename file}" |
28 | | - Extract::uefi file |
29 | | - puts "extracted. filtering modules...".colorize(:blue) |
30 | | - modules = Find.find("#{file}.dump").reject{|e| File.directory? e}.select{|e| e.end_with? ".bin"} |
31 | | - puts "got #{modules.length} modules".colorize(:blue) |
32 | | - puts "finding vbios".colorize(:blue) |
33 | | - line = Cocaine::CommandLine.new("file", "-b :file") |
34 | | - modules = modules.select{|e| line.run(file: e).include? "Video"} |
35 | | - if modules.length > 0 |
36 | | - puts "#{modules.length} possible candidates".colorize(:green) |
37 | | - if Utils::installed?("rom-parser", "required for proper rom naming & higher accuracy") |
38 | | - modules.each do |mod| |
39 | | - rom_parser = Cocaine::CommandLine.new("rom-parser", ":file") |
40 | | - begin |
41 | | - romdata = rom_parser.run(file: mod) |
42 | | - romdata = romdata.split("\n")[1].split(", ").map{|e| e.split(": ")}.to_h rescue nil |
43 | | - unless romdata.nil? || romdata['vendor'].nil? || romdata['device'].nil? |
44 | | - puts "Found VBIOS for device #{romdata['vendor']}:#{romdata['device']}!".colorize(:green) |
45 | | - new_filename = "vbios_#{romdata['vendor']}_#{romdata['device']}.rom" |
46 | | - FileUtils.cp(mod, "#{outpath}/#{new_filename}") |
47 | | - end |
48 | | - rescue Cocaine::ExitStatusError => e |
49 | | - puts "can't determine vbios type" |
| 42 | + puts "extracting uefi data".colorize(:blue) |
| 43 | + Find.find(".").reject{|e| File.directory? e}.each do |e| |
| 44 | + puts "trying to extract #{e}" |
| 45 | + Extraction::attempt(:uefi, "UEFIExtract", "required for UEFI images", e) |
| 46 | + end |
| 47 | + |
| 48 | + outpath = "#{Dir.pwd}/../output" |
| 49 | + FileUtils.mkdir_p outpath |
| 50 | + FileUtils.cp file, "#{outpath}/bios_#{File.basename file}" |
| 51 | + puts "filtering for modules...".colorize(:blue) |
| 52 | + uefibins = Find.find(".").reject{|e| File.directory? e}.select{|e| e.end_with? ".bin"} |
| 53 | + puts "got #{uefibins.length} modules".colorize(:blue) |
| 54 | + puts "finding vbios".colorize(:blue) |
| 55 | + line = Cocaine::CommandLine.new("file", "-b :file") |
| 56 | + modules = uefibins.select{|e| line.run(file: e).include? "Video"} |
| 57 | + if modules.length > 0 |
| 58 | + puts "#{modules.length} possible candidates".colorize(:green) |
| 59 | + if Utils::installed?("rom-parser", "required for proper rom naming & higher accuracy") |
| 60 | + modules.each do |mod| |
| 61 | + rom_parser = Cocaine::CommandLine.new("rom-parser", ":file") |
| 62 | + begin |
| 63 | + romdata = rom_parser.run(file: mod) |
| 64 | + romdata = romdata.split("\n")[1].split(", ").map{|e| e.split(": ")}.to_h rescue nil |
| 65 | + unless romdata.nil? || romdata['vendor'].nil? || romdata['device'].nil? |
| 66 | + puts "Found VBIOS for device #{romdata['vendor']}:#{romdata['device']}!".colorize(:green) |
| 67 | + new_filename = "vbios_#{romdata['vendor']}_#{romdata['device']}.rom" |
| 68 | + FileUtils.cp(mod, "#{outpath}/#{new_filename}") |
50 | 69 | end |
51 | | - end |
52 | | - else |
53 | | - modules.each do |mod| |
54 | | - FileUtils.cp(mod, outpath) |
| 70 | + rescue Cocaine::ExitStatusError => e |
| 71 | + puts "can't determine vbios type" |
55 | 72 | end |
56 | 73 | end |
57 | | - puts "Job done. Extracted files can be found in #{outpath}".colorize(:green) |
58 | 74 | else |
59 | | - puts "no candidates found :(".colorize(:red) |
| 75 | + modules.each do |mod| |
| 76 | + FileUtils.cp(mod, outpath) |
| 77 | + end |
60 | 78 | end |
61 | | - exit 0 |
| 79 | + puts "Job done. Extracted files can be found in #{outpath}".colorize(:green) |
62 | 80 | else |
63 | | - puts "not an uefi image" |
64 | | - end |
65 | | - |
66 | | - Utils::get_new_files.each do |e| |
67 | | - puts |
68 | | - run e |
| 81 | + puts "no candidates found :(".colorize(:red) |
| 82 | + if uefibins.length > 0 |
| 83 | + puts "input contains uefi data but no vbios could be found".colorize(:yellow) |
| 84 | + puts "the vbios might not be baked into the input!".colorize(:yellow) |
| 85 | + end |
69 | 86 | end |
70 | 87 | end |
71 | 88 | end |
|
0 commit comments