metasploit-framework/tools/exploit/jsobfu.rb

114 lines
2.8 KiB
Ruby
Raw Normal View History

2015-04-03 16:30:23 +00:00
#!/usr/bin/env ruby
msfbase = __FILE__
while File.symlink?(msfbase)
msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
end
2015-10-06 15:30:52 +00:00
$:.unshift(File.expand_path(File.join(File.dirname(msfbase), '..', '..', 'lib')))
2015-04-03 16:30:23 +00:00
require 'msfenv'
require 'rex'
require 'msf/core'
require 'msf/base'
require 'optparse'
module Jsobfu
class OptsConsole
def self.parse(args)
options = {}
parser = OptionParser.new do |opt|
opt.banner = "Usage: #{__FILE__} [options]"
opt.separator ''
opt.separator 'Specific options:'
opt.on('-t', '--iteration <Integer>', "Number of times to obfuscate the JavaScript") do |v|
2015-04-03 16:30:23 +00:00
options[:iteration] = v
end
opt.on('-i', '--input <String>', "The JavaScript file you want to obfuscate (default=1)") do |v|
options[:input] = v
end
opt.on('-o', '--output <String>', "Save the obfuscated file as") do |v|
options[:output] = v
end
2016-01-28 20:36:42 +00:00
opt.on('-p', '--preserved-identifiers id1,id2', 'The identifiers to preserve') do |v|
options[:preserved_identifiers] = v.split(',')
end
2015-04-03 16:30:23 +00:00
opt.on_tail('-h', '--help', 'Show this message') do
$stdout.puts opt
exit
end
end
parser.parse!(args)
if options.empty?
raise OptionParser::MissingArgument, 'No options set, try -h for usage'
elsif options[:iteration] && options[:iteration] !~ /^\d+$/
raise OptionParser::InvalidOption, "#{options[:format]} is not a number"
2016-04-20 12:11:34 +00:00
elsif !::File.exist?(options[:input].to_s)
2015-04-03 16:30:23 +00:00
raise OptionParser::InvalidOption, "Cannot find: #{options[:input]}"
end
options[:iteration] = 1 unless options[:iteration]
options
end
end
class Driver
def initialize
begin
@opts = OptsConsole.parse(ARGV)
rescue OptionParser::ParseError => e
$stderr.puts "[x] #{e.message}"
exit
end
end
def run
original_js = read_js(@opts[:input])
js = ::Rex::Exploitation::JSObfu.new(original_js)
2016-01-28 20:36:42 +00:00
obfu_opts = {}
obfu_opts.merge!(iterations: @opts[:iteration].to_i)
obfu_opts.merge!(preserved_identifiers: @opts[:preserved_identifiers] || [])
js.obfuscate(obfu_opts)
2015-04-03 16:30:23 +00:00
js = js.to_s
output_stream = $stdout
output_stream.binmode
output_stream.write js
$stderr.puts
if @opts[:output]
save_as(js, @opts[:output])
end
end
private
def read_js(path)
js = ::File.open(path, 'rb') { |f| js = f.read }
js
end
def save_as(js, outfile)
File.open(outfile, 'wb') do |f|
f.write(js)
end
$stderr.puts
$stderr.puts "File saved as: #{outfile}"
end
end
end
if __FILE__ == $PROGRAM_NAME
driver = Jsobfu::Driver.new
driver.run
end