Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/tools/exploit/jsobfu.rb
Views: 11766
#!/usr/bin/env ruby12##3# This module requires Metasploit: https://metasploit.com/download4# Current source: https://github.com/rapid7/metasploit-framework5##6begin7msfbase = __FILE__8while File.symlink?(msfbase)9msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))10end11$:.unshift(File.expand_path(File.join(File.dirname(msfbase), '..', '..', 'lib')))12require 'msfenv'13require 'rex'14require 'optparse'15require 'rex/exploitation/jsobfu'1617module Jsobfu18class OptsConsole19def self.parse(args)20options = {}21parser = OptionParser.new do |opt|22opt.banner = "Usage: #{__FILE__} [options]"23opt.separator ''24opt.separator 'Specific options:'2526opt.on('-t', '--iteration <Integer>', "Number of times to obfuscate the JavaScript") do |v|27options[:iteration] = v28end2930opt.on('-i', '--input <String>', "The JavaScript file you want to obfuscate (default=1)") do |v|31options[:input] = v32end3334opt.on('-o', '--output <String>', "Save the obfuscated file as") do |v|35options[:output] = v36end3738opt.on('-p', '--preserved-identifiers id1,id2', 'The identifiers to preserve') do |v|39options[:preserved_identifiers] = v.split(',')40end4142opt.on_tail('-h', '--help', 'Show this message') do43$stdout.puts opt44exit45end46end4748parser.parse!(args)4950if options.empty?51raise OptionParser::MissingArgument, 'No options set, try -h for usage'52elsif options[:iteration] && options[:iteration] !~ /^\d+$/53raise OptionParser::InvalidOption, "#{options[:format]} is not a number"54elsif !::File.exist?(options[:input].to_s)55raise OptionParser::InvalidOption, "Cannot find: #{options[:input]}"56end5758options[:iteration] = 1 unless options[:iteration]5960options61end62end6364class Driver65def initialize66begin67@opts = OptsConsole.parse(ARGV)68rescue OptionParser::ParseError => e69$stderr.puts "[x] #{e.message}"70exit71end72end7374def run75original_js = read_js(@opts[:input])76js = ::Rex::Exploitation::JSObfu.new(original_js)77obfu_opts = {}78obfu_opts.merge!(iterations: @opts[:iteration].to_i)79obfu_opts.merge!(preserved_identifiers: @opts[:preserved_identifiers] || [])80js.obfuscate(obfu_opts)81js = js.to_s8283output_stream = $stdout84output_stream.binmode85output_stream.write js86$stderr.puts8788if @opts[:output]89save_as(js, @opts[:output])90end91end9293private9495def read_js(path)96js = ::File.open(path, 'rb') { |f| js = f.read }97js98end99100def save_as(js, outfile)101File.open(outfile, 'wb') do |f|102f.write(js)103end104105$stderr.puts106$stderr.puts "File saved as: #{outfile}"107end108109end110end111112113if __FILE__ == $PROGRAM_NAME114driver = Jsobfu::Driver.new115driver.run116end117rescue SignalException => e118puts("Aborted! #{e}")119end120121122