forked from thathaneydude/go-tenable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio-exports.go
187 lines (172 loc) · 7.75 KB
/
io-exports.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package go_tenable
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"time"
)
func (export *Export) GetUnprocessedChunks() []int {
var UnprocessedChunks []int
log.Printf("Chunks Available: %v ; Chunks Processed: %v\n", export.AvailableChunks, export.ProcessedChunks)
for _, chunkId := range export.AvailableChunks {
if !intInSlice(chunkId, export.ProcessedChunks) {
UnprocessedChunks = append(UnprocessedChunks, chunkId)
}
}
return UnprocessedChunks
}
func intInSlice(a int, list []int) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func (export *Export) RequestExport(Payload []byte) string {
resp, err := export.tioClient.Post(fmt.Sprintf("%v/export", export.ExportType), Payload)
tmp, _ := ioutil.ReadAll(resp.Body)
var exportRequestRes ExportRequestResponse
err = json.Unmarshal(tmp, &exportRequestRes)
if err != nil {
log.Printf("Unable to read response from %v export: %v\n", export.ExportType, err)
return ""
}
log.Printf("Export request response [%v]: %v \n", resp.StatusCode, exportRequestRes)
export.ExportUUID = exportRequestRes.ExportUUID
return exportRequestRes.ExportUUID
}
func (export *Export) RequestStatus() string {
resp, err := export.tioClient.Get(fmt.Sprintf("%v/export/%v/status", export.ExportType, export.ExportUUID),
"")
tmp, _ := ioutil.ReadAll(resp.Body)
var statusRes = ExportStatusResponse{}
err = json.Unmarshal(tmp, &statusRes)
if err != nil {
log.Printf("Unable to unmarshal status response: %v\n", err)
return err.Error()
}
export.ExportStatus = statusRes.Status
export.AvailableChunks = statusRes.ChunksAvailable
log.Printf("Export Status: %v\n", export.ExportStatus)
return export.ExportStatus
}
func (export *Export) DownloadChunk(ChunkID int) AssetChunkDownloadResponse {
resp, err := export.tioClient.Get(fmt.Sprintf("%v/export/%v/chunks/%v", export.ExportType,
export.ExportUUID, ChunkID), "")
tmp, _ := ioutil.ReadAll(resp.Body)
var ChunkResponse = AssetChunkDownloadResponse{}
err = json.Unmarshal(tmp, &ChunkResponse)
if err != nil {
log.Printf("Unable to unmarshal asset chunk: %v\n", err)
}
return ChunkResponse
}
type Export struct {
ExportUUID string
ExportType string
ExportStatus string
AvailableChunks []int
ProcessedChunks []int
tioClient TenableIO
}
func (io TenableIO) NewExport(exportType string) Export {
var ret = Export{}
ret.ExportType = exportType
ret.tioClient = io
return ret
}
type ExportRequestResponse struct {
ExportUUID string `json:"export_uuid"`
}
type ExportStatusResponse struct {
Status string `json:"status"`
ChunksAvailable []int `json:"chunks_available"`
}
type AssetChunkDownloadResponse []struct {
ID string `json:"id,omitempty"`
HasAgent bool `json:"has_agent,omitempty"`
HasPluginResults bool `json:"has_plugin_results,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
TerminatedAt time.Time `json:"terminated_at,omitempty"`
TerminatedBy time.Time `json:"terminated_by,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
DeletedAt time.Time `json:"deleted_at,omitempty"`
DeletedBy time.Time `json:"deleted_by,omitempty"`
FirstSeen time.Time `json:"first_seen,omitempty"`
LastSeen time.Time `json:"last_seen,omitempty"`
FirstScanTime time.Time `json:"first_scan_time,omitempty"`
LastScanTime time.Time `json:"last_scan_time,omitempty"`
LastAuthenticatedScanDate time.Time `json:"last_authenticated_scan_date,omitempty"`
LastLicensedScanDate time.Time `json:"last_licensed_scan_date,omitempty"`
AzureVMID string `json:"azure_vm_id,omitempty"`
AzureResourceID string `json:"azure_resource_id,omitempty"`
AwsEc2InstanceAmiID string `json:"aws_ec2_instance_ami_id,omitempty"`
AwsEc2InstanceID string `json:"aws_ec2_instance_id,omitempty"`
AgentUUID string `json:"agent_uuid,omitempty"`
BiosUUID string `json:"bios_uuid,omitempty"`
EnvironmentID string `json:"environment_id,omitempty"`
AwsOwnerID string `json:"aws_owner_id,omitempty"`
AwsAvailabilityZone string `json:"aws_availability_zone,omitempty"`
AwsRegion string `json:"aws_region,omitempty"`
AwsVpcID string `json:"aws_vpc_id,omitempty"`
AwsEc2InstanceGroupName string `json:"aws_ec2_instance_group_name,omitempty"`
AwsEc2InstanceStateName string `json:"aws_ec2_instance_state_name,omitempty"`
AwsEc2InstanceType string `json:"aws_ec2_instance_type,omitempty"`
AwsSubnetID string `json:"aws_subnet_id,omitempty"`
AwsEc2ProductCode string `json:"aws_ec2_product_code,omitempty"`
AwsEc2Name string `json:"aws_ec2_name,omitempty"`
McafeeEpoGUID string `json:"mcafee_epo_guid,omitempty"`
McafeeEpoAgentGUID string `json:"mcafee_epo_agent_guid,omitempty"`
ServicenowSysid string `json:"servicenow_sysid,omitempty"`
AgentNames []string `json:"agent_names,omitempty"`
Ipv4s []string `json:"ipv4s,omitempty"`
Ipv6s []string `json:"ipv6s,omitempty"`
Fqdns []string `json:"fqdns,omitempty"`
MacAddresses []string `json:"mac_addresses,omitempty"`
NetbiosNames []string `json:"netbios_names,omitempty"`
OperatingSystems []string `json:"operating_systems,omitempty"`
SystemTypes []string `json:"system_types,omitempty"`
Hostnames []string `json:"hostnames,omitempty"`
SSHFingerprints []string `json:"ssh_fingerprints,omitempty"`
QualysAssetIds []string `json:"qualys_asset_ids,omitempty"`
QualysHostIds []string `json:"qualys_host_ids,omitempty"`
ManufacturerTpmIds []string `json:"manufacturer_tpm_ids,omitempty"`
SymantecEpHardwareKeys []string `json:"symantec_ep_hardware_keys,omitempty"`
Sources []struct {
Name string `json:"name,omitempty"`
FirstSeen time.Time `json:"first_seen,omitempty"`
LastSeen time.Time `json:"last_seen,omitempty"`
} `json:"sources,omitempty"`
Tags []string `json:"tags,omitempty"`
NetworkInterfaces []struct {
Name string `json:"name,omitempty"`
Virtual bool `json:"virtual,omitempty"`
Aliased bool `json:"aliased,omitempty"`
Fqdns []string `json:"fqdns,omitempty"`
MacAddresses []string `json:"mac_addresses,omitempty"`
Ipv4S []string `json:"ipv4s,omitempty"`
Ipv6S []string `json:"ipv6s,omitempty"`
} `json:"network_interfaces"`
}
type AssetRequestBody struct {
ChunkSize int `json:"chunk_size"`
CreatedAt float32 `json:"filters.created_at,omitempty"`
UpdatedAt float32 `json:"filters.updated_at,omitempty"`
TerminatedAt float32 `json:"filters.terminated_at,omitempty"`
DeletedAt float32 `json:"filters.deleted_at,omitempty"`
FirstScanTime float32 `json:"filters.first_scan_time,omitempty"`
LastAuthenticatedScanTime float32 `json:"filters.last_authenticated_scan_time,omitempty"`
LastAssessed float32 `json:"filters.last_assessed,omitempty"`
ServiceNowSysID bool `json:"filters.servicenow_sysid,omitempty"`
Sources []string `json:"filters.sources,omitempty"`
HasPluginResults bool `json:"filters.has_plugin_results,omitempty"`
}
func (req AssetRequestBody) ToBytes() []byte {
ret, err := json.Marshal(req)
if err != nil {
log.Printf("Unable to marshal request body\n")
}
return ret
}