Created
March 3, 2015 03:47
-
-
Save crowell/2c74741e7829e821c9eb to your computer and use it in GitHub Desktop.
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
# Some very simple 'encryption' operations. | |
# Just for some dumb obsfucation. | |
class Cipher | |
def initialize(shuffled) | |
normal = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a + [' '] | |
@map = normal.zip(shuffled).inject(:encrypt => {} , :decrypt => {}) do |hash,(a,b)| | |
hash[:encrypt][a] = b | |
hash[:decrypt][b] = a | |
hash | |
end | |
end | |
def encrypt(str) | |
str.split(//).map { |char| @map[:encrypt][char] }.join | |
end | |
def decrypt(str) | |
str.split(//).map { |char| @map[:decrypt][char] }.join | |
end | |
end |
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
require_relative './trie' | |
require_relative './cipher' | |
t = Trie.new | |
t.push("rbc4pr050n1y", "this isnt the flag") | |
t.push("m4tz i5 my h3r0", "neither is this") | |
t.push("d1dy0u r4d4r3 y0ur ruby?", "nope lots of fake flags") | |
t.push("b3c4u5e 17 uses llvm th15 me4nz sp33d", "wow not this either") | |
t.push("d1d y0u tr13 be1ng m04r 2337?", "good boy this is the flag") | |
t.push("d1d y0u kn0w tr13d n07 b31n6 a n00b?", "lole n1c3 try") | |
t.push("rbx 1z e4zy 2 r34d", "1 c4n r34d th15 ju57 l1k3 x86 0r 3n6L15h!") | |
t.push("d1d y0u h4xd 17 y3t", "stop being such a n00b and get the flag") | |
t.push("python is for noobs", "not the flag but a true statement ;)") | |
m = Marshal.dump(t) | |
File.open('trie.dump', 'w') {|f| f.write(m) } |
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
# a simple prefix tree data structure | |
require './cipher' | |
class Trie | |
def push_recursive(node, string, index, value) | |
char = string[index] | |
node = Node.new(char, value) if node.nil? | |
if (char < node.char) | |
node.left = push_recursive(node.left, string, index, value) | |
elsif (char > node.char) | |
node.right = push_recursive(node.right, string, index, value) | |
elsif (index < string.length - 1) | |
node.mid = push_recursive(node.mid, string, index + 1, value) | |
else | |
node.end = true | |
node.value = value | |
end | |
return node | |
end | |
def get_recursive(node, string, index) | |
return nil if node.nil? | |
char = string[index] | |
if (char < node.char) | |
return get_recursive(node.left, string, index) | |
elsif (char > node.char) | |
return get_recursive(node.right, string, index) | |
elsif (index < string.length - 1) | |
return get_recursive(node.mid, string, index + 1) | |
else | |
if node.last? | |
return [node.char, node.value] | |
else | |
return nil | |
end | |
end | |
end | |
def initialize | |
@root = nil | |
end | |
def push(key, value) | |
cipher = Cipher.new ["K", "D", "w", "X", "H", "3", "e", "1", "S", "B", "g", "a", "y", "v", "I", "6", "u", "W", "C", "0", "9", "b", "z", "T", "A", "q", "U", "4", "O", "o", "E", "N", "r", "n", "m", "d", "k", "x", "P", "t", "R", "s", "J", "L", "f", "h", "Z", "j", "Y", "5", "7", "l", "p", "c", "2", "8", "M", "V", "G", "i", " ", "Q", "F"] | |
key = cipher.encrypt key.to_s | |
return nil if key.empty? | |
@root = push_recursive(@root, key, 0, value) | |
return value | |
end | |
alias_method :[]=, :push | |
def has_key?(key) | |
key = key.to_s | |
return false if key.empty? | |
nope = get_recursive(@root, key, 0).nil? | |
return !nope | |
end | |
def get(key) | |
cipher = Cipher.new ["K", "D", "w", "X", "H", "3", "e", "1", "S", "B", "g", "a", "y", "v", "I", "6", "u", "W", "C", "0", "9", "b", "z", "T", "A", "q", "U", "4", "O", "o", "E", "N", "r", "n", "m", "d", "k", "x", "P", "t", "R", "s", "J", "L", "f", "h", "Z", "j", "Y", "5", "7", "l", "p", "c", "2", "8", "M", "V", "G", "i", " ", "Q", "F"] | |
key = cipher.encrypt key.to_s | |
return nil if key.empty? | |
node = get_recursive(@root, key, 0) | |
if node != nil | |
return node.last | |
else | |
return nil | |
end | |
alias_method :[],:get | |
end | |
class Node | |
attr_accessor :left, :mid, :right, :char, :value, :end | |
def initialize(char, value) | |
@char = char | |
@value = value | |
@left = @mid = @right = nil | |
@end = false | |
end | |
def last? | |
return @end == true | |
end | |
end | |
end |
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
require_relative './trie' | |
require_relative './cipher' | |
puts " [*] Greetings to all of the eiltes here [*]" | |
puts "--- I heard you guys all hate ruby, so I have decided to be nice---" | |
puts " --- enjoy this great bytecode instead! ---" | |
puts "" | |
puts "" | |
print "So, you can tell me now... what's the flag? " | |
flag = gets.chomp | |
dumped = File.read('trie.dump') | |
t = Marshal.load dumped | |
res = t.get(flag) | |
if not res.nil? | |
puts res | |
else | |
puts "nop" | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment