● 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 / common / ports.go
← Back to directory
96 lines · 2.4 KB
Raw
1package common
2
3import (
4 "errors"
5 "fmt"
6 "sort"
7 "strconv"
8 "strings"
9)
10
11// ParsePortsField parses preset names ('quick', 'standard', 'full') or
12// comma-separated lists and ranges ('80,443,8000-8100').
13// It returns a deduplicated, sorted slice of valid port numbers (1-65535).
14func ParsePortsField(ports string) ([]int, error) {
15 p := strings.ToLower(strings.TrimSpace(ports))
16 if p == "" {
17 return nil, errors.New("empty port specification")
18 }
19
20 switch p {
21 case PortPresetQuick:
22 out := make([]int, len(QuickPorts))
23 copy(out, QuickPorts)
24 sort.Ints(out)
25 return out, nil
26 case PortPresetStandard:
27 out := make([]int, len(StandardPorts))
28 copy(out, StandardPorts)
29 sort.Ints(out)
30 return out, nil
31 case PortPresetFull:
32 out := make([]int, PortMax)
33 for i := 0; i < PortMax; i++ {
34 out[i] = i + 1
35 }
36 return out, nil
37 }
38
39 portSet := make(map[int]struct{})
40 chunks := strings.Split(p, ",")
41 validChunks := 0
42 for _, chunk := range chunks {
43 chunk = strings.TrimSpace(chunk)
44 if chunk == "" {
45 continue
46 }
47 validChunks++
48
49 if strings.Contains(chunk, "-") {
50 parts := strings.Split(chunk, "-")
51 if len(parts) != 2 {
52 return nil, fmt.Errorf("invalid port range syntax: '%s'", chunk)
53 }
54 startStr := strings.TrimSpace(parts[0])
55 endStr := strings.TrimSpace(parts[1])
56 if startStr == "" || endStr == "" {
57 return nil, fmt.Errorf("invalid port range syntax: '%s'", chunk)
58 }
59
60 start, err1 := strconv.Atoi(startStr)
61 end, err2 := strconv.Atoi(endStr)
62 if err1 != nil || err2 != nil {
63 return nil, fmt.Errorf("invalid port number in range: '%s'", chunk)
64 }
65 if start < PortMin || end > PortMax {
66 return nil, fmt.Errorf("port range boundary out of bounds [1-65535]: '%s'", chunk)
67 }
68 if start > end {
69 return nil, fmt.Errorf("inverted port range [%d > %d]: '%s'", start, end, chunk)
70 }
71 for pt := start; pt <= end; pt++ {
72 portSet[pt] = struct{}{}
73 }
74 } else {
75 pt, err := strconv.Atoi(chunk)
76 if err != nil {
77 return nil, fmt.Errorf("invalid port number: '%s'", chunk)
78 }
79 if pt < PortMin || pt > PortMax {
80 return nil, fmt.Errorf("port number out of bounds [1-65535]: %d", pt)
81 }
82 portSet[pt] = struct{}{}
83 }
84 }
85
86 if validChunks == 0 || len(portSet) == 0 {
87 return nil, errors.New("no valid ports found in specification")
88 }
89
90 result := make([]int, 0, len(portSet))
91 for pt := range portSet {
92 result = append(result, pt)
93 }
94 sort.Ints(result)
95 return result, nil
96}