Skip to content

Commit

Permalink
Added Network::Defang::Mixin#defang (closes #548).
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Nov 25, 2024
1 parent 5f7a3e8 commit 0e4ec3b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/ronin/support/network/defang/mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,27 @@ def refang_url(url)
Defang.refang_url(url)
end

#
# Defangs a URL, IP address, or host name.
#
# @param [String] string
# The URL, IP address, or host name.
#
# @return [String]
# The defanged URL, IP address, or host name.
#
# @example
# defang("https://www.example.com:8080/foo?q=1")
# # => "hxxps[://]www[.]example[.]com[:]8080/foo?q=1"
# defang("192.168.1.1")
# # => "192[.]168[.]1[.]1"
# defang("www.example.com")
# # => "www[.]example[.]com"
#
def defang(string)
Defang.defang(string)
end

#
# Refangs a defanged URL, IP address, or host name.
#
Expand Down
38 changes: 38 additions & 0 deletions spec/network/defang/mixin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,44 @@
end
end

describe "#defang" do
context "when given a defanged URL" do
let(:url) { 'http://www.example.com/foo?q=1' }
let(:defanged) { 'hxxp[://]www[.]example[.]com/foo?q=1' }

it "must return the defanged URL" do
expect(subject.defang(url)).to eq(defanged)
end
end

context "when given a defanged IPv4 address" do
let(:ip) { '192.168.1.1' }
let(:defanged) { '192[.]168[.]1[.]1' }

it "must return the defanged IPv4 address" do
expect(subject.defang(ip)).to eq(defanged)
end
end

context "when given a defanged IPv6 address" do
let(:ip) { '2606:2800:21f:cb07:6820:80da:af6b:8b2c' }
let(:defanged) { '2606[:]2800[:]21f[:]cb07[:]6820[:]80da[:]af6b[:]8b2c' }

it "must return the defanged IPv6 address" do
expect(subject.defang(ip)).to eq(defanged)
end
end

context "when given a defanged host name" do
let(:host) { 'www.example.com' }
let(:defanged) { 'www[.]example[.]com' }

it "must return the defanged host name" do
expect(subject.defang(host)).to eq(defanged)
end
end
end

describe "#refang" do
context "when given a defanged URL" do
let(:defanged) { 'hxxp[://]www[.]example[.]com/foo?q=1' }
Expand Down

0 comments on commit 0e4ec3b

Please sign in to comment.