-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfuzzer.rb
203 lines (190 loc) · 6.7 KB
/
fuzzer.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
require 'socket'
require './lib/engine'
$SERVER_IP = '127.0.0.1'
$DTLS_PORT = 20220
module Fuzzer
class Fuzzer
def self.simple_connect
socket = UDPSocket.new
socket.connect($SERVER_IP, $DTLS_PORT)
Engine.do_steps(socket, 7)
puts 'Test: simple_connect passed'
socket.close
end
def self.replay_attack_test
# initialization
socket = UDPSocket.new
socket.connect($SERVER_IP, $DTLS_PORT)
tosend, messages, keys = Engine.do_steps(socket, 6)
# send old msg again
socket.send(messages[2].to_wire, 0)
# check for correct behavior
if select([socket], nil, nil, $timeout) == nil
puts 'Test: replay_attack_test passed. got no answer'
socket.close
return
end
response = Record::RecordHead.parse(socket.recvfrom(200)[0], keys[1], keys[3])
puts "WARNING: Replay Attack successfull. answer: #{response.inspect}"
socket.close
end
def self.finished_wrong_seq
# initialization
socket = UDPSocket.new
socket.connect($SERVER_IP, $DTLS_PORT)
tosend, messages, keys = Engine.do_steps(socket, 4)
# changing values
tosend[0].epoch = 1
tosend[0].sequence_number = 1 + Random.rand((2**48)-1) # <--- should be 0
# continue handshake
socket.send(tosend[0].to_wire(keys[0], keys[2]), 0)
# check for correct behavior
if select([socket], nil, nil, $timeout) == nil
puts 'Test: unencrypted_finished_wrong_seq passed. got no answer'
socket.close
return
end
response = Record::RecordHead.parse(socket.recvfrom(200)[0])
if response.content.class == Record::Alert
puts "Test: unencrypted_finished_wrong_seq passed. got alert: #{response.inspect}"
socket.close
return
end
puts "WARNING: encrypted Finished paket with wrong sequence number " \
"(#{tosend[0].sequence_number}) accepted. answer: #{response.inspect}"
socket.close
end
def self.unencrypted_finished
# initialization
socket = UDPSocket.new
socket.connect($SERVER_IP, $DTLS_PORT)
tosend, messages, keys = Engine.do_steps(socket, 4)
# changing values
tosend[0].epoch = 0
tosend[0].sequence_number = 4
# continue handshake
socket.send(tosend[0].to_wire, 0)
# check for correct behavior
if select([socket], nil, nil, $timeout) == nil
puts 'Test: unencrypted_finished passed. got no answer'
socket.close
return
end
response = Record::RecordHead.parse(socket.recvfrom(200)[0])
if response.content.class != Record::ChangeCipherSpec
puts "Test: unencrypted_finished passed. answer: #{response.inspect}"
socket.close
return
end
puts "WARNING: unencrypted Finished paket accepted. answer: #{response.inspect}"
socket.close
end
def self.wrong_seq
# initialization
socket = UDPSocket.new
socket.connect($SERVER_IP, $DTLS_PORT)
tosend, messages, keys = Engine.do_steps(socket, 5)
# changing values
tosend[0].sequence_number = 2 + Random.rand((2**48)-2) # <--- should be 2
# continue handshake
socket.send(tosend[0].to_wire(keys[0], keys[2]), 0)
# check for correct behavior
if select([socket], nil, nil, $timeout) == nil
puts 'Test: unencrypted_finished passed'
socket.close
return
end
response = Record::RecordHead.parse(socket.recvfrom(200)[0], keys[1], keys[3])
puts "WARNING: encrypted ApplicationData packet with wrong sequence number " \
"(#{tosend[0].sequence_number}) accepted. answer: #{response.inspect}"
socket.close
end
def self.wrong_finished
# initialization
socket = UDPSocket.new
socket.connect($SERVER_IP, $DTLS_PORT)
tosend, messages, keys = Engine.do_steps(socket, 4)
# changing values
tosend[0].content.content.value = 'New_value123'
# continue handshake
socket.send(tosend[0].to_wire(keys[0], keys[2]), 0)
# check for correct behavior
if select([socket], nil, nil, $timeout) == nil
puts 'Test: wrong_finished passed. got no answer'
socket.close
return
end
response = Record::RecordHead.parse(socket.recvfrom(200)[0])
if response.content.class == Record::Alert
puts "Test: wrong_finished passed. got alert: #{response.inspect}"
socket.close
return
end
puts "WARNING: finished packet with wrong finished value accepted. answer: #{response.inspect}"
socket.close
end
# WORK IN PROGRESS FOR FOLLOWING TESTS
def self.cookie_test
200.times do |n|
socket = UDPSocket.new
socket.connect($SERVER_IP, $DTLS_PORT)
tosend, messages, keys = Engine.do_steps(socket, 0)
tosend[0].content.content.cookie = ''
temp = ''
Random.rand(64).times do |n|
temp += Random.rand(256).chr
end
tosend[0].content.content.cookie = temp
tosend[0].content.content.cookie_length += (1 - Random.rand(2))
socket.send(tosend[0].to_wire, 0)
value = select([socket], nil, nil, $timeout)
unless value.nil?
response = Record::RecordHead.parse(socket.recvfrom(200)[0])
puts "WARNING: Cookie Attack successfull. answer: #{response.inspect}" if
response.content.content.class != Handshake::HelloVerifyRequest
end
socket.close
end
return true
end
def self.after_cookie_test # TODO: NOT WORKING
200.times do |n|
socket = UDPSocket.new
socket.connect($SERVER_IP, $DTLS_PORT)
tosend, messages, keys = Engine.do_steps(socket, 2)
tosend[0].content.content.psk_hint_length += (1 - Random.rand(2))
socket.send(tosend[0].to_wire, 0)
value = select([socket], nil, nil, $timeout)
errormessage = 'PSK Hint Length Attack'
fail StandardError, errormessage if value.nil?
response = Record::RecordHead.parse(socket.recvfrom(200)[0])
fail StandardError, errormessage if response.content.class != Record::Alert
socket.close
end
end
def self.server_explode_test # Warning: port collision very possible
200.times do |n|
socket = UDPSocket.new
socket.connect($SERVER_IP, $DTLS_PORT)
Engine.do_steps(socket, 2)
socket.close
end
end
end
end
puts ''
Fuzzer::Fuzzer.simple_connect
puts ''
Fuzzer::Fuzzer.replay_attack_test
puts ''
Fuzzer::Fuzzer.unencrypted_finished
puts ''
Fuzzer::Fuzzer.finished_wrong_seq
puts ''
Fuzzer::Fuzzer.wrong_seq
puts ''
Fuzzer::Fuzzer.wrong_finished
puts ''
# Fuzzer::Fuzzer.cookie_test
# Fuzzer::Fuzzer.after_cookie_test
# Fuzzer::Fuzzer.server_explode_test