Skip to content

Commit

Permalink
capture list of ports
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaGolomozy committed Jun 7, 2021
1 parent 22f0b09 commit b9063d5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
25 changes: 15 additions & 10 deletions capture/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net"
"os"
"runtime"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -47,7 +48,7 @@ type Listener struct {
Reading chan bool // this channel is closed when the listener has started reading packets
PcapOptions
Engine EngineType
port uint16 // src or/and dst port
ports []uint16 // src or/and dst ports
trackResponse bool

host string // pcap file name or interface (name, hardware addr, index or ip address)
Expand Down Expand Up @@ -98,11 +99,11 @@ func (eng *EngineType) String() (e string) {
// NewListener creates and initialize a new Listener. if transport or/and engine are invalid/unsupported
// is "tcp" and "pcap", are assumed. l.Engine and l.Transport can help to get the values used.
// if there is an error it will be associated with getting network interfaces
func NewListener(host string, port uint16, transport string, engine EngineType, trackResponse bool) (l *Listener, err error) {
func NewListener(host string, ports []uint16, transport string, engine EngineType, trackResponse bool) (l *Listener, err error) {
l = &Listener{}

l.host = host
l.port = port
l.ports = ports
l.Transport = "tcp"
if transport != "" {
l.Transport = transport
Expand Down Expand Up @@ -170,13 +171,17 @@ func (l *Listener) ListenBackground(ctx context.Context, handler PacketHandler)
func (l *Listener) Filter(ifi net.Interface) (filter string) {
// https://www.tcpdump.org/manpages/pcap-filter.7.html

port := fmt.Sprintf("portrange 0-%d", 1<<16-1)
if l.port != 0 {
port = fmt.Sprintf("port %d", l.port)
}
filter = fmt.Sprintf("%s dst %s", l.Transport, port)
if l.trackResponse {
filter = fmt.Sprintf("%s %s", l.Transport, port)
filter = fmt.Sprintf("%s portrange 0-%d", l.Transport, 1<<16-1)
if l.ports[0] != 0 {
var filters []string
for _, port := range l.ports {
if l.trackResponse {
filters = append(filters, fmt.Sprintf("%s port %d", l.Transport, port))
} else {
filters = append(filters, fmt.Sprintf("%s dst port %d", l.Transport, port))
}
}
filter = strings.Join(filters, " or ")
}

if listenAll(l.host) || isDevice(l.host, ifi) {
Expand Down
33 changes: 20 additions & 13 deletions input_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"net"
"strconv"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -61,7 +62,7 @@ type RAWInputConfig struct {
Stats bool `json:"input-raw-stats"`
quit chan bool // Channel used only to indicate goroutine should shutdown
host string
port uint16
ports []uint16
}

// RAWInput used for intercepting traffic for given address
Expand All @@ -81,22 +82,28 @@ func NewRAWInput(address string, config RAWInputConfig) (i *RAWInput) {
i.RAWInputConfig = config
i.message = make(chan *tcp.Message, 10000)
i.quit = make(chan bool)
var host, _port string
var err error
var port int
host, _port, err = net.SplitHostPort(address)

host, _ports, err := net.SplitHostPort(address)
if err != nil {
log.Fatalf("input-raw: error while parsing address: %s", err)
}
if _port != "" {
port, err = strconv.Atoi(_port)
}

if err != nil {
log.Fatalf("parsing port error: %v", err)
var ports []uint16
if _ports != "" {
portsStr := strings.Split(_ports, ",")

for _, portStr := range portsStr {
port, err := strconv.Atoi(strings.TrimSpace(portStr))
if err != nil {
log.Fatalf("parsing port error: %v", err)
}
ports = append(ports, uint16(port))

}
}

i.host = host
i.port = uint16(port)
i.ports = ports

i.listen(address)

Expand Down Expand Up @@ -141,7 +148,7 @@ func (i *RAWInput) PluginRead() (*Message, error) {

func (i *RAWInput) listen(address string) {
var err error
i.listener, err = capture.NewListener(i.host, i.port, "", i.Engine, i.TrackResponse)
i.listener, err = capture.NewListener(i.host, i.ports, "", i.Engine, i.TrackResponse)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -172,7 +179,7 @@ func (i *RAWInput) messageEmitter(m *tcp.Message) {
}

func (i *RAWInput) String() string {
return fmt.Sprintf("Intercepting traffic from: %s:%d", i.host, i.port)
return fmt.Sprintf("Intercepting traffic from: %s:%s", i.host, strings.Join(strings.Fields(fmt.Sprint(i.ports)), ","))
}

// GetStats returns the stats so far and reset the stats
Expand Down

0 comments on commit b9063d5

Please sign in to comment.