forked from iagox86/poracle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLocalTestModule.rb
69 lines (56 loc) · 1.55 KB
/
LocalTestModule.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
## LocaltestModule.rb
# Created: December 10, 2012
# By: Ron Bowes
#
# A very simple application that's vulnerable to a padding oracle attack. It's
# initialized with data and a mode, and the decrypt() function will try to
# decrypt the given ciphertext with the given key.
##
require 'openssl'
class LocalTestModule
attr_reader :iv, :ciphertext, :blocksize
NAME = "LocalTestModule(tm)"
def initialize(mode, data, key = nil, iv = nil, verbose = false, delay = 0)
# Save these variables
@mode = mode
@verbose = verbose
@delay = delay
# Create the cipher
c = OpenSSL::Cipher::Cipher.new(mode)
# Set up the required variables
@blocksize = c.block_size
@key = key.nil? ? (1..c.key_len).map{rand(255).chr}.join : key
@iv = iv.nil? ? (1..c.iv_len).map{rand(255).chr}.join : iv
# Set up the cipher
c.encrypt
c.key = @key
c.iv = @iv
@ciphertext = c.update(data) + c.final
if(verbose)
puts()
puts("-" * 80)
puts("Generated test data: #{data} (#{data.unpack("H*")})")
puts("-" * 80)
puts("mode: #{mode}")
puts("key: #{@key.unpack("H*")}")
puts("iv: #{@iv.unpack("H*")}")
puts("enc: #{@ciphertext.unpack("H*")}")
puts("-" * 80)
end
end
def attempt_decrypt(ciphertext)
begin
if(@delay > 0)
sleep(@delay)
end
c = OpenSSL::Cipher::Cipher.new(@mode)
c.decrypt
c.key = @key
c.update(ciphertext)
c.final()
return true
rescue OpenSSL::Cipher::CipherError
return false
end
end
end