From ed99f2bc0124a49ab76928bfe92450e02ff72bb2 Mon Sep 17 00:00:00 2001 From: nixawk Date: Fri, 8 Jan 2016 22:22:00 +0800 Subject: [PATCH 1/3] Fix dns labels/names size limits --- lib/net/dns/names/names.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/net/dns/names/names.rb b/lib/net/dns/names/names.rb index d65c8bf293..60b3831351 100644 --- a/lib/net/dns/names/names.rb +++ b/lib/net/dns/names/names.rb @@ -46,8 +46,8 @@ module Net # :nodoc: end def pack_name(name) - if name.size > 63 - raise ArgumentError, "Label data cannot exceed 63 chars" + if name.size > 255 + raise ArgumentError, "Name data cannot exceed 255 chars" end arr = name.split(".") str = "" From dceb0f5ea928ada56b658f311e23c440b516687c Mon Sep 17 00:00:00 2001 From: nixawk Date: Sat, 9 Jan 2016 11:39:56 +0800 Subject: [PATCH 2/3] check dns labels size limits --- lib/net/dns/names/names.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/net/dns/names/names.rb b/lib/net/dns/names/names.rb index 60b3831351..b483368877 100644 --- a/lib/net/dns/names/names.rb +++ b/lib/net/dns/names/names.rb @@ -52,6 +52,9 @@ module Net # :nodoc: arr = name.split(".") str = "" arr.each do |elem| + if elem.size > 63 + raise ArgumentError, "Label data cannot exceed 63 chars" + end str += [elem.size,elem].pack("Ca*") end str += [0].pack("C") From 46d4b533f3e41992695d00d5d62c40faff261fc2 Mon Sep 17 00:00:00 2001 From: wchen-r7 Date: Thu, 31 Mar 2016 11:29:30 -0500 Subject: [PATCH 3/3] Add rspec for lib/net/dns/names/names.rb --- spec/lib/net/dns/names/names_spec.rb | 129 +++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 spec/lib/net/dns/names/names_spec.rb diff --git a/spec/lib/net/dns/names/names_spec.rb b/spec/lib/net/dns/names/names_spec.rb new file mode 100644 index 0000000000..96ea39a86f --- /dev/null +++ b/spec/lib/net/dns/names/names_spec.rb @@ -0,0 +1,129 @@ +require 'msf/core' + +RSpec.describe Net::DNS::Names do + subject do + obj = Object.new + obj.extend(described_class) + end + + describe '#dn_expand' do + context 'when offset is great than packet length' do + let(:packet) do + 'AAAAA' + end + + let(:offset) do + 10 + end + + it 'raises an ExpandError exception' do + expect { subject.dn_expand(packet, offset) }.to raise_exception(ExpandError) + end + end + + context 'when packet length is less than offset + INT16SZ' do + let(:packet) do + "\xc0" + end + + let(:offset) do + 0 + end + + it 'raises an ExpandError exception' do + expect { subject.dn_expand(packet, offset) }.to raise_exception(ExpandError) + end + end + + context 'when packet length is less than offset + packet length' do + let(:packet) do + 'AAAAA' + end + + let(:offset) do + 4 + end + + it 'raises an ExpandError exception' do + expect { subject.dn_expand(packet, offset) }.to raise_exception(ExpandError) + end + end + end + + describe '#pack_name' do + context 'when name data size is larger than 255 bytes' do + let(:name) do + 'A' * (255+1) + end + + it 'raises an ArgumentError exception' do + expect { subject.pack_name(name) }.to raise_exception(ArgumentError) + end + end + + context 'when label data is larger than 63 bytes' do + let(:name) do + 'A' * (63+1) + '.' + end + + it 'raises an ArgumentError exception' do + expect { subject.pack_name(name) }.to raise_exception(ArgumentError) + end + end + end + + describe '#names_array' do + let(:name) do + "AAA.AAA" + end + + it 'returns an Array' do + expect(subject.names_array(name)).to be_kind_of(Array) + end + end + + describe '#dn_comp' do + let(:name) do + 'AAAA' + end + + let(:offset) do + 0 + end + + let(:compnames) do + {} + end + + it 'returns 3 values' do + v = subject.dn_comp(name, offset, compnames) + expect(v.length).to eq(3) + expect(v[0]).to be_kind_of(String) + expect(v[1]).to be_kind_of(Fixnum) + expect(v[2]).to be_kind_of(Hash) + end + end + + describe '#valid?' do + context 'when FQDN is valid' do + let(:fqdn) do + 'example.com' + end + + it 'returns the FQDN' do + expect(subject.valid?(fqdn)).to eq(fqdn) + end + + end + + context 'when FQDN is not valid' do + let(:fqdn) do + 'INVALID' + end + + it 'raises ArgumentError exception' do + expect { subject.valid?(fqdn) }.to raise_exception(ArgumentError) + end + end + end +end \ No newline at end of file