Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/tools/rest_api_examples/autorun
1140 views
#!/usr/bin/env ruby
# browser-details - Example BeEF RESTful API script
# Retrieves all Autorun rules, adds a rule, runs it on all online browsers, then deletes it
# Refer to the wiki for info: https://github.com/beefproject/beef/wiki/BeEF-RESTful-API
##
require 'rest-client'
require 'json'
require 'optparse'
require 'pp'
require './lib/string' # colored strings
require './lib/print'  # print wrappers
require './lib/beef_rest_api'

if ARGV.length == 0
  puts "#{$0}:"
  puts "|  Example BeEF RESTful API script"
  puts "|  Use --help for help"
  puts "|_ Use verbose mode (-v) and debug mode (-d) for more output"
  exit 1
end

# API config
proto = 'http'
host = '127.0.0.1'
port = '3000'
user = 'beef'
pass = 'beef'

# Command line options
@debug = false
@verbose = false
OptionParser.new do |opts|
  opts.on('-h', '--help', 'Shows this help screen') do
    puts opts
    exit 1
  end
  opts.on('--host HOST', "Set BeEF host (default: #{host})") do |h|
    host = h
  end
  opts.on('--port PORT', "Set BeEF port (default: #{port})") do |p|
    port = p
  end
  opts.on('--user USERNAME', "Set BeEF username (default: #{user})") do |u|
    user = u
  end
  opts.on('--pass PASSWORD', "Set BeEF password (default: #{pass})") do |p|
    pass = p
  end
  opts.on('--ssl', 'Use HTTPS') do
    proto = 'https'
  end
  opts.on('-v', '--verbose', 'Enable verbose output') do
    @verbose = true
  end
  opts.on('-d', '--debug', 'Enable debug output') do
    @debug = true
  end
end.parse!

@api = BeefRestAPI.new proto, host, port, user, pass

# Retrieve the RESTful API token
print_status "Authenticating to: #{proto}://#{host}:#{port}"
@api.auth

# Retrieve BeEF version
@api.version

print_status("Retrieving Autorun rules")
rules = @api.autorun_rules
print_debug(rules)

print_status("Adding a rule")

res = @api.autorun_add_rule({
  "name": "Say Hello",
  "author": "REST API",
  "modules": [
    {
      "name": "alert_dialog",
      "options": {
        "text":"Hello from REST API"
      }
    }
  ],
  "execution_order": [0],
  "execution_delay": [0]
})

print_debug(res)

rule_id = res['rule_id']

unless rule_id.nil?
  print_status "Running rule #{rule_id} on all browsers"
  res = @api.autorun_run_rule_on_all_browsers(rule_id)
  print_debug(res)

  print_status("Deleting rule #{rule_id}")
  res = @api.autorun_delete_rule(rule_id)
  print_debug(res)
end