Skip to content

Instantly share code, notes, and snippets.

@picatz
Created March 12, 2018 05:12
Show Gist options
  • Save picatz/9d200cd04eeacd53c9880bbd5e9a94aa to your computer and use it in GitHub Desktop.
Save picatz/9d200cd04eeacd53c9880bbd5e9a94aa to your computer and use it in GitHub Desktop.
require 'slack-ruby-client'
require 'net/http'
require 'logger'
# random logging for later analysis
logger = Logger.new('bot.log')
# public ip address for connecting to
PUBLIC_IP = Net::HTTP.get(URI "https://api.ipify.org").strip
# clear old keys
`echo "" > /game_keys`
User = Struct.new(:id, :logger) do
def has_ssh_key?
logger.info("Checking if " + id + " has an SSH key")
if File.file?(id)
logger.info(id + " had an SSH key")
return true
else
logger.info(id + " did not have an SSH key")
return false
end
end
def generate_ssh_key
logger.info("Generated SSH key for " + id)
`ssh-keygen -f #{id} -t rsa -b 4096 -N ''`
open('/game_keys', 'a') { |f|
f.puts "command=\"sudo docker run -it --rm ubuntu\" #{public_ssh_key.strip}"
}
return true
end
def private_ssh_key
unless has_ssh_key?
generate_ssh_key
end
logger.info("Reading private ssh key for " + id)
IO.binread(id)
end
def public_ssh_key
unless has_ssh_key?
generate_ssh_key
end
logger.info("Reading public ssh key for " + id)
IO.binread(id + ".pub")
end
end
class GameServer
def initialize(logger)
@logger = logger
@users = {}
end
def logger
@logger
end
def users
@users
end
def add_user(id)
if users[id]
logger.info("Unable to add " + id + "to the game")
return false
else
@users[id] = User.new(id, @logger)
logger.info(id + " was added to the game")
end
end
end
game_server = GameServer.new(logger)
Slack.configure do |config|
logger.info("configuring slack bot")
config.token = 'YOUR-TOKEN-GOES-HERE-LOL'
end
client = Slack::RealTime::Client.new
trap("SIGINT") { logger.warn("Exiting application!"); puts "Exiting!" ; exit; }
HELP = "`join` : join the enviroment and get your private ssh key.\n" + "`joined?` : check if you've already joined\n" + "`ip address` : get the public facing ip address of the server to connect to.\n" + "`ssh` : show your private ssh key and an example how to login again.\n" + "`help` : show this help menu again.\n"
client.on :message do |data|
if client.ims[data.channel]
case data.text
when /join\b/i then
unless game_server.users[data.user]
logger.info("Added a new user " + data.user)
game_server.add_user(data.user)
logger.info("Delivering first SSH key upon joinging to " + data.user)
client.message channel: data.channel, text: ":dragon: *Welcome to the Kumo Dōjō!* 雲道場 -- Cloud-based security competition practice enviroment.\n> Use the following :key: *ssh private key* to login to the enviroment:\n ```#{game_server.users[data.user].private_ssh_key}```\n" + "Use your `kumo` public ssh key: `ssh ubuntu@#{PUBLIC_IP} -i kumo`"
else
logger.info(data.user + " tried to rejoin the game")
client.message channel: data.channel, text: "*Whoa!* Looks like you've already told me to `join` the dojo. Maybe you're trying to `ssh` into the dojo?"
end
when /joined?\b/i then
if game_server.users[data.user]
client.message channel: data.channel, text: "Yup! You've already joined the enviroment."
else
client.message channel: data.channel, text: "You haven't tried to `join` the enviroment yet."
end
when /ip address/i then
client.message channel: data.channel, text: "The public ip address you can connect to is `#{PUBLIC_IP}`"
when /ssh/ then
if game_server.users[data.user]
logger.info("Delivering SSH key to " + data.user)
client.message channel: data.channel, text: "```#{game_server.users[data.user].private_ssh_key}```"
client.message channel: data.channel, text: "Use your `kumo` public key: `ssh ubuntu@#{PUBLIC_IP} -i kumo`"
else
logger.info(data.user + " tried to get an ssh key without joining the game")
client.message channel: data.channel, text: "*Whoa!* Looks like you've haven't tried to `join` the dojo."
end
when /help/ then
client.message channel: data.channel, text: HELP
else
client.message channel: data.channel, text: HELP
end
else
# ok
end
end
client.start!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment