diff options
Diffstat (limited to '3.1')
-rwxr-xr-x | 3.1/charon.rb | 61 | ||||
-rwxr-xr-x | 3.1/get_mail.rb | 24 | ||||
-rwxr-xr-x | 3.1/get_ticket.rb | 26 | ||||
-rwxr-xr-x | 3.1/kinit.rb | 34 | ||||
-rwxr-xr-x | 3.1/mail.rb | 29 |
5 files changed, 174 insertions, 0 deletions
diff --git a/3.1/charon.rb b/3.1/charon.rb new file mode 100755 index 0000000..75619b8 --- /dev/null +++ b/3.1/charon.rb @@ -0,0 +1,61 @@ +#!/usr/bin/env ruby + +require 'openssl' +require 'securerandom' +require 'sinatra' + +Users = { + "Athena" => "Passw0rd!" +} + +Services = { + "_TGS" => "eiqu@a5ahs8mooqu9Eng", + "Mail" => "{FvM<kgG}VpHxKJO;6Zo" +} + +def encrypt(obj, key) + cipher = OpenSSL::Cipher::AES.new(256, :CBC).encrypt + cipher.key = Digest::SHA2.digest(key) + s = cipher.update(obj) + cipher.final + s.unpack('H*')[0].upcase +end + +def decrypt(obj, key) + ticket = [obj].pack("H*").unpack("C*").pack("c*") + cipher = OpenSSL::Cipher::AES.new(256, :CBC).decrypt + cipher.key = Digest::SHA2.digest(key) + p = cipher.update(ticket) + cipher.final + p.split(?\0) +end + +def ticket(username, ws_address, service) + encrypt([username, ws_address, service].join(?\0), Services[service]) +end + +def noleak(msg, ul, ws) + puts "Error: #{msg}, returning nonsense to avoid leakage." + ticket(SecureRandom.alphanumeric(ul), ws, SecureRandom.alphanumeric(rand(24) + 8)) +end + +post '/login' do + request.body.rewind + data = JSON.parse(request.body.read) + next "Invalid request\n" unless data.keys == %w(username) + un = data["username"] + ul = un.length + ws = request.ip + next noleak("Invalid username", ul, ws) unless Users.keys.include?(un) + tgt = ticket(un, ws, "_TGS") + encrypt(tgt, Users[un]) +end + +post '/ticket' do + request.body.rewind + data = JSON.parse(request.body.read) + next "Invalid request\n" unless data.keys.sort == %w(service ticket username) + un, ws, sn = decrypt(data["ticket"], Services["_TGS"]) + next "Invalid ticket\n" unless sn == "_TGS" + next "Invalid ticket\n" unless un == data["username"] + next "Invalid ticket\n" unless ws == request.ip + ticket(un, ws, data["service"]) +end diff --git a/3.1/get_mail.rb b/3.1/get_mail.rb new file mode 100755 index 0000000..b212aed --- /dev/null +++ b/3.1/get_mail.rb @@ -0,0 +1,24 @@ +#!/usr/bin/env ruby + +require 'net/http' +require 'uri' +require 'json' + +uri = URI.parse("http://localhost:4568/login") + +header = {'Content-Type': 'text/json'} + +print "Username: "; un = gets.strip +print "Ticket: "; t = gets.strip + +login = { + "username": un, + "ticket": t +} + +http = Net::HTTP.new(uri.host, uri.port) +request = Net::HTTP::Post.new(uri.request_uri, header) +request.body = login.to_json + +response = http.request(request) +puts response.body diff --git a/3.1/get_ticket.rb b/3.1/get_ticket.rb new file mode 100755 index 0000000..d22d843 --- /dev/null +++ b/3.1/get_ticket.rb @@ -0,0 +1,26 @@ +#!/usr/bin/env ruby + +require 'net/http' +require 'uri' +require 'json' + +uri = URI.parse("http://localhost:4567/ticket") + +header = {'Content-Type': 'text/json'} + +print "Username: "; un = gets.strip +print "Ticket: "; t = gets.strip +print "Service: "; s = gets.strip + +login = { + "username": un, + "ticket": t, + "service": s +} + +http = Net::HTTP.new(uri.host, uri.port) +request = Net::HTTP::Post.new(uri.request_uri, header) +request.body = login.to_json + +response = http.request(request) +puts response.body diff --git a/3.1/kinit.rb b/3.1/kinit.rb new file mode 100755 index 0000000..0cfa220 --- /dev/null +++ b/3.1/kinit.rb @@ -0,0 +1,34 @@ +#!/usr/bin/env ruby + +require 'io/console' +require 'json' +require 'net/http' +require 'openssl' +require 'uri' + +uri = URI.parse("http://localhost:4567/login") + +header = {'Content-Type': 'text/json'} + +print "Username: "; un = gets.strip +print "Password: "; pw = STDIN.noecho(&:gets).strip; puts + +login = { + "username": un +} + +http = Net::HTTP.new(uri.host, uri.port) +request = Net::HTTP::Post.new(uri.request_uri, header) +request.body = login.to_json + +response = http.request(request) +eticket = [response.body].pack("H*").unpack("C*").pack("c*") + +cipher = OpenSSL::Cipher::AES.new(256, :CBC).decrypt +cipher.key = Digest::SHA2.digest(pw) +begin + ticket = cipher.update(eticket) + cipher.final + puts ticket + ?\n +rescue OpenSSL::Cipher::CipherError + puts "Invalid password?" +end diff --git a/3.1/mail.rb b/3.1/mail.rb new file mode 100755 index 0000000..e7fa6ff --- /dev/null +++ b/3.1/mail.rb @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby + +require 'openssl' +require 'securerandom' +require 'sinatra' + +set :port, 4568 + +Service = "Mail" +ServicePassword = "{FvM<kgG}VpHxKJO;6Zo" + +def decrypt(ticket) + ticket = [ticket].pack("H*").unpack("C*").pack("c*") + cipher = OpenSSL::Cipher::AES.new(256, :CBC).decrypt + cipher.key = Digest::SHA2.digest(ServicePassword) + p = cipher.update(ticket) + cipher.final + p.split(?\0) +end + +post '/login' do + request.body.rewind + data = JSON.parse(request.body.read) + next "Invalid request\n" unless data.keys.sort == %w(ticket username) + un, ws, sn = decrypt(data["ticket"]) + next "Invalid ticket\n" unless sn == Service + next "Invalid ticket\n" unless un == data["username"] + next "Invalid ticket\n" unless ws == request.ip + "Login okay! You have no mail.\n" +end |