Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making pcap handlers shared globally #946

Merged
merged 48 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
7a8f8b5
singleton
Mzack9999 Jan 29, 2024
408ea8c
lint fix
Mzack9999 Jan 29, 2024
d6e46c4
more tests
Mzack9999 Jan 29, 2024
db4cfea
whops
Mzack9999 Jan 29, 2024
dce4d63
active scanning
Mzack9999 Jan 29, 2024
4dc6dfe
.
Mzack9999 Jan 29, 2024
bc18966
singleton source port
Mzack9999 Jan 29, 2024
e7c3163
restoring deps
Mzack9999 Jan 29, 2024
f9fdc68
.
Mzack9999 Jan 29, 2024
9d38a60
.
Mzack9999 Jan 29, 2024
15f374a
tmp unix only singleton
Mzack9999 Jan 29, 2024
80aae14
typo
Mzack9999 Jan 30, 2024
60ef81b
introducing context
Mzack9999 Jan 30, 2024
f486dff
fixing tests
Mzack9999 Jan 30, 2024
5e54f6d
adding drain capabiity via context
Mzack9999 Jan 30, 2024
ba28d40
test coverage
Mzack9999 Jan 30, 2024
30c2490
.
Mzack9999 Jan 30, 2024
192993e
.
Mzack9999 Jan 30, 2024
56213e2
lowering number
Mzack9999 Jan 30, 2024
3d1fa57
.
Mzack9999 Jan 30, 2024
fe818da
.
Mzack9999 Jan 30, 2024
2f963cb
preparing win pcap
Mzack9999 Jan 30, 2024
5980309
deny pcap multi-thread
Mzack9999 Jan 30, 2024
2e76906
single pcap + handlers pool
Mzack9999 Jan 31, 2024
fc812a9
.
Mzack9999 Jan 31, 2024
1300708
tmp: disabling win
Mzack9999 Jan 31, 2024
3d098d1
tmp: skip host discovery
Mzack9999 Jan 31, 2024
8fe6a68
tmp: disable win
Mzack9999 Jan 31, 2024
605fee2
+ host discovery
Mzack9999 Feb 1, 2024
94fa965
+ debug
Mzack9999 Feb 1, 2024
54ced77
+ windows test
Mzack9999 Feb 1, 2024
e8c28e1
win-release
Mzack9999 Feb 1, 2024
fe17808
onreceive callback
Mzack9999 Feb 1, 2024
14fe0a7
making ipv6 optional
Mzack9999 Feb 1, 2024
acaf3a7
updating metrics endpoint
Mzack9999 Feb 1, 2024
9086d3e
output respecting ip-version
Mzack9999 Feb 2, 2024
f4dee54
using const
Mzack9999 Feb 2, 2024
37ee335
defaulting to ipv4
Mzack9999 Feb 2, 2024
02cc809
cli use sdk on receive if not overridden
Mzack9999 Feb 2, 2024
2e8e279
fixing passive scan via onreceive
Mzack9999 Feb 2, 2024
aba872c
fixing connect scan + data race
Mzack9999 Feb 2, 2024
8385a20
moving output handling to runner.New()
Mzack9999 Feb 6, 2024
924c930
filtering packets not in range
Mzack9999 Feb 16, 2024
deb2edd
various perm and checks fixes
Mzack9999 Feb 16, 2024
38cc3f9
lint
Mzack9999 Feb 16, 2024
60bd595
stabilizing output
Mzack9999 Feb 24, 2024
0b749e0
tidy
Mzack9999 Feb 24, 2024
39576b8
.
Mzack9999 Feb 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ updates:
labels:
- "Type: Maintenance"

