Skip to content

Commit

Permalink
Added String#defang (closes #549).
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Nov 25, 2024
1 parent 0e4ec3b commit 4d8add0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/ronin/support/network/defang/core_ext/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@

class String

#
# 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
# "https://www.example.com:8080/foo?q=1".defang
# # => "hxxps[://]www[.]example[.]com[:]8080/foo?q=1"
# "192.168.1.1".defang
# # => "192[.]168[.]1[.]1"
# "www.example.com".defang
# # => "www[.]example[.]com"
#
def defang
Ronin::Support::Network::Defang.defang(self)
end

#
# Refangs a defanged URL, IP address, or host name.
#
Expand Down
42 changes: 42 additions & 0 deletions spec/network/defang/core_ext/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,48 @@
require 'ronin/support/network/defang/core_ext/string'

describe String do
describe "#defang" do
context "when given a defanged URL" do
subject { '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).to eq(defanged)
end
end

context "when given a defanged IPv4 address" do
subject { '192.168.1.1' }

let(:defanged) { '192[.]168[.]1[.]1' }

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

context "when given a defanged IPv6 address" do
subject { '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).to eq(defanged)
end
end

context "when given a defanged host name" do
subject { 'www.example.com' }

let(:defanged) { 'www[.]example[.]com' }

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

describe "#refang" do
context "when the String is a defanged URL" do
subject { 'hxxp[://]www[.]example[.]com/foo?q=1' }
Expand Down

0 comments on commit 4d8add0

Please sign in to comment.