- Notifications
You must be signed in to change notification settings - Fork109
/
Copy pathxml.go
451 lines (382 loc) · 14.9 KB
/
xml.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package nmap
import (
"bytes"
"encoding/xml"
"io"
"os"
"strconv"
"time"
family"github.com/Ullaakut/nmap/v3/pkg/osfamilies"
)
// Run represents an nmap scanning run.
typeRunstruct {
XMLName xml.Name`xml:"nmaprun"`
Argsstring`xml:"args,attr" json:"args"`
ProfileNamestring`xml:"profile_name,attr" json:"profile_name"`
Scannerstring`xml:"scanner,attr" json:"scanner"`
StartStrstring`xml:"startstr,attr" json:"start_str"`
Versionstring`xml:"version,attr" json:"version"`
XMLOutputVersionstring`xml:"xmloutputversion,attr" json:"xml_output_version"`
DebuggingDebugging`xml:"debugging" json:"debugging"`
StatsStats`xml:"runstats" json:"run_stats"`
ScanInfoScanInfo`xml:"scaninfo" json:"scan_info"`
StartTimestamp`xml:"start,attr" json:"start"`
VerboseVerbose`xml:"verbose" json:"verbose"`
Hosts []Host`xml:"host" json:"hosts"`
PostScripts []Script`xml:"postscript>script" json:"post_scripts"`
PreScripts []Script`xml:"prescript>script" json:"pre_scripts"`
Targets []Target`xml:"target" json:"targets"`
TaskBegin []Task`xml:"taskbegin" json:"task_begin"`
TaskProgress []TaskProgress`xml:"taskprogress" json:"task_progress"`
TaskEnd []Task`xml:"taskend" json:"task_end"`
NmapErrors []string
rawXML []byte
}
// ToFile writes a Run as XML into the specified file path.
func (rRun)ToFile(filePathstring)error {
file,err:=os.OpenFile(filePath,os.O_WRONLY|os.O_CREATE,0644)
iferr!=nil {
returnerr
}
_,err=file.Write(r.rawXML)
iferr!=nil {
returnerr
}
returnerr
}
// ToReader writes the raw XML into an streamable buffer.
func (rRun)ToReader() io.Reader {
returnbytes.NewReader(r.rawXML)
}
func (r*Run)FromFile(filenamestring)error {
readFile,err:=os.ReadFile(filename)
iferr!=nil {
returnerr
}
returnParse(readFile,r)
}
// ScanInfo represents the scan information.
typeScanInfostruct {
NumServicesint`xml:"numservices,attr" json:"num_services"`
Protocolstring`xml:"protocol,attr" json:"protocol"`
ScanFlagsstring`xml:"scanflags,attr" json:"scan_flags"`
Servicesstring`xml:"services,attr" json:"services"`
Typestring`xml:"type,attr" json:"type"`
}
// Verbose contains the verbosity level of the scan.
typeVerbosestruct {
Levelint`xml:"level,attr" json:"level"`
}
// Debugging contains the debugging level of the scan.
typeDebuggingstruct {
Levelint`xml:"level,attr" json:"level"`
}
// Task contains information about a task.
typeTaskstruct {
TimeTimestamp`xml:"time,attr" json:"time"`
Taskstring`xml:"task,attr" json:"task"`
ExtraInfostring`xml:"extrainfo,attr" json:"extra_info"`
}
// TaskProgress contains information about the progression of a task.
typeTaskProgressstruct {
Percentfloat32`xml:"percent,attr" json:"percent"`
Remainingint`xml:"remaining,attr" json:"remaining"`
Taskstring`xml:"task,attr" json:"task"`
EtcTimestamp`xml:"etc,attr" json:"etc"`
TimeTimestamp`xml:"time,attr" json:"time"`
}
// Target represents a target, how it was specified when passed to nmap,
// its status and the reason for its status. Example:
// <target specification="domain.does.not.exist" status="skipped" reason="invalid"/>
typeTargetstruct {
Specificationstring`xml:"specification,attr" json:"specification"`
Statusstring`xml:"status,attr" json:"status"`
Reasonstring`xml:"reason,attr" json:"reason"`
}
// Host represents a host that was scanned.
typeHoststruct {
DistanceDistance`xml:"distance" json:"distance"`
EndTimeTimestamp`xml:"endtime,attr,omitempty" json:"end_time"`
IPIDSequenceIPIDSequence`xml:"ipidsequence" json:"ip_id_sequence"`
OSOS`xml:"os" json:"os"`
StartTimeTimestamp`xml:"starttime,attr,omitempty" json:"start_time"`
TimedOutbool`xml:"timedout,attr,omitempty" json:"timed_out"`
StatusStatus`xml:"status" json:"status"`
TCPSequenceTCPSequence`xml:"tcpsequence" json:"tcp_sequence"`
TCPTSSequenceTCPTSSequence`xml:"tcptssequence" json:"tcp_ts_sequence"`
TimesTimes`xml:"times" json:"times"`
TraceTrace`xml:"trace" json:"trace"`
UptimeUptime`xml:"uptime" json:"uptime"`
Commentstring`xml:"comment,attr" json:"comment"`
Addresses []Address`xml:"address" json:"addresses"`
ExtraPorts []ExtraPort`xml:"ports>extraports" json:"extra_ports"`
Hostnames []Hostname`xml:"hostnames>hostname" json:"hostnames"`
HostScripts []Script`xml:"hostscript>script" json:"host_scripts"`
Ports []Port`xml:"ports>port" json:"ports"`
Smurfs []Smurf`xml:"smurf" json:"smurfs"`
}
// Status represents a host's status.
typeStatusstruct {
Statestring`xml:"state,attr" json:"state"`
Reasonstring`xml:"reason,attr" json:"reason"`
ReasonTTLfloat32`xml:"reason_ttl,attr" json:"reason_ttl"`
}
func (sStatus)String()string {
returns.State
}
// Address contains a IPv4 or IPv6 address for a host.
typeAddressstruct {
Addrstring`xml:"addr,attr" json:"addr"`
AddrTypestring`xml:"addrtype,attr" json:"addr_type"`
Vendorstring`xml:"vendor,attr" json:"vendor"`
}
func (aAddress)String()string {
returna.Addr
}
// Hostname is a name for a host.
typeHostnamestruct {
Namestring`xml:"name,attr" json:"name"`
Typestring`xml:"type,attr" json:"type"`
}
func (hHostname)String()string {
returnh.Name
}
// Smurf contains responses from a smurf attack.
typeSmurfstruct {
Responsesstring`xml:"responses,attr" json:"responses"`
}
// ExtraPort contains the information about the closed and filtered ports.
typeExtraPortstruct {
Statestring`xml:"state,attr" json:"state"`
Countint`xml:"count,attr" json:"count"`
Reasons []Reason`xml:"extrareasons" json:"reasons"`
}
// Reason represents a reason why a port is closed or filtered.
// This won't be in the scan results unless WithReason is used.
typeReasonstruct {
Reasonstring`xml:"reason,attr" json:"reason"`
Countint`xml:"count,attr" json:"count"`
}
// Port contains all the information about a scanned port.
typePortstruct {
IDuint16`xml:"portid,attr" json:"id"`
Protocolstring`xml:"protocol,attr" json:"protocol"`
OwnerOwner`xml:"owner" json:"owner"`
ServiceService`xml:"service" json:"service"`
StateState`xml:"state" json:"state"`
Scripts []Script`xml:"script" json:"scripts"`
}
// PortStatus represents a port's state.
typePortStatusstring
// Enumerates the different possible state values.
const (
OpenPortStatus="open"
ClosedPortStatus="closed"
FilteredPortStatus="filtered"
UnfilteredPortStatus="unfiltered"
)
// Status returns the status of a port.
func (pPort)Status()PortStatus {
returnPortStatus(p.State.State)
}
// State contains information about a given port's status.
// State will be open, closed, etc.
typeStatestruct {
Statestring`xml:"state,attr" json:"state"`
Reasonstring`xml:"reason,attr" json:"reason"`
ReasonIPstring`xml:"reason_ip,attr" json:"reason_ip"`
ReasonTTLfloat32`xml:"reason_ttl,attr" json:"reason_ttl"`
}
func (sState)String()string {
returns.State
}
// Owner contains the name of a port's owner.
typeOwnerstruct {
Namestring`xml:"name,attr" json:"name"`
}
func (oOwner)String()string {
returno.Name
}
// Service contains detailed information about a service on an open port.
typeServicestruct {
DeviceTypestring`xml:"devicetype,attr" json:"device_type"`
ExtraInfostring`xml:"extrainfo,attr" json:"extra_info"`
HighVersionstring`xml:"highver,attr" json:"high_version"`
Hostnamestring`xml:"hostname,attr" json:"hostname"`
LowVersionstring`xml:"lowver,attr" json:"low_version"`
Methodstring`xml:"method,attr" json:"method"`
Namestring`xml:"name,attr" json:"name"`
OSTypestring`xml:"ostype,attr" json:"os_type"`
Productstring`xml:"product,attr" json:"product"`
Protostring`xml:"proto,attr" json:"proto"`
RPCNumstring`xml:"rpcnum,attr" json:"rpc_num"`
ServiceFPstring`xml:"servicefp,attr" json:"service_fp"`
Tunnelstring`xml:"tunnel,attr" json:"tunnel"`
Versionstring`xml:"version,attr" json:"version"`
Confidenceint`xml:"conf,attr" json:"confidence"`
CPEs []CPE`xml:"cpe" json:"cpes"`
}
func (sService)String()string {
returns.Name
}
// CPE (Common Platform Enumeration) is a standardized way to name software
// applications, operating systems and hardware platforms.
typeCPEstring
// Script represents an Nmap Scripting Engine script.
// The inner elements can be an arbitrary collection of Tables and Elements. Both of them can also be empty.
typeScriptstruct {
IDstring`xml:"id,attr" json:"id"`
Outputstring`xml:"output,attr" json:"output"`
Elements []Element`xml:"elem,omitempty" json:"elements,omitempty"`
Tables []Table`xml:"table,omitempty" json:"tables,omitempty"`
}
// Table is an arbitrary collection of (sub-)Tables and Elements. All its fields can be empty.
typeTablestruct {
Keystring`xml:"key,attr,omitempty" json:"key,omitempty"`
Tables []Table`xml:"table,omitempty" json:"tables,omitempty"`
Elements []Element`xml:"elem,omitempty" json:"elements,omitempty"`
}
// Element is the smallest building block for scripts/tables. It can optionally(!) have a key.
typeElementstruct {
Keystring`xml:"key,attr,omitempty" json:"key,omitempty"`
Valuestring`xml:",innerxml" json:"value"`
}
// OS contains the fingerprinted operating system for a host.
typeOSstruct {
PortsUsed []PortUsed`xml:"portused" json:"ports_used"`
Matches []OSMatch`xml:"osmatch" json:"os_matches"`
Fingerprints []OSFingerprint`xml:"osfingerprint" json:"os_fingerprints"`
}
// PortUsed is the port used to fingerprint an operating system.
typePortUsedstruct {
Statestring`xml:"state,attr" json:"state"`
Protostring`xml:"proto,attr" json:"proto"`
IDint`xml:"portid,attr" json:"port_id"`
}
// OSMatch contains detailed information regarding an operating system fingerprint.
typeOSMatchstruct {
Namestring`xml:"name,attr" json:"name"`
Accuracyint`xml:"accuracy,attr" json:"accuracy"`
Lineint`xml:"line,attr" json:"line"`
Classes []OSClass`xml:"osclass" json:"os_classes"`
}
// OSClass contains vendor information about an operating system.
typeOSClassstruct {
Vendorstring`xml:"vendor,attr" json:"vendor"`
OSGenerationstring`xml:"osgen,attr" json:"os_generation"`
Typestring`xml:"type,attr" json:"type"`
Accuracyint`xml:"accuracy,attr" json:"accuracy"`
Familystring`xml:"osfamily,attr" json:"os_family"`
CPEs []CPE`xml:"cpe" json:"cpes"`
}
// OSFamily returns the OS family in an enumerated format.
func (oOSClass)OSFamily() family.OSFamily {
returnfamily.OSFamily(o.Family)
}
// OSFingerprint is the actual fingerprint string of an operating system.
typeOSFingerprintstruct {
Fingerprintstring`xml:"fingerprint,attr" json:"fingerprint"`
}
// Distance is the amount of hops to a particular host.
typeDistancestruct {
Valueint`xml:"value,attr" json:"value"`
}
// Uptime is the amount of time the host has been up.
typeUptimestruct {
Secondsint`xml:"seconds,attr" json:"seconds"`
Lastbootstring`xml:"lastboot,attr" json:"last_boot"`
}
// Sequence represents a detected sequence.
typeSequencestruct {
Classstring`xml:"class,attr" json:"class"`
Valuesstring`xml:"values,attr" json:"values"`
}
// TCPSequence represents a detected TCP sequence.
typeTCPSequencestruct {
Indexint`xml:"index,attr" json:"index"`
Difficultystring`xml:"difficulty,attr" json:"difficulty"`
Valuesstring`xml:"values,attr" json:"values"`
}
// IPIDSequence represents a detected IP ID sequence.
typeIPIDSequenceSequence
// TCPTSSequence represents a detected TCP TS sequence.
typeTCPTSSequenceSequence
// Trace represents the trace to a host, including the hops.
typeTracestruct {
Protostring`xml:"proto,attr" json:"proto"`
Portint`xml:"port,attr" json:"port"`
Hops []Hop`xml:"hop" json:"hops"`
}
// Hop is an IP hop to a host.
typeHopstruct {
TTLfloat32`xml:"ttl,attr" json:"ttl"`
RTTstring`xml:"rtt,attr" json:"rtt"`
IPAddrstring`xml:"ipaddr,attr" json:"ip_addr"`
Hoststring`xml:"host,attr" json:"host"`
}
// Times contains time statistics for an nmap scan.
typeTimesstruct {
SRTTstring`xml:"srtt,attr" json:"srtt"`
RTTstring`xml:"rttvar,attr" json:"rttv"`
Tostring`xml:"to,attr" json:"to"`
}
// Stats contains statistics for an nmap scan.
typeStatsstruct {
FinishedFinished`xml:"finished" json:"finished"`
HostsHostStats`xml:"hosts" json:"hosts"`
}
// Finished contains detailed statistics regarding a finished scan.
typeFinishedstruct {
TimeTimestamp`xml:"time,attr" json:"time"`
TimeStrstring`xml:"timestr,attr" json:"time_str"`
Elapsedfloat32`xml:"elapsed,attr" json:"elapsed"`
Summarystring`xml:"summary,attr" json:"summary"`
Exitstring`xml:"exit,attr" json:"exit"`
ErrorMsgstring`xml:"errormsg,attr" json:"error_msg"`
}
// HostStats contains the amount of up and down hosts and the total count.
typeHostStatsstruct {
Upint`xml:"up,attr" json:"up"`
Downint`xml:"down,attr" json:"down"`
Totalint`xml:"total,attr" json:"total"`
}
// Timestamp represents time as a UNIX timestamp in seconds.
typeTimestamp time.Time
// ParseTime converts a UNIX timestamp string to a time.Time.
func (t*Timestamp)ParseTime(sstring)error {
timestamp,err:=strconv.ParseInt(s,10,64)
iferr!=nil {
returnerr
}
*t=Timestamp(time.Unix(timestamp,0))
returnnil
}
// FormatTime formats the time.Time value as a UNIX timestamp string.
func (tTimestamp)FormatTime()string {
returnstrconv.FormatInt(time.Time(t).Unix(),10)
}
// MarshalJSON implements the json.Marshaler interface.
func (tTimestamp)MarshalJSON() ([]byte,error) {
return []byte(t.FormatTime()),nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (t*Timestamp)UnmarshalJSON(b []byte)error {
returnt.ParseTime(string(b))
}
// MarshalXMLAttr implements the xml.MarshalerAttr interface.
func (tTimestamp)MarshalXMLAttr(name xml.Name) (xml.Attr,error) {
iftime.Time(t).IsZero() {
return xml.Attr{},nil
}
return xml.Attr{Name:name,Value:t.FormatTime()},nil
}
// UnmarshalXMLAttr implements the xml.UnmarshalXMLAttr interface.
func (t*Timestamp)UnmarshalXMLAttr(attr xml.Attr) (errerror) {
returnt.ParseTime(attr.Value)
}
// Parse takes a byte array of nmap xml data and unmarshal it into a Run struct.
funcParse(content []byte,result*Run)error {
result.rawXML=content
err:=xml.Unmarshal(content,result)
returnerr
}