# # Maintain dependencies for docker
# - package-ecosystem: "docker"
# directory: "/"
# schedule:
# interval: "weekly"
# target-branch: "dev"
# commit-message:
# prefix: "chore"
# include: "scope"
# labels:
# - "Type: Maintenance"
#
# # Maintain dependencies for GitHub Actions
# - package-ecosystem: "github-actions"
# directory: "/"
# schedule:
# interval: "weekly"
# target-branch: "dev"
# commit-message:
# prefix: "chore"
# include: "scope"
# labels:
# - "Type: Maintenance"
# Maintain dependencies for docker
- package-ecosystem: "docker"
directory: "/"
schedule:
interval: "weekly"
target-branch: "dev"
commit-message:
prefix: "chore"
include: "scope"
labels:
- "Type: Maintenance"

# Maintain dependencies for GitHub Actions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
target-branch: "dev"
commit-message:
prefix: "chore"
include: "scope"
labels:
- "Type: Maintenance"
22 changes: 15 additions & 7 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ jobs:
build-linux:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.21.x

- name: Check out code
uses: actions/checkout@v3

- name: Install libpcap-dev
run: sudo apt install libpcap-dev

Expand All @@ -39,10 +39,14 @@ jobs:
run: bash run.sh
working-directory: integration_tests/

- name: Race Condition Tests
- name: Race Condition Tests - Standard User
run: |
go run -race . -host scanme.sh
sudo go run -race . -host scanme.sh
working-directory: v2/cmd/naabu/

- name: Race Condition Tests - Root User
run: |
sudo go run -race . -host scanme.sh -Pn
working-directory: v2/cmd/naabu/

build-mac:
Expand Down Expand Up @@ -76,10 +80,14 @@ jobs:
run: bash run.sh
working-directory: integration_tests/

- name: Race Condition Tests
- name: Race Condition Tests - Standard User
run: |
go run -race . -host scanme.sh
sudo go run -race . -host scanme.sh
working-directory: v2/cmd/naabu/

- name: Race Condition Tests - Root User
run: |
sudo go run -race . -host scanme.sh -Pn
working-directory: v2/cmd/naabu/

build-windows:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ Naabu also supports excluding CDN/WAF IPs being port scanned. If used, only `80`
Currently `cloudflare`, `akamai`, `incapsula` and `sucuri` IPs are supported for exclusions.

# Scan Status
Naabu exposes json scan info on a local port bound to localhost at `http://localhost:63636` (the port can be changed via the `-metrics-port` flag)
Naabu exposes json scan info on a local port bound to localhost at `http://localhost:63636/metrics` (the port can be changed via the `-metrics-port` flag)

# Using naabu as library
The following sample program scan the port `80` of `scanme.sh`. The results are returned via the `OnResult` callback:
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mv integration-test ../../../integration_tests/integration-test
cd ../../../integration_tests
echo "::endgroup::"

./integration-test
sudo ./integration-test
if [ $? -eq 0 ]
then
exit 0
Expand Down
118 changes: 110 additions & 8 deletions v2/cmd/integration-test/library.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package main

import (
"context"
"errors"
"os"
"os/user"

"github.com/projectdiscovery/naabu/v2/internal/testutils"
"github.com/projectdiscovery/naabu/v2/pkg/privileges"
"github.com/projectdiscovery/naabu/v2/pkg/result"
"github.com/projectdiscovery/naabu/v2/pkg/runner"
)

var libraryTestcases = map[string]testutils.TestCase{
"naabu as library": &httpxLibrary{},
"sdk - one passive execution": &naabuPassiveSingleLibrary{},
"sdk - one execution - connect": &naabuSingleLibrary{scanType: "c"},
"sdk - multiple executions - connect": &naabuMultipleExecLibrary{scanType: "c"},
"sdk - one execution - syn": &naabuSingleLibrary{scanType: "s"},
"sdk - multiple executions - syn": &naabuMultipleExecLibrary{scanType: "s"},
}

