Skip to content

Commit

Permalink
Support more output formats
Browse files Browse the repository at this point in the history
  • Loading branch information
KINGSABRI committed Feb 20, 2013
1 parent df1d558 commit 74a72a9
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 94 deletions.
22 changes: 16 additions & 6 deletions bofk-cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
GEMS = "#{APP_ROOT}/gems"
BIN = "#{APP_ROOT}/bin"
OUT = "#{APP_ROOT}/out"
VERSION = "0.0.4"
VERSION = "0.0.5"

require "#{GEMS}/colorize-0.5.8/lib/colorize.rb"
require 'pattern'
Expand Down Expand Up @@ -54,6 +54,10 @@
opts.on('-x', '--bin2hex BINARY_FILE', "Convert binary shellcode to Hex string.") do |bin2hex|
options[:bin2hex] = bin2hex
end
#--> bin to Hex: Format type
opts.on('-t', '--type TYPE', "Only used with 'bin2hex'. Types: ruby, perl, python, c.") do |type|
options[:type] = type
end
#--> Version
opts.on('-v', '--version', 'Display Buffer Overflow Kit version.') do |v|
options[:version] = v
Expand Down Expand Up @@ -97,7 +101,7 @@
then
decor = decoration.decorate("Pattern create")
puts "#{decor[:head]}".light_blue + "#{decor[:title]}".white + "#{decor[:tail]}".light_blue
puts mark[:+] + "Size: #{@pattern.create(options[:create]).size}\n".white.underline
puts mark[:+] + "Size:".white.underline + " #{@pattern.create(options[:create]).size}\n".white
puts "#{@pattern.create(options[:create])}".light_cyan
puts "#{decor[:end]}".light_blue
puts ""
Expand All @@ -108,8 +112,8 @@
offset = @pattern.offset(options[:offset], options[:pattern_length])
decor = decoration.decorate("Pattern offset")
puts "#{decor[:head]}".light_blue + "#{decor[:title]}".white + "#{decor[:tail]}".light_blue
puts mark[:+] + "Actual pattern length: #{offset[:length]} chars.".white.underline
puts mark[:+] + "Matches: #{offset[:offset].size} times.\n".white.underline
puts mark[:+] + "Actual pattern length:".white.underline + " #{offset[:length]} chars.".white
puts mark[:+] + "Matches:".white.underline + " #{offset[:offset].size} times.\n".white

offset[:offset].each {|o| puts "#{o}".light_cyan}

Expand Down Expand Up @@ -145,8 +149,14 @@
decor = decoration.decorate("Binary to Hex")
puts "#{decor[:head]}".light_blue + "#{decor[:title]}".white + "#{decor[:tail]}".light_blue
@bin2hex.read(options[:bin2hex])
puts mark[:+] + "File Size: #{File.size(options[:bin2hex])} byte.\n".white.underline
puts "#{@bin2hex.to_hex}".light_cyan
puts mark[:+] + "File Size:".white.underline + " #{File.size(options[:bin2hex])} bytes.".white
if options[:type] == nil
type = "No format specified."
else
type = options[:type]
end
puts mark[:+] + "Format type:".white.underline + " #{type} \n".white
puts "#{@bin2hex.to_hex(options[:type])}".light_cyan
puts "#{decor[:end]}".light_blue
puts ""

Expand Down
43 changes: 32 additions & 11 deletions lib/bin2hex.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,64 @@

require 'utils'

class Bin2Hex

def initialize
@file2read = ""
@hex = ""
@hex = []

end

def read(file)
reader = File.open("#{file}" , 'rb')
@file2read = reader.read.each_byte.map { |b| "%02x" % b } # b.to_s(16).rjust(2, '0')

return @file2read
return @file2read # array
end

def to_hex
hex_ary = @file2read.join.scan(/.{2}/)
hex_ary.map do |byte|
@hex << '\x' + byte
end

return @hex
end
def to_hex(format)

end
hex_ary = @file2read

# Default without type option
if format == nil

# TODO add file checker
hex_ary.map do |byte|
@hex << '\x' + byte
end

@hex = @hex.join

else

hex_ary = @file2read

type = BofKUtils::Decoration.new

hex_ary.map do |byte|
@hex << '\x' + byte
end

formatted = type.format(@hex , format , 15)
holder = ""

formatted.map do |l|
if l != formatted.last
holder << "\"#{l[0..-2].join}\"" + l.last
else
holder << "\"#{l.join}\""
end
end

@hex = holder

end

return @hex
end

end



Expand Down
221 changes: 144 additions & 77 deletions lib/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,87 +4,154 @@

class BofKUtils

class FilesFolders

def initialize
@mark = BofKUtils::Decoration.new
end

