● Notice //GitHub (@shehzade) is temporarily suspended pending an automated false-positive review. All tools are hosted on this Code Mirror and available on request (contact); GitHub links will return once reinstated.
PUBLIC MIRRORGo v1.0

Multi-protocol network egress assessment and DLP verification framework featuring automated L4 sweeps, corporate proxy traversal, and covert protocol smuggling across 20+ channels.

$ git clone https://shehzade.io/repos/contraband.git Copy
Source ZIP (1.93 MB)
Code 174 Commits 1
contraband / pkg / server / multiplexer / port_safety.go
← Back to directory
178 lines · 6.3 KB
Raw
1package multiplexer
2
3import (
4 "errors"
5 "fmt"
6 "net"
7 "strconv"
8 "strings"
9 "syscall"
10 "time"
11
12 "github.com/shehzade/contraband/pkg/common"
13)
14
15// ProtectedPorts defines the strict non-interference port safety policy.
16// These operational ports are strictly excluded from redirection, interception,
17// and multiplexer binding to guarantee non-interference with critical services
18// (e.g., Cobalt Strike TeamServer PID 8842, HTTPS/Web, C2 listeners, and SSH management).
19var ProtectedPorts = map[int]string{
20 22: "SSH Management",
21 443: "HTTPS / TeamServer Web",
22 4444: "C2 Listener",
23 4445: "C2 Listener",
24 4446: "C2 Listener",
25 4447: "C2 Listener",
26 4448: "C2 Listener",
27 4449: "C2 Listener",
28 50050: "Cobalt Strike TeamServer",
29}
30
31// IsProtectedPort reports whether a given port is protected from multiplexer binding or redirection.
32func IsProtectedPort(port int) bool {
33 _, protected := ProtectedPorts[port]
34 return protected
35}
36
37// IsPortOccupied probes whether another process is actively listening on target port on 127.0.0.1.
38// Strictly non-interfering: uses non-blocking dial with 20ms timeout for TCP.
39// Protected operational ports (22, 443, 4444-4449, 50050) always report occupied to guarantee non-interference.
40func IsPortOccupied(proto string, port int) bool {
41 if IsProtectedPort(port) {
42 return true
43 }
44 if strings.EqualFold(proto, "tcp") {
45 target := net.JoinHostPort("127.0.0.1", strconv.Itoa(port))
46 conn, err := net.DialTimeout("tcp", target, 20*time.Millisecond)
47 if err == nil {
48 _ = conn.Close()
49 return true
50 }
51 } else if strings.EqualFold(proto, "udp") {
52 addr := net.JoinHostPort("127.0.0.1", strconv.Itoa(port))
53 pc, err := net.ListenPacket("udp", addr)
54 if err != nil {
55 if errors.Is(err, syscall.EADDRINUSE) || strings.Contains(err.Error(), "address already in use") {
56 return true
57 }
58 } else {
59 _ = pc.Close()
60 return false
61 }
62 }
63 return false
64}
65
66// SafetyChecker evaluates local port availability without service disruption.
67type SafetyChecker struct {
68 logger common.Logger
69}
70
71// NewSafetyChecker constructs a new safety pre-checker.
72func NewSafetyChecker(logger common.Logger) *SafetyChecker {
73 if logger == nil {
74 logger = common.GetLogger()
75 }
76 return &SafetyChecker{logger: logger}
77}
78
79// IsPortOccupied probes whether another process is actively listening on target port.
80func (s *SafetyChecker) IsPortOccupied(proto string, port int) bool {
81 return IsPortOccupied(proto, port)
82}
83
84// IsPortOccupiedOn checks if port is occupied on a specific bind IP.
85func (s *SafetyChecker) IsPortOccupiedOn(network, bindIP string, port int) bool {
86 if IsProtectedPort(port) {
87 return true
88 }
89 if strings.EqualFold(network, "tcp") {
90 targetHost := bindIP
91 if targetHost == "" || targetHost == "0.0.0.0" {
92 targetHost = "127.0.0.1"
93 }
94 target := net.JoinHostPort(targetHost, strconv.Itoa(port))
95 conn, err := net.DialTimeout("tcp", target, 20*time.Millisecond)
96 if err == nil {
97 _ = conn.Close()
98 return true
99 }
100 } else if strings.EqualFold(network, "udp") {
101 targetHost := bindIP
102 if targetHost == "" || targetHost == "0.0.0.0" {
103 targetHost = "127.0.0.1"
104 }
105 addr := net.JoinHostPort(targetHost, strconv.Itoa(port))
106 pc, err := net.ListenPacket("udp", addr)
107 if err != nil {
108 if errors.Is(err, syscall.EADDRINUSE) || strings.Contains(err.Error(), "address already in use") {
109 return true
110 }
111 } else {
112 _ = pc.Close()
113 return false
114 }
115 }
116 return false
117}
118
119// SafeListenTCP attempts to bind a TCP listener. If the port is already bound
120// by an existing system service, requires root privileges, or is on the strict
121// protected ports list, it gracefully skips without failing or disrupting existing infrastructure.
122func (s *SafetyChecker) SafeListenTCP(bindIP string, port int) (net.Listener, PortBindStatus, error) {
123 // Strict Port Safety Policy: protect operational ports (22, 443, 4444-4449, 50050)
124 if IsProtectedPort(port) {
125 s.logger.Warnf("Port %d/TCP is a strictly protected operational port (%s); skipping gracefully without disruption (operational safety rule)", port, ProtectedPorts[port])
126 return nil, PortStatusOccupiedSkipped, nil
127 }
128
129 addr := net.JoinHostPort(bindIP, strconv.Itoa(port))
130
131 // Pre-check occupancy
132 if s.IsPortOccupiedOn("tcp", bindIP, port) {
133 s.logger.Warnf("Port %d/TCP is occupied by an existing system service; skipping gracefully without disruption (operational safety rule)", port)
134 return nil, PortStatusOccupiedSkipped, nil
135 }
136
137 ln, err := net.Listen("tcp", addr)
138 if err != nil {
139 if errors.Is(err, syscall.EADDRINUSE) || strings.Contains(err.Error(), "address already in use") {
140 s.logger.Warnf("Port %d/TCP in use (EADDRINUSE); skipping gracefully without disruption", port)
141 return nil, PortStatusOccupiedSkipped, nil
142 }
143 if errors.Is(err, syscall.EACCES) || strings.Contains(err.Error(), "permission denied") {
144 s.logger.Warnf("Port %d/TCP requires elevated privileges (cap_net_bind_service); skipping gracefully", port)
145 return nil, PortStatusPermissionDenied, nil
146 }
147 return nil, PortStatusFailed, fmt.Errorf("failed to bind TCP port %d: %w", port, err)
148 }
149
150 return ln, PortStatusBound, nil
151}
152
153// SafeListenUDP attempts to bind a UDP packet connection. Handles EADDRINUSE,
154// EACCES, and protected ports gracefully.
155func (s *SafetyChecker) SafeListenUDP(bindIP string, port int) (net.PacketConn, PortBindStatus, error) {
156 // Strict Port Safety Policy: protect operational ports (22, 443, 4444-4449, 50050)
157 if IsProtectedPort(port) {
158 s.logger.Warnf("Port %d/UDP is a strictly protected operational port (%s); skipping gracefully without disruption (operational safety rule)", port, ProtectedPorts[port])
159 return nil, PortStatusOccupiedSkipped, nil
160 }
161
162 addr := net.JoinHostPort(bindIP, strconv.Itoa(port))
163
164 pc, err := net.ListenPacket("udp", addr)
165 if err != nil {
166 if errors.Is(err, syscall.EADDRINUSE) || strings.Contains(err.Error(), "address already in use") {
167 s.logger.Warnf("Port %d/UDP in use (EADDRINUSE); skipping gracefully without disruption", port)
168 return nil, PortStatusOccupiedSkipped, nil
169 }
170 if errors.Is(err, syscall.EACCES) || strings.Contains(err.Error(), "permission denied") {
171 s.logger.Warnf("Port %d/UDP requires elevated privileges (cap_net_bind_service); skipping gracefully", port)
172 return nil, PortStatusPermissionDenied, nil
173 }
174 return nil, PortStatusFailed, fmt.Errorf("failed to bind UDP port %d: %w", port, err)
175 }
176
177 return pc, PortStatusBound, nil
178}