type httpxLibrary struct {
type naabuPassiveSingleLibrary struct {
}

func (h *httpxLibrary) Execute() error {
func (h *naabuPassiveSingleLibrary) Execute() error {
testFile := "test.txt"
err := os.WriteFile(testFile, []byte("scanme.sh"), 0644)
if err != nil {
Expand All @@ -24,10 +32,11 @@ func (h *httpxLibrary) Execute() error {
defer os.RemoveAll(testFile)

options := runner.Options{
HostsFile: testFile,
Ports: "80",
Passive: true,
OnResult: func(hr *result.HostResult) {},
HostsFile: testFile,
Ports: "80",
Passive: true,
SkipHostDiscovery: true,
OnResult: func(hr *result.HostResult) {},
}

naabuRunner, err := runner.NewRunner(&options)
Expand All @@ -36,5 +45,98 @@ func (h *httpxLibrary) Execute() error {
}
defer naabuRunner.Close()

return naabuRunner.RunEnumeration()
return naabuRunner.RunEnumeration(context.TODO())
}

type naabuSingleLibrary struct {
scanType string
}

func (h *naabuSingleLibrary) Execute() error {
if h.scanType == "s" && !privileges.IsPrivileged {
usr, _ := user.Current()
return errors.New("invalid user" + usr.Name)
}

testFile := "test.txt"
err := os.WriteFile(testFile, []byte("scanme.sh"), 0644)
if err != nil {
return err
}
defer os.RemoveAll(testFile)

var got bool

options := runner.Options{
HostsFile: testFile,
Ports: "80",
SkipHostDiscovery: true,
ScanType: h.scanType,
OnResult: func(hr *result.HostResult) {
got = true
},
WarmUpTime: 2,
}

naabuRunner, err := runner.NewRunner(&options)
if err != nil {
return err
}
defer naabuRunner.Close()

if err = naabuRunner.RunEnumeration(context.TODO()); err != nil {
return err
}
if !got {
return errors.New("no results found")
}

return nil
}

type naabuMultipleExecLibrary struct {
scanType string
}

func (h *naabuMultipleExecLibrary) Execute() error {
if h.scanType == "s" && !privileges.IsPrivileged {
usr, _ := user.Current()
return errors.New("invalid user" + usr.Name)
}

testFile := "test.txt"
err := os.WriteFile(testFile, []byte("scanme.sh"), 0644)
if err != nil {
return err
}
defer os.RemoveAll(testFile)

var got bool

options := runner.Options{
HostsFile: testFile,
Ports: "80",
ScanType: h.scanType,
SkipHostDiscovery: true,
OnResult: func(hr *result.HostResult) {
got = true
},
WarmUpTime: 2,
}

for i := 0; i < 3; i++ {
naabuRunner, err := runner.NewRunner(&options)
if err != nil {
return err
}

if err = naabuRunner.RunEnumeration(context.TODO()); err != nil {
return err
}
if !got {
return errors.New("no results found")
}
naabuRunner.Close()
}
return nil
}
8 changes: 5 additions & 3 deletions v2/cmd/naabu/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package main

import (
"context"
"os"
"os/signal"

_ "github.com/projectdiscovery/fdmax/autofdmax"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/naabu/v2/pkg/runner"
"os"
"os/signal"
)

func main() {
Expand Down Expand Up @@ -35,7 +37,7 @@ func main() {
}
}()

err = naabuRunner.RunEnumeration()
err = naabuRunner.RunEnumeration(context.TODO())
if err != nil {
gologger.Fatal().Msgf("Could not run enumeration: %s\n", err)
}
Expand Down
2 changes: 1 addition & 1 deletion v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/projectdiscovery/naabu/v2
go 1.21

require (
github.com/Mzack9999/gcache v0.0.0-20230410081825-519e28eab057
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2
github.com/google/gopacket v1.1.19
github.com/logrusorgru/aurora v2.0.3+incompatible
Expand Down Expand Up @@ -34,7 +35,6 @@ require (
require (
aead.dev/minisign v0.2.0 // indirect
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/Mzack9999/gcache v0.0.0-20230410081825-519e28eab057 // indirect
github.com/Mzack9999/go-http-digest-auth-client v0.6.1-0.20220414142836-eb8883508809 // indirect
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/akrylysov/pogreb v0.10.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions v2/pkg/result/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"golang.org/x/exp/maps"
)

type ResultFn func(*HostResult)

type HostResult struct {
Host string
IP string
Expand Down
17 changes: 6 additions & 11 deletions v2/pkg/runner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/projectdiscovery/naabu/v2/pkg/privileges"
"github.com/projectdiscovery/naabu/v2/pkg/result"
"github.com/projectdiscovery/naabu/v2/pkg/scan"
fileutil "github.com/projectdiscovery/utils/file"

"github.com/projectdiscovery/goflags"
Expand Down Expand Up @@ -61,7 +62,8 @@ type Options struct {
ProxyAuth string // Socks5 proxy authentication (username:password)
Resolvers string // Resolvers (comma separated or file)
baseResolvers []string
OnResult OnResultCallback // OnResult callback
OnResult result.ResultFn // callback on final host result
OnReceive result.ResultFn // callback on response receive
CSV bool
Resume bool
ResumeCfg *ResumeCfg
Expand Down Expand Up @@ -96,9 +98,6 @@ type Options struct {
MetricsPort int
}

// OnResultCallback (hostResult)
type OnResultCallback func(*result.HostResult)

// ParseOptions parses the command line flags provided by a user
func ParseOptions() *Options {
options := &Options{}
Expand Down Expand Up @@ -141,7 +140,7 @@ func ParseOptions() *Options {

flagSet.CreateGroup("config", "Configuration",
flagSet.BoolVarP(&options.ScanAllIPS, "sa", "scan-all-ips", false, "scan all the IP's associated with DNS record"),
flagSet.StringSliceVarP(&options.IPVersion, "iv", "ip-version", nil, "ip version to scan of hostname (4,6) - (default 4)", goflags.NormalizedStringSliceOptions),
flagSet.StringSliceVarP(&options.IPVersion, "iv", "ip-version", []string{scan.IPv4}, "ip version to scan of hostname (4,6) - (default 4)", goflags.NormalizedStringSliceOptions),
flagSet.StringVarP(&options.ScanType, "s", "scan-type", SynScan, "type of port scan (SYN/CONNECT)"),
flagSet.StringVar(&options.SourceIP, "source-ip", "", "source ip and port (x.x.x.x:yyy)"),
flagSet.BoolVarP(&options.InterfacesList, "il", "interface-list", false, "list available interfaces and public ip"),
Expand Down Expand Up @@ -198,7 +197,7 @@ func ParseOptions() *Options {
flagSet.BoolVar(&options.Version, "version", false, "display version of naabu"),
flagSet.BoolVar(&options.EnableProgressBar, "stats", false, "display stats of the running scan (deprecated)"),
flagSet.IntVarP(&options.StatsInterval, "stats-interval", "si", DefautStatsInterval, "number of seconds to wait between showing a statistics update (deprecated)"),
flagSet.IntVarP(&options.MetricsPort, "metrics-port", "mp", 63636, "port to expose nuclei metrics on"),
flagSet.IntVarP(&options.MetricsPort, "metrics-port", "mp", 63636, "port to expose naabu metrics on"),
)

_ = flagSet.Parse()
Expand All @@ -211,8 +210,6 @@ func ParseOptions() *Options {
// Check if stdin pipe was given
options.Stdin = !options.DisableStdin && fileutil.HasStdin()

// Read the inputs and configure the logging
options.configureOutput()
options.ResumeCfg = NewResumeCfg()
if options.ShouldLoadResume() {
if err := options.ResumeCfg.ConfigureResume(); err != nil {
Expand All @@ -230,9 +227,7 @@ func ParseOptions() *Options {
if !options.DisableUpdateCheck {
latestVersion, err := updateutils.GetToolVersionCallback("naabu", version)()
if err != nil {
if options.Verbose {
gologger.Error().Msgf("naabu version check failed: %v", err.Error())
}
gologger.Verbose().Msgf("naabu version check failed: %v", err.Error())
} else {
gologger.Info().Msgf("Current naabu version %v %v", version, updateutils.GetVersionDescription(version, latestVersion))
}
Expand Down
Loading
Loading