$s = Sock.new('crypto.chal.csaw.io', 1003)
$s.recvuntil(':').split("\n")[0]
B = 16
def hexdecode(string)
[string].pack('H*').bytes
end
def get_ciphertext(input)
puts "Trying #{input}"
$s.sendline(input)
$s.recvline
hexdecode($s.recvline.chomp)
end
def duplicate_blocks(ciphertext)
blocks = ciphertext.each_slice(B)
blocks.each_with_index do |block, i|
return i if blocks.select { |b| b == block }.length > 1
end
false
end
def guess_prefix_size
len = 0
glue = 'X' * 32
loop do
i = duplicate_blocks(get_ciphertext('A' * len + glue))
return B * i - len if i
len += 1
end
end
def guess_suffix_size(prefix_size)
input = 'X' * 16
old_len = get_ciphertext(input).length
loop do
input += 'X'
new_len = get_ciphertext(input).length
return new_len - B - prefix_size - input.length if new_len > old_len
end
end