Skip to content

Commit 28c320e

Browse files
committed
Implement Layer 3 and 4 filtering (WIP)
1 parent 64b3fc4 commit 28c320e

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

sockets/raw_socket.go

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"net"
66
"os"
7+
"strings"
78
"syscall"
89

910
"github.com/NamelessOne91/bisturi/protocols"
@@ -36,11 +37,17 @@ type RawSocket struct {
3637
// NewRawSocket opens a raw socket for the specified protocol by calling SYS_SOCKET
3738
// and returns the struct representing it, or eventual errors
3839
func NewRawSocket(protocol string, ethType uint16) (*RawSocket, error) {
40+
filter := "all"
41+
if strings.HasPrefix(protocol, "udp") {
42+
filter = "udp"
43+
} else if strings.HasPrefix(protocol, "tcp") {
44+
filter = "tcp"
45+
}
3946

4047
rawSocket := &RawSocket{
4148
shutdownChan: make(chan os.Signal, 1),
4249
ethType: ethType,
43-
layer4Filter: protocol,
50+
layer4Filter: filter,
4451
}
4552
// AF_PACKET specifies a packet socket, operating at the data link layer (Layer 2)
4653
// SOCK_RAW specifies a raw socket
@@ -76,31 +83,18 @@ func (rs *RawSocket) ReadToChan(dataChan chan<- NetworkPacket, errChan chan<- er
7683
}
7784

7885
switch rs.ethType {
79-
case syscall.ETH_P_IP:
80-
fallthrough
81-
case syscall.ETH_P_IPV6:
86+
case syscall.ETH_P_ARP:
87+
// TODO: ARP parsing
88+
case syscall.ETH_P_IP, syscall.ETH_P_IPV6:
8289
packet, err := protocols.IPPacketFromBytes(buf[:n])
8390
if err != nil {
8491
errChan <- fmt.Errorf("error reading IP packet: %v", err)
8592
continue
8693
}
87-
94+
// IPv4 VS IPv6 packets filtering should be handled by the socket itself
8895
l4Protocol := packet.Header().TransportLayerProtocol()
89-
switch l4Protocol {
90-
case "udp":
91-
packet, err := protocols.UDPPacketFromIPPacket(packet)
92-
if err != nil {
93-
errChan <- fmt.Errorf("error reading UDP packet: %v", err)
94-
continue
95-
}
96-
dataChan <- packet
97-
case "tcp":
98-
packet, err := protocols.TCPPacketFromIPPacket(packet)
99-
if err != nil {
100-
errChan <- fmt.Errorf("error reading TCP packet: %v", err)
101-
continue
102-
}
103-
dataChan <- packet
96+
if rs.layer4Filter == "all" || (l4Protocol == rs.layer4Filter) {
97+
handleLayer4Protocol(l4Protocol, packet, dataChan, errChan)
10498
}
10599
}
106100
}
@@ -110,3 +104,20 @@ func (rs *RawSocket) ReadToChan(dataChan chan<- NetworkPacket, errChan chan<- er
110104
func (rs *RawSocket) Close() error {
111105
return syscall.Close(rs.fd)
112106
}
107+
108+
func handleLayer4Protocol(protocol string, packet protocols.IPPacket, dataChan chan<- NetworkPacket, errChan chan<- error) {
109+
var np NetworkPacket
110+
var err error
111+
112+
switch protocol {
113+
case "udp":
114+
np, err = protocols.UDPPacketFromIPPacket(packet)
115+
case "tcp":
116+
np, err = protocols.TCPPacketFromIPPacket(packet)
117+
}
118+
119+
if err != nil {
120+
errChan <- fmt.Errorf("error reading UDP packet: %v", err)
121+
}
122+
dataChan <- np
123+
}

tui/models/packets_table.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ func newPacketsTable(max int) packetsTablemodel {
3333
cachedRows: rows,
3434
table: table.New([]table.Column{
3535
table.NewColumn(columnKeyID, "#", 5),
36-
table.NewColumn(columnKeyDate, "Date", 20),
37-
table.NewColumn(columnKeySource, "Source", 30),
38-
table.NewColumn(columnKeyDestination, "Destination", 30),
36+
table.NewColumn(columnKeyDate, "Date", 18),
37+
table.NewColumn(columnKeySource, "Source", 50),
38+
table.NewColumn(columnKeyDestination, "Destination", 50),
3939
table.NewColumn(columnKeyInfo, "Info", 100),
4040
}).
4141
WithRows(rows).

0 commit comments

Comments
 (0)