2019-12-01 20:10:49 +01:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
2020-11-20 00:53:29 +01:00
|
|
|
"fmt"
|
2020-02-18 02:05:15 +01:00
|
|
|
"net"
|
2020-11-20 00:53:29 +01:00
|
|
|
"strconv"
|
2020-03-06 20:32:19 +01:00
|
|
|
"syscall"
|
netlink: get active connections by source port + protocol
- Dump connections from kernel querying by source port + protocol.
- Prioritize responses which match the outgoing connection.
- If we don't get any response, apply the default action configured in
/etc/opensnitchd/default-config.json
--
A connection can be considered unique if:
protocol + source port + source ip + destination ip + destination port
We can be quite sure that only one process has created the connection.
However, many times, querying the kernel for the connection details by
all these parameters results in no response.
A regular query and normal response would be:
query: TCP:47344:192.168.1.106 -> 151.101.65.140:443
response: 47344:192.168.1.106 -> 151.101.65.140:443, inode: 1234567, ...
But in another cases, the details of the outgoing connection differs
from the kernel response, or it even doesn't exist.
However, if we query by protocol+source port, we can get more entries, and
somewhat guess what program opened the outgoing connection.
Some examples of querying by outgoing connection and response from
kernel:
query: 8612:192.168.1.5 -> 192.168.1.255:8612
response: 8612:192.168.1.105 -> 0.0.0.0:0
query: 123:192.168.1.5 -> 217.144.138.234:123
response: 123:0.0.0.0 -> 0.0.0.0:0
query: 45015:127.0.0.1 -> 239.255.255.250:1900
response: 45015:127.0.0.1 -> 0.0.0.0:0
query: 50416:fe80::9fc2:ddcf:df22:aa50 -> fe80::1:53
response: 50416:254.128.0.0 -> 254.128.0.0:53
query: 51413:192.168.1.106 -> 103.224.182.250:1337
response: 51413:0.0.0.0 -> 0.0.0.0:0
2020-04-05 19:14:51 +02:00
|
|
|
|
2020-12-09 18:18:42 +01:00
|
|
|
"github.com/evilsocket/opensnitch/daemon/log"
|
2024-05-13 00:27:41 +02:00
|
|
|
"github.com/vishvananda/netlink"
|
2023-07-25 01:42:54 +02:00
|
|
|
"golang.org/x/sys/unix"
|
2019-12-01 20:10:49 +01:00
|
|
|
)
|
|
|
|
|
2020-03-06 20:32:19 +01:00
|
|
|
// GetSocketInfo asks the kernel via netlink for a given connection.
|
netlink: get active connections by source port + protocol
- Dump connections from kernel querying by source port + protocol.
- Prioritize responses which match the outgoing connection.
- If we don't get any response, apply the default action configured in
/etc/opensnitchd/default-config.json
--
A connection can be considered unique if:
protocol + source port + source ip + destination ip + destination port
We can be quite sure that only one process has created the connection.
However, many times, querying the kernel for the connection details by
all these parameters results in no response.
A regular query and normal response would be:
query: TCP:47344:192.168.1.106 -> 151.101.65.140:443
response: 47344:192.168.1.106 -> 151.101.65.140:443, inode: 1234567, ...
But in another cases, the details of the outgoing connection differs
from the kernel response, or it even doesn't exist.
However, if we query by protocol+source port, we can get more entries, and
somewhat guess what program opened the outgoing connection.
Some examples of querying by outgoing connection and response from
kernel:
query: 8612:192.168.1.5 -> 192.168.1.255:8612
response: 8612:192.168.1.105 -> 0.0.0.0:0
query: 123:192.168.1.5 -> 217.144.138.234:123
response: 123:0.0.0.0 -> 0.0.0.0:0
query: 45015:127.0.0.1 -> 239.255.255.250:1900
response: 45015:127.0.0.1 -> 0.0.0.0:0
query: 50416:fe80::9fc2:ddcf:df22:aa50 -> fe80::1:53
response: 50416:254.128.0.0 -> 254.128.0.0:53
query: 51413:192.168.1.106 -> 103.224.182.250:1337
response: 51413:0.0.0.0 -> 0.0.0.0:0
2020-04-05 19:14:51 +02:00
|
|
|
// If the connection is found, we return the uid and the possible
|
|
|
|
// associated inodes.
|
|
|
|
// If the outgoing connection is not found but there're entries with the source
|
|
|
|
// port and same protocol, add all the inodes to the list.
|
|
|
|
//
|
|
|
|
// Some examples:
|
|
|
|
// outgoing connection as seen by netfilter || connection details dumped from kernel
|
|
|
|
//
|
|
|
|
// 47344:192.168.1.106 -> 151.101.65.140:443 || in kernel: 47344:192.168.1.106 -> 151.101.65.140:443
|
|
|
|
// 8612:192.168.1.5 -> 192.168.1.255:8612 || in kernel: 8612:192.168.1.105 -> 0.0.0.0:0
|
|
|
|
// 123:192.168.1.5 -> 217.144.138.234:123 || in kernel: 123:0.0.0.0 -> 0.0.0.0:0
|
|
|
|
// 45015:127.0.0.1 -> 239.255.255.250:1900 || in kernel: 45015:127.0.0.1 -> 0.0.0.0:0
|
|
|
|
// 50416:fe80::9fc2:ddcf:df22:aa50 -> fe80::1:53 || in kernel: 50416:254.128.0.0 -> 254.128.0.0:53
|
|
|
|
// 51413:192.168.1.106 -> 103.224.182.250:1337 || in kernel: 51413:0.0.0.0 -> 0.0.0.0:0
|
|
|
|
func GetSocketInfo(proto string, srcIP net.IP, srcPort uint, dstIP net.IP, dstPort uint) (uid int, inodes []int) {
|
|
|
|
uid = -1
|
2020-02-18 02:05:15 +01:00
|
|
|
family := uint8(syscall.AF_INET)
|
|
|
|
ipproto := uint8(syscall.IPPROTO_TCP)
|
|
|
|
protoLen := len(proto)
|
|
|
|
if proto[protoLen-1:protoLen] == "6" {
|
|
|
|
family = syscall.AF_INET6
|
|
|
|
}
|
2019-12-01 20:10:49 +01:00
|
|
|
|
2020-02-18 02:05:15 +01:00
|
|
|
if proto[:3] == "udp" {
|
|
|
|
ipproto = syscall.IPPROTO_UDP
|
2020-03-06 20:32:19 +01:00
|
|
|
if protoLen >= 7 && proto[:7] == "udplite" {
|
2020-02-18 02:05:15 +01:00
|
|
|
ipproto = syscall.IPPROTO_UDPLITE
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 00:41:06 +01:00
|
|
|
if protoLen >= 4 && proto[:4] == "sctp" {
|
|
|
|
ipproto = syscall.IPPROTO_SCTP
|
|
|
|
}
|
|
|
|
if protoLen >= 4 && proto[:4] == "icmp" {
|
|
|
|
ipproto = syscall.IPPROTO_RAW
|
|
|
|
}
|
netlink: get active connections by source port + protocol
- Dump connections from kernel querying by source port + protocol.
- Prioritize responses which match the outgoing connection.
- If we don't get any response, apply the default action configured in
/etc/opensnitchd/default-config.json
--
A connection can be considered unique if:
protocol + source port + source ip + destination ip + destination port
We can be quite sure that only one process has created the connection.
However, many times, querying the kernel for the connection details by
all these parameters results in no response.
A regular query and normal response would be:
query: TCP:47344:192.168.1.106 -> 151.101.65.140:443
response: 47344:192.168.1.106 -> 151.101.65.140:443, inode: 1234567, ...
But in another cases, the details of the outgoing connection differs
from the kernel response, or it even doesn't exist.
However, if we query by protocol+source port, we can get more entries, and
somewhat guess what program opened the outgoing connection.
Some examples of querying by outgoing connection and response from
kernel:
query: 8612:192.168.1.5 -> 192.168.1.255:8612
response: 8612:192.168.1.105 -> 0.0.0.0:0
query: 123:192.168.1.5 -> 217.144.138.234:123
response: 123:0.0.0.0 -> 0.0.0.0:0
query: 45015:127.0.0.1 -> 239.255.255.250:1900
response: 45015:127.0.0.1 -> 0.0.0.0:0
query: 50416:fe80::9fc2:ddcf:df22:aa50 -> fe80::1:53
response: 50416:254.128.0.0 -> 254.128.0.0:53
query: 51413:192.168.1.106 -> 103.224.182.250:1337
response: 51413:0.0.0.0 -> 0.0.0.0:0
2020-04-05 19:14:51 +02:00
|
|
|
if sockList, err := SocketGet(family, ipproto, uint16(srcPort), uint16(dstPort), srcIP, dstIP); err == nil {
|
|
|
|
for n, sock := range sockList {
|
|
|
|
if sock.UID != 0xffffffff {
|
|
|
|
uid = int(sock.UID)
|
|
|
|
}
|
2021-11-15 13:26:52 +01:00
|
|
|
log.Debug("[%d/%d] outgoing connection uid: %d, %d:%v -> %v:%d || netlink response: %d:%v -> %v:%d inode: %d - loopback: %v multicast: %v unspecified: %v linklocalunicast: %v ifaceLocalMulticast: %v GlobalUni: %v ",
|
netlink: get active connections by source port + protocol
- Dump connections from kernel querying by source port + protocol.
- Prioritize responses which match the outgoing connection.
- If we don't get any response, apply the default action configured in
/etc/opensnitchd/default-config.json
--
A connection can be considered unique if:
protocol + source port + source ip + destination ip + destination port
We can be quite sure that only one process has created the connection.
However, many times, querying the kernel for the connection details by
all these parameters results in no response.
A regular query and normal response would be:
query: TCP:47344:192.168.1.106 -> 151.101.65.140:443
response: 47344:192.168.1.106 -> 151.101.65.140:443, inode: 1234567, ...
But in another cases, the details of the outgoing connection differs
from the kernel response, or it even doesn't exist.
However, if we query by protocol+source port, we can get more entries, and
somewhat guess what program opened the outgoing connection.
Some examples of querying by outgoing connection and response from
kernel:
query: 8612:192.168.1.5 -> 192.168.1.255:8612
response: 8612:192.168.1.105 -> 0.0.0.0:0
query: 123:192.168.1.5 -> 217.144.138.234:123
response: 123:0.0.0.0 -> 0.0.0.0:0
query: 45015:127.0.0.1 -> 239.255.255.250:1900
response: 45015:127.0.0.1 -> 0.0.0.0:0
query: 50416:fe80::9fc2:ddcf:df22:aa50 -> fe80::1:53
response: 50416:254.128.0.0 -> 254.128.0.0:53
query: 51413:192.168.1.106 -> 103.224.182.250:1337
response: 51413:0.0.0.0 -> 0.0.0.0:0
2020-04-05 19:14:51 +02:00
|
|
|
n, len(sockList),
|
2021-11-15 13:26:52 +01:00
|
|
|
int(sock.UID),
|
netlink: get active connections by source port + protocol
- Dump connections from kernel querying by source port + protocol.
- Prioritize responses which match the outgoing connection.
- If we don't get any response, apply the default action configured in
/etc/opensnitchd/default-config.json
--
A connection can be considered unique if:
protocol + source port + source ip + destination ip + destination port
We can be quite sure that only one process has created the connection.
However, many times, querying the kernel for the connection details by
all these parameters results in no response.
A regular query and normal response would be:
query: TCP:47344:192.168.1.106 -> 151.101.65.140:443
response: 47344:192.168.1.106 -> 151.101.65.140:443, inode: 1234567, ...
But in another cases, the details of the outgoing connection differs
from the kernel response, or it even doesn't exist.
However, if we query by protocol+source port, we can get more entries, and
somewhat guess what program opened the outgoing connection.
Some examples of querying by outgoing connection and response from
kernel:
query: 8612:192.168.1.5 -> 192.168.1.255:8612
response: 8612:192.168.1.105 -> 0.0.0.0:0
query: 123:192.168.1.5 -> 217.144.138.234:123
response: 123:0.0.0.0 -> 0.0.0.0:0
query: 45015:127.0.0.1 -> 239.255.255.250:1900
response: 45015:127.0.0.1 -> 0.0.0.0:0
query: 50416:fe80::9fc2:ddcf:df22:aa50 -> fe80::1:53
response: 50416:254.128.0.0 -> 254.128.0.0:53
query: 51413:192.168.1.106 -> 103.224.182.250:1337
response: 51413:0.0.0.0 -> 0.0.0.0:0
2020-04-05 19:14:51 +02:00
|
|
|
srcPort, srcIP, dstIP, dstPort,
|
|
|
|
sock.ID.SourcePort, sock.ID.Source,
|
|
|
|
sock.ID.Destination, sock.ID.DestinationPort, sock.INode,
|
|
|
|
sock.ID.Destination.IsLoopback(),
|
|
|
|
sock.ID.Destination.IsMulticast(),
|
|
|
|
sock.ID.Destination.IsUnspecified(),
|
|
|
|
sock.ID.Destination.IsLinkLocalUnicast(),
|
|
|
|
sock.ID.Destination.IsLinkLocalMulticast(),
|
|
|
|
sock.ID.Destination.IsGlobalUnicast(),
|
|
|
|
)
|
|
|
|
|
|
|
|
if sock.ID.SourcePort == uint16(srcPort) && sock.ID.Source.Equal(srcIP) &&
|
|
|
|
(sock.ID.DestinationPort == uint16(dstPort)) &&
|
|
|
|
((sock.ID.Destination.IsGlobalUnicast() || sock.ID.Destination.IsLoopback()) && sock.ID.Destination.Equal(dstIP)) {
|
|
|
|
inodes = append([]int{int(sock.INode)}, inodes...)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Debug("GetSocketInfo() invalid: %d:%v -> %v:%d", sock.ID.SourcePort, sock.ID.Source, sock.ID.Destination, sock.ID.DestinationPort)
|
|
|
|
}
|
|
|
|
|
2021-11-15 13:26:52 +01:00
|
|
|
// handle special cases (see function description): ntp queries (123), broadcasts, incomming connections.
|
netlink: get active connections by source port + protocol
- Dump connections from kernel querying by source port + protocol.
- Prioritize responses which match the outgoing connection.
- If we don't get any response, apply the default action configured in
/etc/opensnitchd/default-config.json
--
A connection can be considered unique if:
protocol + source port + source ip + destination ip + destination port
We can be quite sure that only one process has created the connection.
However, many times, querying the kernel for the connection details by
all these parameters results in no response.
A regular query and normal response would be:
query: TCP:47344:192.168.1.106 -> 151.101.65.140:443
response: 47344:192.168.1.106 -> 151.101.65.140:443, inode: 1234567, ...
But in another cases, the details of the outgoing connection differs
from the kernel response, or it even doesn't exist.
However, if we query by protocol+source port, we can get more entries, and
somewhat guess what program opened the outgoing connection.
Some examples of querying by outgoing connection and response from
kernel:
query: 8612:192.168.1.5 -> 192.168.1.255:8612
response: 8612:192.168.1.105 -> 0.0.0.0:0
query: 123:192.168.1.5 -> 217.144.138.234:123
response: 123:0.0.0.0 -> 0.0.0.0:0
query: 45015:127.0.0.1 -> 239.255.255.250:1900
response: 45015:127.0.0.1 -> 0.0.0.0:0
query: 50416:fe80::9fc2:ddcf:df22:aa50 -> fe80::1:53
response: 50416:254.128.0.0 -> 254.128.0.0:53
query: 51413:192.168.1.106 -> 103.224.182.250:1337
response: 51413:0.0.0.0 -> 0.0.0.0:0
2020-04-05 19:14:51 +02:00
|
|
|
if len(inodes) == 0 && len(sockList) > 0 {
|
|
|
|
for n, sock := range sockList {
|
2021-07-29 15:40:25 +02:00
|
|
|
if sockList[n].ID.Destination.Equal(net.IPv4zero) || sockList[n].ID.Destination.Equal(net.IPv6zero) {
|
|
|
|
inodes = append([]int{int(sock.INode)}, inodes...)
|
|
|
|
log.Debug("netlink socket not found, adding entry: %d:%v -> %v:%d || %d:%v -> %v:%d inode: %d state: %s",
|
|
|
|
srcPort, srcIP, dstIP, dstPort,
|
|
|
|
sockList[n].ID.SourcePort, sockList[n].ID.Source,
|
|
|
|
sockList[n].ID.Destination, sockList[n].ID.DestinationPort,
|
|
|
|
sockList[n].INode, TCPStatesMap[sock.State])
|
2021-11-15 13:26:52 +01:00
|
|
|
} else if sock.ID.SourcePort == uint16(srcPort) && sock.ID.Source.Equal(srcIP) &&
|
|
|
|
(sock.ID.DestinationPort == uint16(dstPort)) {
|
|
|
|
inodes = append([]int{int(sock.INode)}, inodes...)
|
|
|
|
continue
|
2021-07-29 15:40:25 +02:00
|
|
|
} else {
|
|
|
|
log.Debug("netlink socket not found, EXCLUDING entry: %d:%v -> %v:%d || %d:%v -> %v:%d inode: %d state: %s",
|
|
|
|
srcPort, srcIP, dstIP, dstPort,
|
|
|
|
sockList[n].ID.SourcePort, sockList[n].ID.Source,
|
|
|
|
sockList[n].ID.Destination, sockList[n].ID.DestinationPort,
|
|
|
|
sockList[n].INode, TCPStatesMap[sock.State])
|
|
|
|
}
|
netlink: get active connections by source port + protocol
- Dump connections from kernel querying by source port + protocol.
- Prioritize responses which match the outgoing connection.
- If we don't get any response, apply the default action configured in
/etc/opensnitchd/default-config.json
--
A connection can be considered unique if:
protocol + source port + source ip + destination ip + destination port
We can be quite sure that only one process has created the connection.
However, many times, querying the kernel for the connection details by
all these parameters results in no response.
A regular query and normal response would be:
query: TCP:47344:192.168.1.106 -> 151.101.65.140:443
response: 47344:192.168.1.106 -> 151.101.65.140:443, inode: 1234567, ...
But in another cases, the details of the outgoing connection differs
from the kernel response, or it even doesn't exist.
However, if we query by protocol+source port, we can get more entries, and
somewhat guess what program opened the outgoing connection.
Some examples of querying by outgoing connection and response from
kernel:
query: 8612:192.168.1.5 -> 192.168.1.255:8612
response: 8612:192.168.1.105 -> 0.0.0.0:0
query: 123:192.168.1.5 -> 217.144.138.234:123
response: 123:0.0.0.0 -> 0.0.0.0:0
query: 45015:127.0.0.1 -> 239.255.255.250:1900
response: 45015:127.0.0.1 -> 0.0.0.0:0
query: 50416:fe80::9fc2:ddcf:df22:aa50 -> fe80::1:53
response: 50416:254.128.0.0 -> 254.128.0.0:53
query: 51413:192.168.1.106 -> 103.224.182.250:1337
response: 51413:0.0.0.0 -> 0.0.0.0:0
2020-04-05 19:14:51 +02:00
|
|
|
}
|
2020-02-18 02:05:15 +01:00
|
|
|
}
|
netlink: get active connections by source port + protocol
- Dump connections from kernel querying by source port + protocol.
- Prioritize responses which match the outgoing connection.
- If we don't get any response, apply the default action configured in
/etc/opensnitchd/default-config.json
--
A connection can be considered unique if:
protocol + source port + source ip + destination ip + destination port
We can be quite sure that only one process has created the connection.
However, many times, querying the kernel for the connection details by
all these parameters results in no response.
A regular query and normal response would be:
query: TCP:47344:192.168.1.106 -> 151.101.65.140:443
response: 47344:192.168.1.106 -> 151.101.65.140:443, inode: 1234567, ...
But in another cases, the details of the outgoing connection differs
from the kernel response, or it even doesn't exist.
However, if we query by protocol+source port, we can get more entries, and
somewhat guess what program opened the outgoing connection.
Some examples of querying by outgoing connection and response from
kernel:
query: 8612:192.168.1.5 -> 192.168.1.255:8612
response: 8612:192.168.1.105 -> 0.0.0.0:0
query: 123:192.168.1.5 -> 217.144.138.234:123
response: 123:0.0.0.0 -> 0.0.0.0:0
query: 45015:127.0.0.1 -> 239.255.255.250:1900
response: 45015:127.0.0.1 -> 0.0.0.0:0
query: 50416:fe80::9fc2:ddcf:df22:aa50 -> fe80::1:53
response: 50416:254.128.0.0 -> 254.128.0.0:53
query: 51413:192.168.1.106 -> 103.224.182.250:1337
response: 51413:0.0.0.0 -> 0.0.0.0:0
2020-04-05 19:14:51 +02:00
|
|
|
} else {
|
|
|
|
log.Debug("netlink socket error: %v - %d:%v -> %v:%d", err, srcPort, srcIP, dstIP, dstPort)
|
2020-02-18 02:05:15 +01:00
|
|
|
}
|
2019-12-01 20:10:49 +01:00
|
|
|
|
netlink: get active connections by source port + protocol
- Dump connections from kernel querying by source port + protocol.
- Prioritize responses which match the outgoing connection.
- If we don't get any response, apply the default action configured in
/etc/opensnitchd/default-config.json
--
A connection can be considered unique if:
protocol + source port + source ip + destination ip + destination port
We can be quite sure that only one process has created the connection.
However, many times, querying the kernel for the connection details by
all these parameters results in no response.
A regular query and normal response would be:
query: TCP:47344:192.168.1.106 -> 151.101.65.140:443
response: 47344:192.168.1.106 -> 151.101.65.140:443, inode: 1234567, ...
But in another cases, the details of the outgoing connection differs
from the kernel response, or it even doesn't exist.
However, if we query by protocol+source port, we can get more entries, and
somewhat guess what program opened the outgoing connection.
Some examples of querying by outgoing connection and response from
kernel:
query: 8612:192.168.1.5 -> 192.168.1.255:8612
response: 8612:192.168.1.105 -> 0.0.0.0:0
query: 123:192.168.1.5 -> 217.144.138.234:123
response: 123:0.0.0.0 -> 0.0.0.0:0
query: 45015:127.0.0.1 -> 239.255.255.250:1900
response: 45015:127.0.0.1 -> 0.0.0.0:0
query: 50416:fe80::9fc2:ddcf:df22:aa50 -> fe80::1:53
response: 50416:254.128.0.0 -> 254.128.0.0:53
query: 51413:192.168.1.106 -> 103.224.182.250:1337
response: 51413:0.0.0.0 -> 0.0.0.0:0
2020-04-05 19:14:51 +02:00
|
|
|
return uid, inodes
|
2019-12-01 20:10:49 +01:00
|
|
|
}
|
2020-11-20 00:53:29 +01:00
|
|
|
|
2020-12-23 13:24:59 -05:00
|
|
|
// GetSocketInfoByInode dumps the kernel sockets table and searches the given
|
2020-11-20 00:53:29 +01:00
|
|
|
// inode on it.
|
|
|
|
func GetSocketInfoByInode(inodeStr string) (*Socket, error) {
|
|
|
|
inode, err := strconv.ParseUint(inodeStr, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type inetStruct struct{ family, proto uint8 }
|
|
|
|
socketTypes := []inetStruct{
|
|
|
|
{syscall.AF_INET, syscall.IPPROTO_TCP},
|
|
|
|
{syscall.AF_INET, syscall.IPPROTO_UDP},
|
|
|
|
{syscall.AF_INET6, syscall.IPPROTO_TCP},
|
|
|
|
{syscall.AF_INET6, syscall.IPPROTO_UDP},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, socket := range socketTypes {
|
|
|
|
socketList, err := SocketsDump(socket.family, socket.proto)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for idx := range socketList {
|
|
|
|
if uint32(inode) == socketList[idx].INode {
|
|
|
|
return socketList[idx], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Inode not found")
|
|
|
|
}
|
2021-09-12 10:54:24 +02:00
|
|
|
|
|
|
|
// KillSocket kills a socket given the properties of a connection.
|
|
|
|
func KillSocket(proto string, srcIP net.IP, srcPort uint, dstIP net.IP, dstPort uint) {
|
|
|
|
family := uint8(syscall.AF_INET)
|
|
|
|
ipproto := uint8(syscall.IPPROTO_TCP)
|
|
|
|
protoLen := len(proto)
|
|
|
|
if proto[protoLen-1:protoLen] == "6" {
|
|
|
|
family = syscall.AF_INET6
|
|
|
|
}
|
|
|
|
|
|
|
|
if proto[:3] == "udp" {
|
|
|
|
ipproto = syscall.IPPROTO_UDP
|
|
|
|
if protoLen >= 7 && proto[:7] == "udplite" {
|
|
|
|
ipproto = syscall.IPPROTO_UDPLITE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sockList, err := SocketGet(family, ipproto, uint16(srcPort), uint16(dstPort), srcIP, dstIP); err == nil {
|
|
|
|
for _, s := range sockList {
|
2023-07-25 01:42:54 +02:00
|
|
|
if err := SocketKill(family, ipproto, s.ID); err != nil {
|
2022-01-28 14:07:48 +01:00
|
|
|
log.Debug("Unable to kill socket: %d, %d, %v", srcPort, dstPort, err)
|
2021-09-12 10:54:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-24 11:18:09 +02:00
|
|
|
|
2023-07-25 01:42:54 +02:00
|
|
|
// KillSockets kills all sockets given a family and a protocol.
|
|
|
|
// Be careful if you don't exclude local sockets, many local servers may misbehave,
|
|
|
|
// entering in an infinite loop.
|
|
|
|
func KillSockets(fam, proto uint8, excludeLocal bool) error {
|
|
|
|
sockListTCP, err := SocketsDump(fam, proto)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("eBPF could not dump TCP (%d/%d) sockets via netlink: %v", fam, proto, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, sock := range sockListTCP {
|
2023-07-25 02:11:35 +02:00
|
|
|
if excludeLocal && (isPrivate(sock.ID.Destination) ||
|
2023-07-25 01:42:54 +02:00
|
|
|
sock.ID.Source.IsUnspecified() ||
|
|
|
|
sock.ID.Destination.IsUnspecified()) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := SocketKill(fam, proto, sock.ID); err != nil {
|
2023-07-25 02:11:35 +02:00
|
|
|
log.Debug("Unable to kill socket (%+v): %s", sock.ID, err)
|
2023-07-25 01:42:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// KillAllSockets kills the sockets for the given families and protocols.
|
|
|
|
func KillAllSockets() {
|
|
|
|
type opts struct {
|
|
|
|
fam uint8
|
|
|
|
proto uint8
|
|
|
|
}
|
|
|
|
optList := []opts{
|
|
|
|
// add families and protos as wish
|
|
|
|
{unix.AF_INET, uint8(syscall.IPPROTO_TCP)},
|
|
|
|
{unix.AF_INET6, uint8(syscall.IPPROTO_TCP)},
|
|
|
|
{unix.AF_INET, uint8(syscall.IPPROTO_UDP)},
|
|
|
|
{unix.AF_INET6, uint8(syscall.IPPROTO_UDP)},
|
|
|
|
{unix.AF_INET, uint8(syscall.IPPROTO_SCTP)},
|
|
|
|
{unix.AF_INET6, uint8(syscall.IPPROTO_SCTP)},
|
|
|
|
}
|
|
|
|
for _, opt := range optList {
|
|
|
|
KillSockets(opt.fam, opt.proto, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-05-13 00:27:41 +02:00
|
|
|
// FlushConnections flushes conntrack as soon as netfilter rule is set.
|
|
|
|
// This ensures that already-established connections will go to netfilter queue.
|
|
|
|
func FlushConnections() {
|
|
|
|
if err := netlink.ConntrackTableFlush(netlink.ConntrackTable); err != nil {
|
|
|
|
log.Error("error flushing ConntrackTable %s", err)
|
|
|
|
}
|
|
|
|
if err := netlink.ConntrackTableFlush(netlink.ConntrackExpectTable); err != nil {
|
|
|
|
log.Error("error flusing ConntrackExpectTable %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force established connections to reestablish again.
|
|
|
|
KillAllSockets()
|
|
|
|
}
|
|
|
|
|
2023-07-24 11:18:09 +02:00
|
|
|
// SocketsAreEqual compares 2 different sockets to see if they match.
|
|
|
|
func SocketsAreEqual(aSocket, bSocket *Socket) bool {
|
|
|
|
return ((*aSocket).INode == (*bSocket).INode &&
|
|
|
|
//inodes are unique enough, so the matches below will never have to be checked
|
|
|
|
(*aSocket).ID.SourcePort == (*bSocket).ID.SourcePort &&
|
|
|
|
(*aSocket).ID.Source.Equal((*bSocket).ID.Source) &&
|
|
|
|
(*aSocket).ID.Destination.Equal((*bSocket).ID.Destination) &&
|
|
|
|
(*aSocket).ID.DestinationPort == (*bSocket).ID.DestinationPort &&
|
|
|
|
(*aSocket).UID == (*bSocket).UID)
|
|
|
|
}
|