File tree 6 files changed +29
-51
lines changed
6 files changed +29
-51
lines changed Original file line number Diff line number Diff line change 2
2
3
3
module Redhound
4
4
class Analyzer
5
- def self . analyze ( msg :)
6
- new ( msg : msg ) . analyze
5
+ def self . analyze ( msg :, count : )
6
+ new ( msg :, count : ) . analyze
7
7
end
8
8
9
- def initialize ( msg :)
9
+ def initialize ( msg :, count : )
10
10
@msg = msg
11
+ @count = count
11
12
end
12
13
13
14
def analyze
14
- puts 'Analyzing...'
15
- ether = Header ::Ether . generate ( bytes : @msg . bytes [ 0 ..13 ] )
15
+ ether = Header ::Ether . generate ( bytes : @msg . bytes [ 0 ..13 ] , count : @count )
16
16
ether . dump
17
17
return unless ether . ipv4?
18
18
@@ -25,7 +25,6 @@ def analyze
25
25
icmp = Header ::Icmp . generate ( bytes : @msg . bytes [ 34 ..] )
26
26
icmp . dump
27
27
end
28
- puts
29
28
end
30
29
end
31
30
end
Original file line number Diff line number Diff line change @@ -6,15 +6,16 @@ class Ether
6
6
ETH_P_IP = 0x0800
7
7
8
8
class << self
9
- def generate ( bytes :)
10
- new ( bytes :) . generate
9
+ def generate ( bytes :, count : )
10
+ new ( bytes :, count : ) . generate
11
11
end
12
12
end
13
13
14
- def initialize ( bytes :)
14
+ def initialize ( bytes :, count : )
15
15
raise ArgumentError , 'bytes must be 14 bytes' unless bytes . size == 14
16
16
17
17
@bytes = bytes
18
+ @count = count
18
19
end
19
20
20
21
def generate
@@ -29,16 +30,11 @@ def ipv4?
29
30
end
30
31
31
32
def dump
32
- puts 'ETHERNET HEADER----------------'
33
33
puts self
34
34
end
35
35
36
36
def to_s
37
- <<~ETHER
38
- Destination MAC: #{ dhost }
39
- Source MAC: #{ shost }
40
- Type: #{ type }
41
- ETHER
37
+ "[#{ @count } ] Ethernet Dst: #{ dhost } Src: #{ shost } Type: #{ type } "
42
38
end
43
39
44
40
def dhost
Original file line number Diff line number Diff line change @@ -31,26 +31,19 @@ def generate
31
31
end
32
32
33
33
def dump
34
- puts 'ICMP HEADER----------------'
35
34
puts self
36
35
end
37
36
38
37
def to_s
39
38
if @type . zero? || @type == 8
40
- <<~ICMP
41
- Type: #{ @type }
42
- Code: #{ @code }
43
- Checksum: #{ check }
44
- ID: #{ id }
45
- Sequence: #{ seq }
46
- Data: #{ data }
39
+ <<-ICMP . chomp
40
+ └─ ICMP Type: #{ @type } Code: #{ @code } Checksum: #{ check } ID: #{ id } Sequence: #{ seq }
41
+ └─ Payload: #{ data }
47
42
ICMP
48
43
else
49
- <<~ICMP
50
- Type: #{ @type }
51
- Code: #{ @code }
52
- Checksum: #{ check }
53
- Data: #{ data }
44
+ <<-ICMP . chomp
45
+ └─ ICMP Type: #{ @type } Code: #{ @code } Checksum: #{ check }
46
+ └─ Payload: #{ data }
54
47
ICMP
55
48
end
56
49
end
@@ -70,7 +63,7 @@ def seq
70
63
end
71
64
72
65
def data
73
- @data . map ( &:chr ) . join
66
+ @data . map ( &:chr ) . join . force_encoding ( "UTF-8" )
74
67
end
75
68
end
76
69
end
Original file line number Diff line number Diff line change @@ -43,24 +43,11 @@ def udp?
43
43
end
44
44
45
45
def dump
46
- puts 'IPv4 HEADER----------------'
47
46
puts self
48
47
end
49
48
50
49
def to_s
51
- <<~IPV4
52
- Version: #{ @version }
53
- IHL: #{ @ihl }
54
- TOS: #{ @tos }
55
- Total Length: #{ tot_len }
56
- ID: #{ id }
57
- Fragment Offset: #{ frag_off }
58
- TTL: #{ @ttl }
59
- Protocol: #{ protocol }
60
- Checksum: #{ check }
61
- Source IP: #{ saddr }
62
- Destination IP: #{ daddr }
63
- IPV4
50
+ " └─ IPv4 Ver: #{ version } IHL: #{ ihl } TOS: #{ @tos } Total Length: #{ tot_len } ID: #{ id } Offset: #{ frag_off } TTL: #{ @ttl } Protocol: #{ protocol } Checksum: #{ check } Src: #{ saddr } Dst: #{ daddr } "
64
51
end
65
52
66
53
private
Original file line number Diff line number Diff line change @@ -25,17 +25,13 @@ def generate
25
25
end
26
26
27
27
def dump
28
- puts 'UDP HEADER----------------'
29
28
puts self
30
29
end
31
30
32
31
def to_s
33
- <<~UDP
34
- Source Port: #{ sport }
35
- Destination Port: #{ dport }
36
- Length: #{ len }
37
- Checksum: #{ check }
38
- Data: #{ data }
32
+ <<-UDP
33
+ └─ UDP Src: #{ sport } Dst: #{ dport } Len: #{ len } Checksum: #{ check }
34
+ └─ Payload: #{ data }
39
35
UDP
40
36
end
41
37
Original file line number Diff line number Diff line change @@ -17,17 +17,24 @@ def initialize(ifname:, filename:)
17
17
@writer = Writer . new ( filename :)
18
18
@writer . start
19
19
end
20
+ @count = 0
20
21
end
21
22
22
23
def run
23
24
loop do
24
25
msg , = @source . next_packet
25
- Analyzer . analyze ( msg :)
26
+ Analyzer . analyze ( msg :, count : increment )
26
27
@writer &.write ( msg )
27
28
rescue Interrupt
28
29
@writer &.stop
29
30
break
30
31
end
31
32
end
33
+
34
+ private
35
+
36
+ def increment
37
+ @count . tap { @count += 1 }
38
+ end
32
39
end
33
40
end
You can’t perform that action at this time.
0 commit comments