Skip to content

Commit

Permalink
🧹 rename nmap.target to nmap.network
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rock committed Oct 4, 2024
1 parent 6f8670a commit d4657a4
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 117 deletions.
8 changes: 4 additions & 4 deletions providers/nmap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ cnquery shell nmap
*Scan active IP address in network*

```shell
nmap.target("192.168.178.0/24").hosts { name ports { * } }
nmap.target.hosts: [
nmap.network("192.168.178.0/24").hosts { name ports { * } }
nmap.network.hosts: [
0: {
ports: [
0: {
Expand Down Expand Up @@ -73,8 +73,8 @@ nmap.target.hosts: [
*Host scan with specific ip*
```shell
nmap.target(target: "192.168.178.25").hosts { ports }
nmap.target.hosts: [
nmap.network(target: "192.168.178.25").hosts { ports }
nmap.network.hosts: [
0: {
ports: [
0: nmap.port port=5000 service="rtsp"
Expand Down
2 changes: 1 addition & 1 deletion providers/nmap/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package connection

import (
"strings"

"go.mondoo.com/cnquery/v11/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/plugin"
)
Expand Down
4 changes: 2 additions & 2 deletions providers/nmap/resources/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ func Discover(runtime *plugin.Runtime, opts map[string]string) (*inventory.Inven
for i := range networks {
network := networks[i]

targetResource, err := runtime.CreateResource(runtime, "nmap.target ", map[string]*llx.RawData{
targetResource, err := runtime.CreateResource(runtime, "nmap.network ", map[string]*llx.RawData{
"target": llx.StringData(network),
})
if err != nil {
return nil, err
}
hosts := targetResource.(*mqlNmapTarget).GetHosts().Data
hosts := targetResource.(*mqlNmapNetwork).GetHosts().Data
for i := range hosts {
entry := hosts[i]
host := entry.(*mqlNmapHost)
Expand Down
72 changes: 72 additions & 0 deletions providers/nmap/resources/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package resources

import (
"context"
"time"

"github.com/Ullaakut/nmap/v3"
"github.com/cockroachdb/errors"
"go.mondoo.com/cnquery/v11/llx"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/util/convert"
)

func (r *mqlNmapNetwork) id() (string, error) {
return "nmap.target/" + r.Target.Data, nil
}

func initNmapNetwork(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
return args, nil, nil
}

func (r *mqlNmapNetwork) scan() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()

// set default values
r.Hosts = plugin.TValue[[]interface{}]{Data: nil, Error: nil, State: plugin.StateIsSet | plugin.StateIsNull}
r.Warnings = plugin.TValue[[]interface{}]{Data: nil, Error: nil, State: plugin.StateIsSet | plugin.StateIsNull}

if r.Target.Data == "" {
return errors.New("target is required")
}

scanner, err := nmap.NewScanner(
ctx,
nmap.WithConnectScan(),
nmap.WithTimingTemplate(nmap.TimingAggressive),
nmap.WithServiceInfo(),
nmap.WithDisabledDNSResolution(), // -n
nmap.WithTargets(r.Target.Data),
)
if err != nil {
return errors.Wrap(err, "unable to create nmap scanner")
}

result, warnings, err := scanner.Run()

if warnings != nil && len(*warnings) > 0 {
r.Warnings = plugin.TValue[[]interface{}]{Data: convert.SliceAnyToInterface(*warnings), Error: nil, State: plugin.StateIsSet}
}

var hosts []interface{}
for _, host := range result.Hosts {
r, err := newMqlNmapHost(r.MqlRuntime, host)
if err != nil {
return err
}
hosts = append(hosts, r)
}

r.Hosts = plugin.TValue[[]interface{}]{Data: hosts, Error: nil, State: plugin.StateIsSet}

return nil
}

func (r *mqlNmapNetwork) hosts() ([]interface{}, error) {
return nil, r.scan()
}

func (r *mqlNmapNetwork) warnings() ([]interface{}, error) {
return nil, r.scan()
}
71 changes: 0 additions & 71 deletions providers/nmap/resources/nmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@

package resources

import (
"context"
"time"

"github.com/Ullaakut/nmap/v3"
"github.com/cockroachdb/errors"
"go.mondoo.com/cnquery/v11/llx"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/util/convert"
)

// standard nmap scan
// nmap -sT -T4 192.168.178.0/24
//
Expand All @@ -25,63 +14,3 @@ import (
func (r *mqlNmap) id() (string, error) {
return "nmap", nil
}

func (r *mqlNmapTarget) id() (string, error) {
return "nmap.target/" + r.Target.Data, nil
}

func initNmapTarget(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
return args, nil, nil
}

func (r *mqlNmapTarget) scan() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()

// set default values
r.Hosts = plugin.TValue[[]interface{}]{Data: nil, Error: nil, State: plugin.StateIsSet | plugin.StateIsNull}
r.Warnings = plugin.TValue[[]interface{}]{Data: nil, Error: nil, State: plugin.StateIsSet | plugin.StateIsNull}

if r.Target.Data == "" {
return errors.New("target is required")
}

scanner, err := nmap.NewScanner(
ctx,
nmap.WithConnectScan(),
nmap.WithTimingTemplate(nmap.TimingAggressive),
nmap.WithServiceInfo(),
nmap.WithDisabledDNSResolution(), // -n
nmap.WithTargets(r.Target.Data),
)
if err != nil {
return errors.Wrap(err, "unable to create nmap scanner")
}

result, warnings, err := scanner.Run()

if warnings != nil && len(*warnings) > 0 {
r.Warnings = plugin.TValue[[]interface{}]{Data: convert.SliceAnyToInterface(*warnings), Error: nil, State: plugin.StateIsSet}
}

var hosts []interface{}
for _, host := range result.Hosts {
r, err := newMqlNmapHost(r.MqlRuntime, host)
if err != nil {
return err
}
hosts = append(hosts, r)
}

r.Hosts = plugin.TValue[[]interface{}]{Data: hosts, Error: nil, State: plugin.StateIsSet}

return nil
}

func (r *mqlNmapTarget) hosts() ([]interface{}, error) {
return nil, r.scan()
}

func (r *mqlNmapTarget) warnings() ([]interface{}, error) {
return nil, r.scan()
}
4 changes: 2 additions & 2 deletions providers/nmap/resources/nmap.lr
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ nmap {
version() nmap.versionInformation
}

// nmap target
nmap.target {
// nmap network
nmap.network {
init(target string)
// Target IP address or hostname
target string
Expand Down
62 changes: 31 additions & 31 deletions providers/nmap/resources/nmap.lr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions providers/nmap/resources/nmap.lr.manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ resources:
trace: {}
is_private: true
min_mondoo_version: latest
nmap.network:
fields:
hosts: {}
target: {}
warnings: {}
min_mondoo_version: 9.0.0
nmap.port:
fields:
method: {}
Expand All @@ -33,12 +39,6 @@ resources:
version: {}
is_private: true
min_mondoo_version: latest
nmap.target:
fields:
hosts: {}
target: {}
warnings: {}
min_mondoo_version: latest
nmap.version:
fields:
compiledWith: {}
Expand Down

0 comments on commit d4657a4

Please sign in to comment.