|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +msfbase = __FILE__ |
| 4 | +while File.symlink?(msfbase) |
| 5 | + msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase)) |
| 6 | +end |
| 7 | +$:.unshift(File.expand_path(File.join(File.dirname(msfbase), '..', 'lib'))) |
| 8 | +require 'msfenv' |
| 9 | +require 'rex' |
| 10 | +require 'msf/core' |
| 11 | +require 'msf/base' |
| 12 | +require 'optparse' |
| 13 | + |
| 14 | +module Jsobfu |
| 15 | + class OptsConsole |
| 16 | + def self.parse(args) |
| 17 | + options = {} |
| 18 | + parser = OptionParser.new do |opt| |
| 19 | + opt.banner = "Usage: #{__FILE__} [options]" |
| 20 | + opt.separator '' |
| 21 | + opt.separator 'Specific options:' |
| 22 | + |
| 23 | + opt.on('-t', '--iteration <Fixnum>', "Number of times to obfuscate the JavaScript") do |v| |
| 24 | + options[:iteration] = v |
| 25 | + end |
| 26 | + |
| 27 | + opt.on('-i', '--input <String>', "The JavaScript file you want to obfuscate (default=1)") do |v| |
| 28 | + options[:input] = v |
| 29 | + end |
| 30 | + |
| 31 | + opt.on('-o', '--output <String>', "Save the obfuscated file as") do |v| |
| 32 | + options[:output] = v |
| 33 | + end |
| 34 | + |
| 35 | + opt.on_tail('-h', '--help', 'Show this message') do |
| 36 | + $stdout.puts opt |
| 37 | + exit |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + parser.parse!(args) |
| 42 | + |
| 43 | + if options.empty? |
| 44 | + raise OptionParser::MissingArgument, 'No options set, try -h for usage' |
| 45 | + elsif options[:iteration] && options[:iteration] !~ /^\d+$/ |
| 46 | + raise OptionParser::InvalidOption, "#{options[:format]} is not a number" |
| 47 | + elsif !::File.exists?(options[:input].to_s) |
| 48 | + raise OptionParser::InvalidOption, "Cannot find: #{options[:input]}" |
| 49 | + end |
| 50 | + |
| 51 | + options[:iteration] = 1 unless options[:iteration] |
| 52 | + |
| 53 | + options |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + class Driver |
| 58 | + def initialize |
| 59 | + begin |
| 60 | + @opts = OptsConsole.parse(ARGV) |
| 61 | + rescue OptionParser::ParseError => e |
| 62 | + $stderr.puts "[x] #{e.message}" |
| 63 | + exit |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + def run |
| 68 | + original_js = read_js(@opts[:input]) |
| 69 | + js = ::Rex::Exploitation::JSObfu.new(original_js) |
| 70 | + js.obfuscate(:iterations=>@opts[:iteration].to_i) |
| 71 | + js = js.to_s |
| 72 | + |
| 73 | + output_stream = $stdout |
| 74 | + output_stream.binmode |
| 75 | + output_stream.write js |
| 76 | + $stderr.puts |
| 77 | + |
| 78 | + if @opts[:output] |
| 79 | + save_as(js, @opts[:output]) |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + private |
| 84 | + |
| 85 | + def read_js(path) |
| 86 | + js = ::File.open(path, 'rb') { |f| js = f.read } |
| 87 | + js |
| 88 | + end |
| 89 | + |
| 90 | + def save_as(js, outfile) |
| 91 | + File.open(outfile, 'wb') do |f| |
| 92 | + f.write(js) |
| 93 | + end |
| 94 | + |
| 95 | + $stderr.puts |
| 96 | + $stderr.puts "File saved as: #{outfile}" |
| 97 | + end |
| 98 | + |
| 99 | + end |
| 100 | +end |
| 101 | + |
| 102 | + |
| 103 | +if __FILE__ == $PROGRAM_NAME |
| 104 | + driver = Jsobfu::Driver.new |
| 105 | + driver.run |
| 106 | +end |
0 commit comments