File tree Expand file tree Collapse file tree 2 files changed +66
-0
lines changed
lib/ronin/support/network Expand file tree Collapse file tree 2 files changed +66
-0
lines changed Original file line number Diff line number Diff line change 16
16
# along with ronin-support. If not, see <https://www.gnu.org/licenses/>.
17
17
#
18
18
19
+ require_relative 'url'
20
+ require_relative 'ip'
21
+
19
22
module Ronin
20
23
module Support
21
24
module Network
@@ -159,6 +162,31 @@ def self.refang_url(url)
159
162
end
160
163
end
161
164
165
+ #
166
+ # Defangs a URL, IP address, or host name.
167
+ #
168
+ # @param [String] string
169
+ # The URL, IP address, or host name.
170
+ #
171
+ # @return [String]
172
+ # The defanged URL, IP address, or host name.
173
+ #
174
+ # @example
175
+ # Defang.defang("https://www.example.com:8080/foo?q=1")
176
+ # # => "hxxps[://]www[.]example[.]com[:]8080/foo?q=1"
177
+ # Defang.defang("192.168.1.1")
178
+ # # => "192[.]168[.]1[.]1"
179
+ # Defang.defang("www.example.com")
180
+ # # => "www[.]example[.]com"
181
+ #
182
+ def self . defang ( string )
183
+ case string
184
+ when IP ::REGEX then defang_ip ( string )
185
+ when URL ::REGEX then defang_url ( string )
186
+ else defang_host ( string )
187
+ end
188
+ end
189
+
162
190
#
163
191
# Refangs a defanged URL, IP address, or host name.
164
192
#
Original file line number Diff line number Diff line change 190
190
end
191
191
end
192
192
193
+ describe ".defang" do
194
+ context "when given a defanged URL" do
195
+ let ( :url ) { 'http://www.example.com/foo?q=1' }
196
+ let ( :defanged ) { 'hxxp[://]www[.]example[.]com/foo?q=1' }
197
+
198
+ it "must return the defanged URL" do
199
+ expect ( subject . defang ( url ) ) . to eq ( defanged )
200
+ end
201
+ end
202
+
203
+ context "when given a defanged IPv4 address" do
204
+ let ( :ip ) { '192.168.1.1' }
205
+ let ( :defanged ) { '192[.]168[.]1[.]1' }
206
+
207
+ it "must return the defanged IPv4 address" do
208
+ expect ( subject . defang ( ip ) ) . to eq ( defanged )
209
+ end
210
+ end
211
+
212
+ context "when given a defanged IPv6 address" do
213
+ let ( :ip ) { '2606:2800:21f:cb07:6820:80da:af6b:8b2c' }
214
+ let ( :defanged ) { '2606[:]2800[:]21f[:]cb07[:]6820[:]80da[:]af6b[:]8b2c' }
215
+
216
+ it "must return the defanged IPv6 address" do
217
+ expect ( subject . defang ( ip ) ) . to eq ( defanged )
218
+ end
219
+ end
220
+
221
+ context "when given a defanged host name" do
222
+ let ( :host ) { 'www.example.com' }
223
+ let ( :defanged ) { 'www[.]example[.]com' }
224
+
225
+ it "must return the defanged host name" do
226
+ expect ( subject . defang ( host ) ) . to eq ( defanged )
227
+ end
228
+ end
229
+ end
230
+
193
231
describe ".refang" do
194
232
context "when given a defanged URL" do
195
233
let ( :defanged ) { 'hxxp[://]www[.]example[.]com/foo?q=1' }
You can’t perform that action at this time.
0 commit comments