Skip to content

Commit

Permalink
MAINT, FIX: apply rubocop to code, close coderobe#40, coderobe#50
Browse files Browse the repository at this point in the history
* auto-fix formatting where acceptable
* adjust regexps
* fix issues with uppercase names
* correct upx path to output file
  • Loading branch information
vkhodygo committed Oct 29, 2022
1 parent 72f6190 commit 41bb22f
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 166 deletions.
2 changes: 1 addition & 1 deletion _init.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "./src/cli"
require './src/cli'

$0=ARGV.first
$PROGRAM_NAME=$0
Expand Down
32 changes: 17 additions & 15 deletions src/cli.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
require "thor"
require "fileutils"
require "logger"
require "colorize"
require "./src/methods"
require "./src/utils"

#Terrapin::CommandLine.logger = Logger.new(STDOUT)
require 'thor'
require 'fileutils'
require 'logger'
require 'colorize'
require './src/methods'
require './src/utils'

# Terrapin::CommandLine.logger = Logger.new(STDOUT)
module VBiosFinder
@@wd

class CLI < Thor
desc 'extract <bios update file>'.colorize(:blue), 'attempts to extract an embedded vbios from a bios update'
def extract file=nil

def extract(file = nil)
wd = "#{Dir.pwd}/tmp-vbiosfinder"
if file.nil?
puts "no file specified".colorize(:red)
puts 'no file specified'.colorize(:red)
return
end
if File.directory? wd
Expand All @@ -23,17 +24,18 @@ def extract file=nil
end
FileUtils.mkdir_p wd
Kernel.at_exit do
puts "Cleaning up garbage".colorize(:blue)
puts 'Cleaning up garbage'.colorize(:blue)
FileUtils.remove_entry_secure wd
end
@@wd = wd
Dir.chdir wd
puts "output will be stored in '#{wd}'".colorize(":blue")
Utils::installed?("ruby") # "bugfix"
Utils::get_new_files # "bugfix" #2
puts "output will be stored in '#{wd}'".colorize(':blue')
Utils.installed?('ruby') # "bugfix"
Utils.get_new_files # "bugfix" #2
FileUtils.cp(file, wd)
puts 'copying BIOS file to the work directory'.colorize(':blue')
puts
Main::run Utils::get_new_files.first
Main.run Utils.get_new_files.first
end
end
end
33 changes: 15 additions & 18 deletions src/extract-7z.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
require "terrapin"
require 'terrapin'

module VBiosFinder
class Extract
def self.p7zip file
begin
line = Terrapin::CommandLine.new("7z", "x :file")
line.run(file: file)
rescue Terrapin::ExitStatusError => e
puts e.message
return
end
def self.p7zip(file)
line = Terrapin::CommandLine.new('7z', 'x :file')
line.run(file: file)
rescue Terrapin::ExitStatusError => e
puts e.message
nil
end
end

class Test
def self.p7zip file
begin
line = Terrapin::CommandLine.new("7z", "l :file | grep 'Type = 7z'")
line.run(file: file)
true
rescue Terrapin::ExitStatusError => e
false
end
def self.p7zip(file)
line = Terrapin::CommandLine.new('7z', "l :file | grep 'Type = 7z'")
line.run(file: file)
true
rescue Terrapin::ExitStatusError => e
false
end
end
end
end
33 changes: 15 additions & 18 deletions src/extract-innosetup.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
require "terrapin"
require 'terrapin'

module VBiosFinder
class Extract
def self.innosetup file
begin
line = Terrapin::CommandLine.new("innoextract", ":file")
puts line.run(file: file)
rescue Terrapin::ExitStatusError => e
puts e.message
return
end
def self.innosetup(file)
line = Terrapin::CommandLine.new('innoextract', ':file')
puts line.run(file: file)
rescue Terrapin::ExitStatusError => e
puts e.message
nil
end
end

class Test
def self.innosetup file
begin
line = Terrapin::CommandLine.new("innoextract", "-t :file")
line.run(file: file)
true
rescue Terrapin::ExitStatusError => e
false
end
def self.innosetup(file)
line = Terrapin::CommandLine.new('innoextract', '-t :file')
line.run(file: file)
true
rescue Terrapin::ExitStatusError => e
false
end
end
end
end
21 changes: 11 additions & 10 deletions src/extract-polyglot.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
module VBiosFinder
class Extract
def self.polyglot file
File.open file, "r:ASCII-8BIT" do |data|
regex = /(.{4})\xAA\xEE\xAA\x76\x1B\xEC\xBB\x20\xF1\xE6\x51(.{1})/n
def self.polyglot(file)
File.open file, 'r:ASCII-8BIT' do |data|
regex = /(.{4})\xAA\xEE\xAA\x76\x1B\xEC\xBB\x20\xF1\xE6\x51(.)/n
input = data.read
matches = regex.match input
payload_size = matches.captures.first.unpack('V').first
payload_size = matches.captures.first.unpack1('V')
payload_offset = matches.offset(2).last
data.seek payload_offset
File.open "#{file}-polyglot", "w:ASCII-8BIT" do |outdata|
File.open "#{file}-polyglot", 'w:ASCII-8BIT' do |outdata|
outdata.write data.read
end
end
end
end