def file_exist?(file)
case
when File.directory?(file) == true
puts @mark[:!] + "Error!:" + " #{file} : Please mention file not Directory!!"
exit
when File.exist?(file) == true
return true
when File.exist?(file) == false
puts @mark[:!] + "Error!:" + " #{file} : File not found!"
exit
end
end

end # FilesFolders

class Decoration

attr_reader :terminal_size , :mark

def terminal_size
`stty size`.scan(/\d+/).map { |s| s.to_i }.reverse
rescue
puts "I can't detect you commandline environment! - plz contact rise issue: https://github.com/KINGSABRI/BufferOverflow-Kit"
end

def decorate(title = "")

if title.size + 7 > terminal_size[0]
puts "\n\nTerminal Size is too small, plz resize your terminal's window!" +
"plz contact rise issue: https://github.com/KINGSABRI/BufferOverflow-Kit\n\n"
exit
end
twidth = terminal_size[0]
title_head = "---[ "
title_tail = " ]"
title_tail << "-" * (twidth - title_head.size - title.size - title_tail.size)
tail = "-" * twidth

decor = {:head => title_head , :title => title , :tail => title_tail , :end => tail}

return decor
end

def mark
mark = { :+ => "[+] ".green , :- => "[-] " , :! => "[!] ".yellow }
end

def format(array = [], type = "" , line_length = 15) # takes array , output type , line length

if type == nil
type = "ruby"
else
type.downcase!
end

liner = []

array.each_slice(line_length) do |line|
liner << line
end

case
when type == "ruby"
liner = liner.map do |i|
if i != liner.last
i << " +\n"
else
i
end
end
when type == "perl"
liner = liner.map do |i|
if i != liner.last
i << " .\n"
else
i
end
end
when type == "python"
liner = liner.map do |i|
if i != liner.last
i << " \n"
else
i
end
end
when type == "c"
liner = liner.map do |i|
if i != liner.last
i << " \n"
else
i
end
end

else # TODO should rise an error unknown format [Ruby default]

puts "#{mark[:!]}" + "Error: Unknown #{type} type."
puts "Default type (ruby) will be used. \n\r"
sleep 2

liner = liner.map do |i|
if i != liner.last
i << " +\n"
else
i #= i
end
end
end

return liner

end

class FilesFolders

def initialize
@mark = Decoration.new.mark
end

def file_exist?(file)
case
when File.directory?(file) == true
puts @mark[:!] + "Error!:" + " #{file} : Please mention file not Directory!!"
exit
when File.exist?(file) == true
return true
when File.exist?(file) == false
puts @mark[:!] + "Error!:" + " #{file} : File not found!"
exit
end
end

end # FilesFolders

class Decoration

attr_reader :terminal_size , :mark

def terminal_size
`stty size`.scan(/\d+/).map { |s| s.to_i }.reverse
rescue
puts "I can't detect you commandline environment! - plz contact rise issue: https://github.com/KINGSABRI/BufferOverflow-Kit"
end

def decorate(title = "")

if title.size + 7 > terminal_size[0]
puts "\n\nTerminal Size is too small, plz resize your terminal's window!" +
"plz contact rise issue: https://github.com/KINGSABRI/BufferOverflow-Kit\n\n"
exit
class Decode

# Sanitization
def sanitize (string)
case
when string.include?('0x') #== false
string = string.gsub(/0x/, "") # Sanitize "0x77d6b141" format
when string.include?("\\x")
string = string.gsub(/[\\x]/, "") # Sanitize "\x77\xd6\xb1\x41" format
when string.include?('x')
then
string = string.gsub(/[\\x]/, "") # Sanitize "\x77\xd6\xb1\x41" format
else
return string
end
end

def to_hex(ascii)
ascii.unpack('H*')[0]
end

def to_ascii(hex)
hex_sanitized = sanitize(hex)
[hex_sanitized].pack('H*')
end
end
twidth = terminal_size[0]
title_head = "---[ "
title_tail = " ]"
title_tail << "-" * (twidth - title_head.size - title.size - title_tail.size)
tail = "-" * twidth

decor = {:head => title_head , :title => title , :tail => title_tail , :end => tail}

return decor
end

def mark
mark = { :+ => "[+] ".green , :- => "[-] " , :! => "[!] ".yellow }
end

end

class Decode

# Sanitization
def sanitize (string)
case
when string.include?('0x') #== false
string = string.gsub(/0x/, "") # Sanitize "0x77d6b141" format
when string.include?("\\x")
string = string.gsub(/[\\x]/, "") # Sanitize "\x77\xd6\xb1\x41" format
when string.include?('x')
then
string = string.gsub(/[\\x]/, "") # Sanitize "\x77\xd6\xb1\x41" format
else
return string
end
end

def to_hex(ascii)
ascii.unpack('H*')[0]
end

def to_ascii(hex)
hex_sanitized = sanitize(hex)
[hex_sanitized].pack('H*')
end
end
end

0 comments on commit 74a72a9

Please sign in to comment.