diff --git a/providers/nmap/README.md b/providers/nmap/README.md index e869f4618..ce6537c3d 100644 --- a/providers/nmap/README.md +++ b/providers/nmap/README.md @@ -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: { @@ -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" diff --git a/providers/nmap/connection/connection.go b/providers/nmap/connection/connection.go index a9dfbb350..4c778d545 100644 --- a/providers/nmap/connection/connection.go +++ b/providers/nmap/connection/connection.go @@ -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" ) diff --git a/providers/nmap/resources/discovery.go b/providers/nmap/resources/discovery.go index 7da4dc1e2..64c40117f 100644 --- a/providers/nmap/resources/discovery.go +++ b/providers/nmap/resources/discovery.go @@ -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) diff --git a/providers/nmap/resources/network.go b/providers/nmap/resources/network.go new file mode 100644 index 000000000..f453dd139 --- /dev/null +++ b/providers/nmap/resources/network.go @@ -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() +} diff --git a/providers/nmap/resources/nmap.go b/providers/nmap/resources/nmap.go index 3b7c77e7c..1118f382a 100644 --- a/providers/nmap/resources/nmap.go +++ b/providers/nmap/resources/nmap.go @@ -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 // @@ -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() -} diff --git a/providers/nmap/resources/nmap.lr b/providers/nmap/resources/nmap.lr index ade336f71..8f836672f 100644 --- a/providers/nmap/resources/nmap.lr +++ b/providers/nmap/resources/nmap.lr @@ -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 diff --git a/providers/nmap/resources/nmap.lr.go b/providers/nmap/resources/nmap.lr.go index 780801f0f..9cc4b3a34 100644 --- a/providers/nmap/resources/nmap.lr.go +++ b/providers/nmap/resources/nmap.lr.go @@ -22,9 +22,9 @@ func init() { // to override args, implement: initNmap(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) Create: createNmap, }, - "nmap.target": { - Init: initNmapTarget, - Create: createNmapTarget, + "nmap.network": { + Init: initNmapNetwork, + Create: createNmapNetwork, }, "nmap.host": { Init: initNmapHost, @@ -109,14 +109,14 @@ var getDataFields = map[string]func(r plugin.Resource) *plugin.DataRes{ "nmap.version": func(r plugin.Resource) *plugin.DataRes { return (r.(*mqlNmap).GetVersion()).ToDataRes(types.Resource("nmap.versionInformation")) }, - "nmap.target.target": func(r plugin.Resource) *plugin.DataRes { - return (r.(*mqlNmapTarget).GetTarget()).ToDataRes(types.String) + "nmap.network.target": func(r plugin.Resource) *plugin.DataRes { + return (r.(*mqlNmapNetwork).GetTarget()).ToDataRes(types.String) }, - "nmap.target.hosts": func(r plugin.Resource) *plugin.DataRes { - return (r.(*mqlNmapTarget).GetHosts()).ToDataRes(types.Array(types.Resource("nmap.host"))) + "nmap.network.hosts": func(r plugin.Resource) *plugin.DataRes { + return (r.(*mqlNmapNetwork).GetHosts()).ToDataRes(types.Array(types.Resource("nmap.host"))) }, - "nmap.target.warnings": func(r plugin.Resource) *plugin.DataRes { - return (r.(*mqlNmapTarget).GetWarnings()).ToDataRes(types.Array(types.String)) + "nmap.network.warnings": func(r plugin.Resource) *plugin.DataRes { + return (r.(*mqlNmapNetwork).GetWarnings()).ToDataRes(types.Array(types.String)) }, "nmap.host.name": func(r plugin.Resource) *plugin.DataRes { return (r.(*mqlNmapHost).GetName()).ToDataRes(types.String) @@ -204,20 +204,20 @@ var setDataFields = map[string]func(r plugin.Resource, v *llx.RawData) bool { r.(*mqlNmap).Version, ok = plugin.RawToTValue[*mqlNmapVersionInformation](v.Value, v.Error) return }, - "nmap.target.__id": func(r plugin.Resource, v *llx.RawData) (ok bool) { - r.(*mqlNmapTarget).__id, ok = v.Value.(string) + "nmap.network.__id": func(r plugin.Resource, v *llx.RawData) (ok bool) { + r.(*mqlNmapNetwork).__id, ok = v.Value.(string) return }, - "nmap.target.target": func(r plugin.Resource, v *llx.RawData) (ok bool) { - r.(*mqlNmapTarget).Target, ok = plugin.RawToTValue[string](v.Value, v.Error) + "nmap.network.target": func(r plugin.Resource, v *llx.RawData) (ok bool) { + r.(*mqlNmapNetwork).Target, ok = plugin.RawToTValue[string](v.Value, v.Error) return }, - "nmap.target.hosts": func(r plugin.Resource, v *llx.RawData) (ok bool) { - r.(*mqlNmapTarget).Hosts, ok = plugin.RawToTValue[[]interface{}](v.Value, v.Error) + "nmap.network.hosts": func(r plugin.Resource, v *llx.RawData) (ok bool) { + r.(*mqlNmapNetwork).Hosts, ok = plugin.RawToTValue[[]interface{}](v.Value, v.Error) return }, - "nmap.target.warnings": func(r plugin.Resource, v *llx.RawData) (ok bool) { - r.(*mqlNmapTarget).Warnings, ok = plugin.RawToTValue[[]interface{}](v.Value, v.Error) + "nmap.network.warnings": func(r plugin.Resource, v *llx.RawData) (ok bool) { + r.(*mqlNmapNetwork).Warnings, ok = plugin.RawToTValue[[]interface{}](v.Value, v.Error) return }, "nmap.host.__id": func(r plugin.Resource, v *llx.RawData) (ok bool) { @@ -405,19 +405,19 @@ func (c *mqlNmap) GetVersion() *plugin.TValue[*mqlNmapVersionInformation] { }) } -// mqlNmapTarget for the nmap.target resource -type mqlNmapTarget struct { +// mqlNmapNetwork for the nmap.network resource +type mqlNmapNetwork struct { MqlRuntime *plugin.Runtime __id string - // optional: if you define mqlNmapTargetInternal it will be used here + // optional: if you define mqlNmapNetworkInternal it will be used here Target plugin.TValue[string] Hosts plugin.TValue[[]interface{}] Warnings plugin.TValue[[]interface{}] } -// createNmapTarget creates a new instance of this resource -func createNmapTarget(runtime *plugin.Runtime, args map[string]*llx.RawData) (plugin.Resource, error) { - res := &mqlNmapTarget{ +// createNmapNetwork creates a new instance of this resource +func createNmapNetwork(runtime *plugin.Runtime, args map[string]*llx.RawData) (plugin.Resource, error) { + res := &mqlNmapNetwork{ MqlRuntime: runtime, } @@ -434,7 +434,7 @@ func createNmapTarget(runtime *plugin.Runtime, args map[string]*llx.RawData) (pl } if runtime.HasRecording { - args, err = runtime.ResourceFromRecording("nmap.target", res.__id) + args, err = runtime.ResourceFromRecording("nmap.network", res.__id) if err != nil || args == nil { return res, err } @@ -444,22 +444,22 @@ func createNmapTarget(runtime *plugin.Runtime, args map[string]*llx.RawData) (pl return res, nil } -func (c *mqlNmapTarget) MqlName() string { - return "nmap.target" +func (c *mqlNmapNetwork) MqlName() string { + return "nmap.network" } -func (c *mqlNmapTarget) MqlID() string { +func (c *mqlNmapNetwork) MqlID() string { return c.__id } -func (c *mqlNmapTarget) GetTarget() *plugin.TValue[string] { +func (c *mqlNmapNetwork) GetTarget() *plugin.TValue[string] { return &c.Target } -func (c *mqlNmapTarget) GetHosts() *plugin.TValue[[]interface{}] { +func (c *mqlNmapNetwork) GetHosts() *plugin.TValue[[]interface{}] { return plugin.GetOrCompute[[]interface{}](&c.Hosts, func() ([]interface{}, error) { if c.MqlRuntime.HasRecording { - d, err := c.MqlRuntime.FieldResourceFromRecording("nmap.target", c.__id, "hosts") + d, err := c.MqlRuntime.FieldResourceFromRecording("nmap.network", c.__id, "hosts") if err != nil { return nil, err } @@ -472,7 +472,7 @@ func (c *mqlNmapTarget) GetHosts() *plugin.TValue[[]interface{}] { }) } -func (c *mqlNmapTarget) GetWarnings() *plugin.TValue[[]interface{}] { +func (c *mqlNmapNetwork) GetWarnings() *plugin.TValue[[]interface{}] { return plugin.GetOrCompute[[]interface{}](&c.Warnings, func() ([]interface{}, error) { return c.warnings() }) diff --git a/providers/nmap/resources/nmap.lr.manifest.yaml b/providers/nmap/resources/nmap.lr.manifest.yaml index 91ee0ec73..09902d310 100755 --- a/providers/nmap/resources/nmap.lr.manifest.yaml +++ b/providers/nmap/resources/nmap.lr.manifest.yaml @@ -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: {} @@ -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: {}