From db5e26744e9f1d49e6157ed5a862b9729a30f848 Mon Sep 17 00:00:00 2001 From: James Lee Date: Tue, 10 Feb 2015 14:55:25 -0600 Subject: [PATCH 1/3] Add some better docs in readme --- README.md | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 41ae7a1d3..bff271ee1 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,8 @@ It supports authentication via NTLM using the [ruby ntlm gem](https://rubygems.o ## Installation +This gem has not yet been released, but when it is, do this: + Add this line to your application's Gemfile: ```ruby @@ -26,14 +28,34 @@ Or install it yourself as: ## Usage +### Using the `Client` class + +```ruby +sock = TCPSocket.new("192.168.100.140", 445) +c = Smb2::Client.new( + socket: sock, + username:"administrator", + password:"P@ssword1", + domain:"asdfasdf" +) +c.negotiate +c.authenticate +``` + +### Making packets manually + ```ruby sock = TCPSocket.new("192.168.100.140", 445) neg = Smb2::Packet::NegotiateRequest.new( - dialects: "\x02\x02".b, + # This is necessary until I can figure out how to set a default for + # `rest` fields + dialects: "\x02\x02".force_encoding("binary"), ) nbss = [neg.length].pack("N") sock.write(nbss + neg.to_s) -data = sock.read(36) +# Grab NBSS size +size = sock.read(4).unpack("N").first +data = sock.read(size) neg_response = Smb2::Packet::NegotiateResponse.new(data) ``` From c636485346bdc36210d4ecb9603d0a4c32b27fc1 Mon Sep 17 00:00:00 2001 From: James Lee Date: Tue, 10 Feb 2015 15:55:40 -0600 Subject: [PATCH 2/3] Cargo cult semver stuff from metasploit-credential --- lib/smb2/version.rb | 43 ++++++++++- smb2.gemspec | 3 +- spec/lib/smb2/version_spec.rb | 139 ++++++++++++++++++++++++++++++++++ 3 files changed, 182 insertions(+), 3 deletions(-) create mode 100644 spec/lib/smb2/version_spec.rb diff --git a/lib/smb2/version.rb b/lib/smb2/version.rb index 3c3b81a39..5d42f8aea 100644 --- a/lib/smb2/version.rb +++ b/lib/smb2/version.rb @@ -1,3 +1,44 @@ module Smb2 - VERSION = "0.0.1" + + # Holds components of {VERSION} as defined by {http://semver.org/spec/v2.0.0.html semantic versioning v2.0.0}. + module Version + # The major version number. + MAJOR = 0 + # The minor version number, scoped to the {MAJOR} version number. + MINOR = 0 + # The patch number, scoped to the {MINOR} version number. + PATCH = 1 + + PRERELEASE = 'semver' + + # The full version string, including the {MAJOR}, {MINOR}, {PATCH}, and optionally, the {PRERELEASE} in the + # {http://semver.org/spec/v2.0.0.html semantic versioning v2.0.0} format. + # + # @return [String] '{MAJOR}.{MINOR}.{PATCH}' on master. '{MAJOR}.{MINOR}.{PATCH}-{PRERELEASE}' on any branch + # other than master. + def self.full + version = "#{MAJOR}.#{MINOR}.#{PATCH}" + + if defined? PRERELEASE + version = "#{version}-#{PRERELEASE}" + end + + version + end + + # The full gem version string, including the {MAJOR}, {MINOR}, {PATCH}, and optionally, the {PRERELEASE} in the + # {http://guides.rubygems.org/specification-reference/#version RubyGems versioning} format. + # + # @return [String] '{MAJOR}.{MINOR}.{PATCH}' on master. '{MAJOR}.{MINOR}.{PATCH}.{PRERELEASE}' on any branch + # other than master. + def self.gem + full.gsub('-', '.pre.') + end + end + + # @see Version.gem + GEM_VERSION = Version.gem + + # @see Version.full + VERSION = Version.full end diff --git a/smb2.gemspec b/smb2.gemspec index 07c4d87bb..d8df0b545 100644 --- a/smb2.gemspec +++ b/smb2.gemspec @@ -5,7 +5,7 @@ require 'smb2/version' Gem::Specification.new do |spec| spec.name = "smb2" - spec.version = Smb2::VERSION + spec.version = Smb2::GEM_VERSION spec.authors = ["James Lee"] spec.email = ["egypt@metasploit.com"] spec.summary = %q{A message creator and parser for the SMB2 protocol} @@ -28,7 +28,6 @@ Gem::Specification.new do |spec| spec.add_development_dependency "bundler", "~> 1.7" spec.add_development_dependency "fivemat" - spec.add_development_dependency "mocksocket" spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rspec", "~> 3.0" spec.add_development_dependency "simplecov" diff --git a/spec/lib/smb2/version_spec.rb b/spec/lib/smb2/version_spec.rb new file mode 100644 index 000000000..e384b3290 --- /dev/null +++ b/spec/lib/smb2/version_spec.rb @@ -0,0 +1,139 @@ +describe Smb2::Version do + context 'CONSTANTS' do + context 'MAJOR' do + subject(:major) do + described_class::MAJOR + end + + it 'is 0 because the API is not locked yet' do + expect(major).to eq(0) + end + end + + context 'MINOR' do + subject(:minor) do + described_class::MINOR + end + + it { should be_a Integer } + end + + context 'PATCH' do + subject(:patch) do + described_class::PATCH + end + + it { should be_a Integer } + end + + pull_request = ENV['TRAVIS_PULL_REQUEST'] + + # a pull request cannot check PRERELEASE because it will be tested in the target branch, but the source itself + # is from the source branch and so has the source branch PRERELEASE. + # + # PRERELEASE can only be set appropriately for a merge by merging to the target branch and then updating PRERELEASE + # on the target branch before committing and/or pushing to github and travis-ci. + if pull_request.nil? || pull_request == 'false' + context 'PREPRELEASE' do + subject(:prerelease) do + described_class::PRERELEASE + end + + branch = ENV['TRAVIS_BRANCH'] + + if branch.nil? || branch.empty? + branch = `git rev-parse --abbrev-ref HEAD`.strip + end + + if branch == 'master' + it 'does not have a PRERELEASE' do + expect(defined? described_class::PRERELEASE).to be_nil + end + else + branch_regex = %r{\A(?:refs/remotes/)?(?bug|chore|feature|staging)(/(?[^/]+))?/(?[^\/]+)\z} + match = branch.match(branch_regex) + + if match + it 'matches the branch relative name' do + expect(prerelease).to eq(match[:prerelease]) + end + else + tag_regex = /\Av(?\d+).(?\d+).(?\d+)(\.pre\.(?.*))?\z/ + # travis-ci sets TRAVIS_BRANCH to the tag name for tag builds + match = branch.match(tag_regex) + + if match + tag_prerelease = match[:prerelease] + + if tag_prerelease + it 'matches the tag prerelease converted from a gem version to a VERSION' do + expect(prerelease).to eq(tag_prerelease.gsub('.pre.', '-')) + end + else + it 'does not have a PRERELEASE' do + expect(defined? described_class::PRERELEASE).to be_nil + end + end + else + it 'has a abbreviated reference that can be parsed for prerelease' do + fail "Do not know how to parse #{branch.inspect} for PRERELEASE" + end + end + end + end + end + end + end + + context 'full' do + subject(:full) do + described_class.full + end + + # + # lets + # + + let(:major) do + 1 + end + + let(:minor) do + 2 + end + + let(:patch) do + 3 + end + + before(:each) do + stub_const("#{described_class}::MAJOR", major) + stub_const("#{described_class}::MINOR", minor) + stub_const("#{described_class}::PATCH", patch) + end + + context 'with PRERELEASE' do + let(:prerelease) do + 'prerelease' + end + + before(:each) do + stub_const("#{described_class}::PRERELEASE", prerelease) + end + + it 'is ..-' do + expect(full).to eq("#{major}.#{minor}.#{patch}-#{prerelease}") + end + end + + context 'without PRERELEASE' do + before(:each) do + hide_const("#{described_class}::PRERELEASE") + end + + it 'is ..' do + expect(full).to eq("#{major}.#{minor}.#{patch}") + end + end + end +end From 2caf3f5825a2af9f1b692aa308990534edbfc792 Mon Sep 17 00:00:00 2001 From: James Lee Date: Thu, 12 Feb 2015 10:20:54 -0600 Subject: [PATCH 3/3] Use metasploit-version instead of copy-pasta --- smb2.gemspec | 1 + spec/lib/smb2/version_spec.rb | 141 +--------------------------------- spec/lib/smb2_spec.rb | 6 ++ spec/spec_helper.rb | 7 ++ 4 files changed, 18 insertions(+), 137 deletions(-) create mode 100644 spec/lib/smb2_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/smb2.gemspec b/smb2.gemspec index d8df0b545..50c163235 100644 --- a/smb2.gemspec +++ b/smb2.gemspec @@ -28,6 +28,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency "bundler", "~> 1.7" spec.add_development_dependency "fivemat" + spec.add_development_dependency "metasploit-version" spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rspec", "~> 3.0" spec.add_development_dependency "simplecov" diff --git a/spec/lib/smb2/version_spec.rb b/spec/lib/smb2/version_spec.rb index e384b3290..6ae3f1ea3 100644 --- a/spec/lib/smb2/version_spec.rb +++ b/spec/lib/smb2/version_spec.rb @@ -1,139 +1,6 @@ -describe Smb2::Version do - context 'CONSTANTS' do - context 'MAJOR' do - subject(:major) do - described_class::MAJOR - end - - it 'is 0 because the API is not locked yet' do - expect(major).to eq(0) - end - end - - context 'MINOR' do - subject(:minor) do - described_class::MINOR - end - - it { should be_a Integer } - end - - context 'PATCH' do - subject(:patch) do - described_class::PATCH - end - - it { should be_a Integer } - end - - pull_request = ENV['TRAVIS_PULL_REQUEST'] - - # a pull request cannot check PRERELEASE because it will be tested in the target branch, but the source itself - # is from the source branch and so has the source branch PRERELEASE. - # - # PRERELEASE can only be set appropriately for a merge by merging to the target branch and then updating PRERELEASE - # on the target branch before committing and/or pushing to github and travis-ci. - if pull_request.nil? || pull_request == 'false' - context 'PREPRELEASE' do - subject(:prerelease) do - described_class::PRERELEASE - end - - branch = ENV['TRAVIS_BRANCH'] - - if branch.nil? || branch.empty? - branch = `git rev-parse --abbrev-ref HEAD`.strip - end - - if branch == 'master' - it 'does not have a PRERELEASE' do - expect(defined? described_class::PRERELEASE).to be_nil - end - else - branch_regex = %r{\A(?:refs/remotes/)?(?bug|chore|feature|staging)(/(?[^/]+))?/(?[^\/]+)\z} - match = branch.match(branch_regex) - - if match - it 'matches the branch relative name' do - expect(prerelease).to eq(match[:prerelease]) - end - else - tag_regex = /\Av(?\d+).(?\d+).(?\d+)(\.pre\.(?.*))?\z/ - # travis-ci sets TRAVIS_BRANCH to the tag name for tag builds - match = branch.match(tag_regex) - - if match - tag_prerelease = match[:prerelease] +require 'spec_helper' - if tag_prerelease - it 'matches the tag prerelease converted from a gem version to a VERSION' do - expect(prerelease).to eq(tag_prerelease.gsub('.pre.', '-')) - end - else - it 'does not have a PRERELEASE' do - expect(defined? described_class::PRERELEASE).to be_nil - end - end - else - it 'has a abbreviated reference that can be parsed for prerelease' do - fail "Do not know how to parse #{branch.inspect} for PRERELEASE" - end - end - end - end - end - end - end - - context 'full' do - subject(:full) do - described_class.full - end - - # - # lets - # - - let(:major) do - 1 - end - - let(:minor) do - 2 - end - - let(:patch) do - 3 - end - - before(:each) do - stub_const("#{described_class}::MAJOR", major) - stub_const("#{described_class}::MINOR", minor) - stub_const("#{described_class}::PATCH", patch) - end - - context 'with PRERELEASE' do - let(:prerelease) do - 'prerelease' - end - - before(:each) do - stub_const("#{described_class}::PRERELEASE", prerelease) - end - - it 'is ..-' do - expect(full).to eq("#{major}.#{minor}.#{patch}-#{prerelease}") - end - end - - context 'without PRERELEASE' do - before(:each) do - hide_const("#{described_class}::PRERELEASE") - end - - it 'is ..' do - expect(full).to eq("#{major}.#{minor}.#{patch}") - end - end - end +describe Smb2::Version do + it_should_behave_like 'Metasploit::Version Version Module' end + diff --git a/spec/lib/smb2_spec.rb b/spec/lib/smb2_spec.rb new file mode 100644 index 000000000..86eb8a49d --- /dev/null +++ b/spec/lib/smb2_spec.rb @@ -0,0 +1,6 @@ +require 'spec_helper' + +describe Smb2 do + it_should_behave_like 'Metasploit::Version VERSION constant' + it_should_behave_like 'Metasploit::Version GEM_VERSION constant' +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 000000000..a916ea06a --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,7 @@ +# Use find_all_by_name instead of find_by_name as find_all_by_name will return pre-release versions +gem_specification = Gem::Specification.find_all_by_name('metasploit-version').first + +Dir[File.join(gem_specification.gem_dir, 'spec', 'support', '**', '*.rb')].each do |f| + require f +end +