class Test
def self.polyglot file
File.open file, "r:ASCII-8BIT" do |data|
regex = /(.{4})\xAA\xEE\xAA\x76\x1B\xEC\xBB\x20\xF1\xE6\x51.{1}/n
return !(regex.match(data.read).nil?)
def self.polyglot(file)
File.open file, 'r:ASCII-8BIT' do |data|
regex = /(.{4})\xAA\xEE\xAA\x76\x1B\xEC\xBB\x20\xF1\xE6\x51./n
return !regex.match(data.read).nil?
end
end
end
end
end
35 changes: 16 additions & 19 deletions src/extract-uefi.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
require "terrapin"
require 'terrapin'

module VBiosFinder
class Extract
def self.uefi file
begin
line = Terrapin::CommandLine.new("UEFIExtract", ":file all")
line.run(file: file)
rescue Terrapin::ExitStatusError => e
# TODO: fix Test::uefi before uncommenting this
puts e.message
return
end
def self.uefi(file)
line = Terrapin::CommandLine.new('uefiextract', ':file all')
line.run(file: file)
rescue Terrapin::ExitStatusError => e
# TODO: fix Test::uefi before uncommenting this
puts e.message
nil
end
end

class Test
def self.uefi file
begin
line = Terrapin::CommandLine.new("UEFIExtract", ":file report")
line.run(file: file)
true
rescue Terrapin::ExitStatusError => e
false
end
def self.uefi(file)
line = Terrapin::CommandLine.new('uefiextract', ':file report')
line.run(file: file)
true
rescue Terrapin::ExitStatusError => e
false
end
end
end
end
33 changes: 15 additions & 18 deletions src/extract-upx.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
require "terrapin"
require 'terrapin'

module VBiosFinder
class Extract
def self.upx file
begin
line = Terrapin::CommandLine.new("upx", "-d :file -o :outfile")
line.run(file: file, outfile: "upx-#{file}")
rescue Terrapin::ExitStatusError => e
puts e.message
return
end
def self.upx(file)
line = Terrapin::CommandLine.new('upx', '-d :file -o :outfile')
line.run(file: file, outfile: "upx-#{File.basename(file)}")
rescue Terrapin::ExitStatusError => e
puts e.message
nil
end
end

class Test
def self.upx file
begin
line = Terrapin::CommandLine.new("upx", "-t :file")
line.run(file: file)
true
rescue Terrapin::ExitStatusError => e
false
end
def self.upx(file)
line = Terrapin::CommandLine.new('upx', '-t :file')
line.run(file: file)
true
rescue Terrapin::ExitStatusError => e
false
end
end
end
end
19 changes: 10 additions & 9 deletions src/extract-zlib.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
require "zlib"
require 'zlib'

module VBiosFinder
class Extract
def self.zlib file
def self.zlib(file)
target = "#{file}-zlib"
begin
File.open file, "r:ASCII-8BIT" do |data|
File.open target, "w:ASCII-8BIT" do |outdata|
File.open file, 'r:ASCII-8BIT' do |data|
File.open target, 'w:ASCII-8BIT' do |outdata|
outdata.write Zlib::Inflate.inflate(data.read)
end
end
rescue Zlib::DataError
puts "wrong guess :(".colorize(:red)
puts 'wrong guess :('.colorize(:red)
FileUtils.rm target
end
end
end

class Test
def self.zlib file
File.open file, "r:ASCII-8BIT" do |data|
def self.zlib(file)
File.open file, 'r:ASCII-8BIT' do |data|
regex = /^\x78\x9C/n
return !(regex.match(data.read).nil?)
return !regex.match(data.read).nil?
end
end
end
end
end
10 changes: 5 additions & 5 deletions src/extraction.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require "colorize"
require "./src/utils"
require "./src/methods"
require 'colorize'
require './src/utils'
require './src/methods'

module VBiosFinder
class Extraction
def self.attempt method_s, requires, reason, file
def self.attempt(method_s, requires, reason, file)
if Test.method(method_s).call(file)
puts "found #{requires} archive".colorize(:green)
Extract.method(method_s).call(file)
Expand All @@ -13,4 +13,4 @@ def self.attempt method_s, requires, reason, file
end
end
end
end
end
Loading

0 comments on commit 41bb22f

Please sign in to comment.