Class: Syspy::TdsPackageStream
- Inherits:
-
Object
- Object
- Syspy::TdsPackageStream
- Defined in:
- lib/tds_package_stream.rb
Instance Method Summary collapse
- #each_package ⇒ Object
- #handle_package(io) ⇒ Object
-
#initialize(interface, dst, dst_port) ⇒ TdsPackageStream
constructor
A new instance of TdsPackageStream.
- #read_ip_header(io) ⇒ Object
- #read_tcp_header(io, ip_package_size) ⇒ Object
Constructor Details
#initialize(interface, dst, dst_port) ⇒ TdsPackageStream
Returns a new instance of TdsPackageStream.
11 12 13 14 15 16 |
# File 'lib/tds_package_stream.rb', line 11 def initialize(interface,dst,dst_port) @interface = interface @dst = dst @dst_port = dst_port @in,@out = IO.pipe() end |
Instance Method Details
#each_package ⇒ Object
18 19 20 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 |
# File 'lib/tds_package_stream.rb', line 18 def each_package() Thread.abort_on_exception = true @tcpdump_thread = Thread.new(){ IO.popen("tcpdump -q -y EN10MB -U -B 2048 -w - -i #{@interface} tcp and dst #{@dst} and dst port #{@dst_port} 2>/dev/null"){|io| content = "" loop(){ tcp_length = read_ip_header(io) Log.debug "Got IP package: #{tcp_length}" content_length = read_tcp_header(io,tcp_length) Log.debug "Got TCP package: #{content_length}" if(content_length > 0) content = io.read(content_length) @out.write(content) @out.flush 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
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/tds_package_stream.rb', line 64 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 |
#read_ip_header(io) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/tds_package_stream.rb', line 82 def read_ip_header(io) count = 0 loop(){ network_type = Bytes.uint16be(io) break if network_type == 0x800 and count >= 14 count += 1 } # read IP version and header length ver_len = Bytes.uint(io) version = ver_len >> 4 raise "Invalid IP version: #{version}" unless version == 4 || version == 6 header_length = (ver_len & 0x0F) * 4 # skip TOS Bytes.uint(io) # get package length content_length = Bytes.uint16be(io) # consume remaining IP header io.read(header_length - 4) content_length - header_length end |
#read_tcp_header(io, ip_package_size) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/tds_package_stream.rb', line 109 def read_tcp_header(io, ip_package_size) # skip first 12 io.read(12) # get header length tcp_offset = (Bytes.uint(io) >> 4) * 4 # consume remaining tcp header io.read(tcp_offset - 13) ip_package_size - tcp_offset end |