Method: Masscan::Parsers::Binary.parse

Defined in:
lib/masscan/parsers/binary.rb

.parse(io) {|record| ... } ⇒ Enumerator

Parses masscan binary data.

Parameters:

  • io (IO)

    The IO object to read from.

Yields:

  • (record)

    If a block is given, it will be passed each parsed record.

Yield Parameters:

Returns:

  • (Enumerator)

    If no block is given, it will return an Enumerator.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/masscan/parsers/binary.rb', line 59

def self.parse(io)
  return enum_for(__method__,io) unless block_given?

  pseudo = read_pseudo_record(io)

  # look for the start time
  if (match = pseudo.match(/s:(\d+)/))
    start_time = decode_timestamp(match[1].to_i)
  end

  total_records = 0

  # read all records
  loop do
    # read the TYPE field
    unless (type = read_multibyte_uint(io))
      return
    end

    # read the LENGTH field
    unless (length = read_multibyte_uint(io))
      return
    end

    if length > BUF_MAX
      raise(CorruptedFile,"file corrupted")
    end

    # read the remainder of the record
    buffer = io.read(length)

    if buffer.length < length
      return
    end

    # parse the specific record type
    record = case type
             when 1 # STATUS: open
               parse_status(buffer,:open)
             when 2 # STATUS: closed
               parse_status(buffer,:closed)
             when 3 # BANNER
               parse_banner3(buffer)
             when 4
               io.getbyte
               parse_banner4(buffer)
             when 5
               parse_banner4(buffer)
             when 6 # STATUS: open
               parse_status2(buffer,:open)
             when 7 # STATUS: closed
               parse_status2(buffer,:closed)
             when 9
               parse_banner9(buffer)
             when 10 # Open6
               parse_status6(buffer,:open)
             when 11 # Closed6
               parse_status6(buffer,:closed)
             when 13 # Banner6
               parse_banner6(buffer)
             when 109 # 'm'.ord # FILEHEADER
               next
             else
               raise(CorruptedFile,"unknown type: #{type.inspect}")
             end

    if record
      start_time ||= record.timestamp

      yield record

      total_records += 1
    end
  end

  return total_records
end