Created
August 23, 2013 01:57
-
-
Save StephanieSunshine/6314789 to your computer and use it in GitHub Desktop.
The Neurobots controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'base32' | |
require 'sinatra' | |
require 'mysql' | |
require 'json' | |
require 'ap' | |
require 'sys/proctable' | |
require 'turntabler' | |
include Sys | |
PHK = 'LJaEToVra6qL7rIEgHZdecDjcRXNN27dk1T3ccyNKQkMFyHCGVBFHU3uDjelM' | |
PREFIX = '/controller' | |
THREAD = `ps -aef | grep #{Process.pid}| awk '{print $11;}'`.scan(/\d/).first.to_i | |
# Classes | |
class App | |
attr_accessor :pid, :type, :id, :mc, :port, :botlist | |
def initialize(pid, type, id, mc) | |
self.pid = pid | |
self.type = type | |
self.id = id | |
self.mc = mc | |
self.botlist = Hash.new | |
end | |
end | |
# Functions | |
# Prove the key is good | |
def valid_key(id,magickey) | |
db = Mysql::new("localhost", ENV['DBUSER'], ENV['DBPASS'], "neurobots") | |
db.query("select id from users where magic_key='#{magickey}' AND bot_userid='#{id}'").each do |row| | |
return true | |
end | |
return false | |
end | |
def get_port(id) | |
key = "" | |
db = Mysql::new("localhost", ENV['DBUSER'], ENV['DBPASS'], "neurobots") | |
db.query("select id from users where bot_userid='#{id}'").each do |row| | |
key = row[0].to_i + 30000 | |
end | |
return key | |
end | |
# Pull the key for the backdoor | |
def get_key(id) | |
key = "" | |
db = Mysql::new("localhost", ENV['DBUSER'], ENV['DBPASS'], "neurobots") | |
db.query("select magic_key from users where bot_userid='#{id}'").each do |row| | |
key = row[0] | |
end | |
return key | |
end | |
# Start bot | |
def start_bot(id,magickey) | |
# make sure the bot isn't running and then start it if it's not | |
found = false | |
get_ps.each { |ps| found = true if ps.id == id } | |
if(!found) | |
`export BOTPORT='#{get_port(id)}'; export MAGICKEY='#{magickey}'; export BOTUSERID='#{id}'; cd ~/bot/current; nohup ./websocketProxy.rb #{id} > /dev/null &` | |
return "1" | |
end | |
return "0" | |
end | |
# Stop bot | |
def stop_bot(id,magickey) | |
found = false | |
get_ps.each do |ps| | |
found = true if ps.id == id | |
Process.kill('SIGKILL', ps.pid) if ps.id == id | |
end | |
return "1" if found | |
return "0" | |
end | |
# Status | |
def c_status(id,magickey) | |
puts "c_status started #{id} #{magickey}" | |
found = false | |
get_ps.each do |ps| | |
found = true if ps.id == id | |
end | |
return "1" if found | |
return "0" | |
end | |
# Get usable process list | |
def get_ps | |
ps_list = [] | |
ProcTable.ps do |process| | |
#output += PP.pp(process,"") if process.comm.(/ruby/) | |
if process.comm.match(/ruby/) | |
type = '' | |
type = "ws" if process.cmdline.match(/websocketProxy.rb/) | |
type = "bc" if process.cmdline.match(/main.rb/) | |
# "#{pid} #{type} #{botid} #{magic}\n" if type != "" | |
ps_list.push(App.new(process.pid, type, process.environ['BOTUSERID'], process.environ['MAGICKEY'] )) if type != '' | |
end | |
end | |
return ps_list | |
end | |
# Lookups | |
class MyApp < Sinatra::Base | |
attr_accessor :botlist | |
get "#{PREFIX}/" do | |
"Controller #{THREAD} Online" | |
end | |
# Backdoor Start | |
get "#{PREFIX}/#{PHK}/start/:id" do |id| | |
"Backdoor start called with id #{id} magic key: #{get_key(id)}" | |
return start_bot(id,get_key(id)) | |
end | |
# Backdoor Stop | |
get "#{PREFIX}/#{PHK}/stop/:id" do |id| | |
"Backdoor stop called with id #{id}" | |
return stop_bot(id,get_key(id)) | |
end | |
# Backdoor Status | |
get "#{PREFIX}/#{PHK}/status/:id" do |id| | |
"Backdoor status called with id #{id}" | |
return c_status(id,get_key(id)) | |
end | |
# Backdoor Console | |
get "#{PREFIX}/#{PHK}/console" do |id| | |
# "Backdoor status called with id #{id}" | |
# return c_status(id,get_key(id)) | |
output = "" | |
get_ps.each do |bot| | |
if bot.type = "bc" | |
output += "#{bot.id} Stop<br />" | |
end | |
end | |
return output | |
end | |
# Global stats | |
get "#{PREFIX}/status" do | |
output = "Controller stats\n" | |
build_for_json = [] | |
get_ps.each do |process| | |
build_for_json.push([THREAD, process.pid, process.id, get_port(process.id)]) if process.type == "bc" | |
end | |
return JSON.dump(build_for_json) | |
end | |
# Start | |
get "#{PREFIX}/start/:hash" do |hash| | |
id, key = JSON.parse(Base32.decode(hash)) | |
"Start called with id #{id} and hash of #{key}" | |
return start_bot(id,key) if valid_key(id,key) | |
return 0 | |
end | |
# Stop | |
get "#{PREFIX}/stop/:hash" do |hash| | |
id, key = JSON.parse(Base32.decode(hash)) | |
"Stop called with id #{id} and hash of #{key}" | |
return stop_bot(id,key) if valid_key(id,key) | |
return 0 | |
end | |
# Status | |
get "#{PREFIX}/status/:hash" do |hash| | |
id, key = JSON.parse(Base32.decode(hash)) | |
"Status called with id #{id} and hash of #{key}" | |
return c_status(id,key) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment