Class: Syspy::TdsPackageStream

Inherits:
Object
  • Object
show all
Defined in:
lib/tds_package_stream.rb

Constant Summary collapse

HEADER_LENGTH =
45
HEADER_REGEXP =
/\d{3}\.\d{3}\.\d{3}\.\d{3}\.\d{5}-\d{3}\.\d{3}\.\d{3}\.\d{3}\.\d{5}: /

Instance Method Summary collapse

Constructor Details

#initialize(interface, dst, dst_port) ⇒ TdsPackageStream

Returns a new instance of TdsPackageStream.



14
15
16
17
18
19
# File 'lib/tds_package_stream.rb', line 14

def initialize(interface,dst,dst_port)
  @interface = interface
  @dst = dst
  @dst_port = dst_port
  @in,@out = IO.pipe()
end

Instance Method Details

#each_packageObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tds_package_stream.rb', line 21

def each_package()
  Thread.abort_on_exception = true       
  @tcpdump_thread = Thread.new(){             
    IO.popen("#{File.dirname(__FILE__)}/tcpflow -c -B -i #{@interface} tcp and dst #{@dst} and dst port #{@dst_port} 2>/dev/null"){|io|         
      content = ""
      loop(){           
        content << io.read(1)
        
        if(content.match(HEADER_REGEXP))                           
          payload = content[0..(-2 - HEADER_LENGTH)]
          @out.write(payload)
          @out.flush
          content = ""
        end
       
        Log.debug "Network package done"      
      }
    } 
  }  

  content = ""
  @in.each_byte(){|byte|
    if(byte == 0x0F)                 
      last_packet_indicator = Bytes.uint(@in)          
      length = Bytes.uint16be(@in)
       
      # skip next 4 bytes of the header
      if(Bytes.uint32be(@in) == 0x00)
        content << @in.read(length - 8)
        
        if(last_packet_indicator == 0x1)              
          begin
            io = StringIO.new(content)
            package = handle_package(io)
            yield package if package               
          ensure
            content = ""
          end             
        end
      end
    end           
  }     
end

#handle_package(io) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/tds_package_stream.rb', line 65

def handle_package(io)    
  token = Bytes.uint(io)
  token_class = TdsTokens.token_class(token)
  if(token_class)
    length = TdsTokens.fixed_length(token)
    unless(length)
      length_field_size = TdsTokens.length_field_size(token)
      if(length_field_size)
        length = Bytes.uintle(io,length_field_size)
      end       
    end
    Log.debug("Got #{TdsTokens.token_name(token)} of #{length} bytes")
    package = token_class.new(io,length)
    return package        
  end
  nil   
end