forked from Ullaakut/nmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml.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.
type Run struct {
XMLName xml.Name `xml:"nmaprun"`
Args string `xml:"args,attr" json:"args"`
ProfileName string `xml:"profile_name,attr" json:"profile_name"`
Scanner string `xml:"scanner,attr" json:"scanner"`
StartStr string `xml:"startstr,attr" json:"start_str"`
Version string `xml:"version,attr" json:"version"`
XMLOutputVersion string `xml:"xmloutputversion,attr" json:"xml_output_version"`
Debugging Debugging `xml:"debugging" json:"debugging"`
Stats Stats `xml:"runstats" json:"run_stats"`
ScanInfo ScanInfo `xml:"scaninfo" json:"scan_info"`
Start Timestamp `xml:"start,attr" json:"start"`
Verbose Verbose `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 (r Run) ToFile(filePath string) error {
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return err
}
_, err = file.Write(r.rawXML)
if err != nil {
return err
}
return err
}
// ToReader writes the raw XML into an streamable buffer.
func (r Run) ToReader() io.Reader {
return bytes.NewReader(r.rawXML)
}
func (r *Run) FromFile(filename string) error {
readFile, err := os.ReadFile(filename)
if err != nil {
return err
}
return Parse(readFile, r)
}
// ScanInfo represents the scan information.
type ScanInfo struct {
NumServices int `xml:"numservices,attr" json:"num_services"`
Protocol string `xml:"protocol,attr" json:"protocol"`
ScanFlags string `xml:"scanflags,attr" json:"scan_flags"`
Services string `xml:"services,attr" json:"services"`
Type string `xml:"type,attr" json:"type"`
}
// Verbose contains the verbosity level of the scan.
type Verbose struct {
Level int `xml:"level,attr" json:"level"`
}
// Debugging contains the debugging level of the scan.
type Debugging struct {
Level int `xml:"level,attr" json:"level"`
}
// Task contains information about a task.
type Task struct {
Time Timestamp `xml:"time,attr" json:"time"`
Task string `xml:"task,attr" json:"task"`
ExtraInfo string `xml:"extrainfo,attr" json:"extra_info"`
}
// TaskProgress contains information about the progression of a task.
type TaskProgress struct {
Percent float32 `xml:"percent,attr" json:"percent"`
Remaining int `xml:"remaining,attr" json:"remaining"`
Task string `xml:"task,attr" json:"task"`
Etc Timestamp `xml:"etc,attr" json:"etc"`
Time Timestamp `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"/>
type Target struct {
Specification string `xml:"specification,attr" json:"specification"`
Status string `xml:"status,attr" json:"status"`
Reason string `xml:"reason,attr" json:"reason"`
}
// Host represents a host that was scanned.
type Host struct {
Distance Distance `xml:"distance" json:"distance"`
EndTime Timestamp `xml:"endtime,attr,omitempty" json:"end_time"`
IPIDSequence IPIDSequence `xml:"ipidsequence" json:"ip_id_sequence"`
OS OS `xml:"os" json:"os"`
StartTime Timestamp `xml:"starttime,attr,omitempty" json:"start_time"`
TimedOut bool `xml:"timedout,attr,omitempty" json:"timed_out"`
Status Status `xml:"status" json:"status"`
TCPSequence TCPSequence `xml:"tcpsequence" json:"tcp_sequence"`
TCPTSSequence TCPTSSequence `xml:"tcptssequence" json:"tcp_ts_sequence"`
Times Times `xml:"times" json:"times"`
Trace Trace `xml:"trace" json:"trace"`
Uptime Uptime `xml:"uptime" json:"uptime"`
Comment string `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.
type Status struct {
State string `xml:"state,attr" json:"state"`
Reason string `xml:"reason,attr" json:"reason"`
ReasonTTL float32 `xml:"reason_ttl,attr" json:"reason_ttl"`
}
func (s Status) String() string {
return s.State
}
// Address contains a IPv4 or IPv6 address for a host.
type Address struct {
Addr string `xml:"addr,attr" json:"addr"`
AddrType string `xml:"addrtype,attr" json:"addr_type"`
Vendor string `xml:"vendor,attr" json:"vendor"`
}
func (a Address) String() string {
return a.Addr
}
// Hostname is a name for a host.
type Hostname struct {
Name string `xml:"name,attr" json:"name"`
Type string `xml:"type,attr" json:"type"`
}
func (h Hostname) String() string {
return h.Name
}
// Smurf contains responses from a smurf attack.
type Smurf struct {
Responses string `xml:"responses,attr" json:"responses"`
}
// ExtraPort contains the information about the closed and filtered ports.
type ExtraPort struct {
State string `xml:"state,attr" json:"state"`
Count int `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.
type Reason struct {
Reason string `xml:"reason,attr" json:"reason"`
Count int `xml:"count,attr" json:"count"`
}
// Port contains all the information about a scanned port.
type Port struct {
ID uint16 `xml:"portid,attr" json:"id"`
Protocol string `xml:"protocol,attr" json:"protocol"`
Owner Owner `xml:"owner" json:"owner"`
Service Service `xml:"service" json:"service"`
State State `xml:"state" json:"state"`
Scripts []Script `xml:"script" json:"scripts"`
}
// PortStatus represents a port's state.
type PortStatus string
// Enumerates the different possible state values.
const (
Open PortStatus = "open"
Closed PortStatus = "closed"
Filtered PortStatus = "filtered"
Unfiltered PortStatus = "unfiltered"
)
// Status returns the status of a port.
func (p Port) Status() PortStatus {
return PortStatus(p.State.State)
}
// State contains information about a given port's status.
// State will be open, closed, etc.
type State struct {
State string `xml:"state,attr" json:"state"`
Reason string `xml:"reason,attr" json:"reason"`
ReasonIP string `xml:"reason_ip,attr" json:"reason_ip"`
ReasonTTL float32 `xml:"reason_ttl,attr" json:"reason_ttl"`
}
func (s State) String() string {
return s.State
}
// Owner contains the name of a port's owner.
type Owner struct {
Name string `xml:"name,attr" json:"name"`
}
func (o Owner) String() string {
return o.Name
}
// Service contains detailed information about a service on an open port.
type Service struct {
DeviceType string `xml:"devicetype,attr" json:"device_type"`
ExtraInfo string `xml:"extrainfo,attr" json:"extra_info"`
HighVersion string `xml:"highver,attr" json:"high_version"`
Hostname string `xml:"hostname,attr" json:"hostname"`
LowVersion string `xml:"lowver,attr" json:"low_version"`
Method string `xml:"method,attr" json:"method"`
Name string `xml:"name,attr" json:"name"`
OSType string `xml:"ostype,attr" json:"os_type"`
Product string `xml:"product,attr" json:"product"`
Proto string `xml:"proto,attr" json:"proto"`
RPCNum string `xml:"rpcnum,attr" json:"rpc_num"`
ServiceFP string `xml:"servicefp,attr" json:"service_fp"`
Tunnel string `xml:"tunnel,attr" json:"tunnel"`
Version string `xml:"version,attr" json:"version"`
Confidence int `xml:"conf,attr" json:"confidence"`
CPEs []CPE `xml:"cpe" json:"cpes"`
}
func (s Service) String() string {
return s.Name
}
// CPE (Common Platform Enumeration) is a standardized way to name software
// applications, operating systems and hardware platforms.
type CPE string
// 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.
type Script struct {
ID string `xml:"id,attr" json:"id"`
Output string `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.
type Table struct {
Key string `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.
type Element struct {
Key string `xml:"key,attr,omitempty" json:"key,omitempty"`
Value string `xml:",innerxml" json:"value"`
}
// OS contains the fingerprinted operating system for a host.
type OS struct {
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.
type PortUsed struct {
State string `xml:"state,attr" json:"state"`
Proto string `xml:"proto,attr" json:"proto"`
ID int `xml:"portid,attr" json:"port_id"`
}
// OSMatch contains detailed information regarding an operating system fingerprint.
type OSMatch struct {
Name string `xml:"name,attr" json:"name"`
Accuracy int `xml:"accuracy,attr" json:"accuracy"`
Line int `xml:"line,attr" json:"line"`
Classes []OSClass `xml:"osclass" json:"os_classes"`
}
// OSClass contains vendor information about an operating system.
type OSClass struct {
Vendor string `xml:"vendor,attr" json:"vendor"`
OSGeneration string `xml:"osgen,attr" json:"os_generation"`
Type string `xml:"type,attr" json:"type"`
Accuracy int `xml:"accuracy,attr" json:"accuracy"`
Family string `xml:"osfamily,attr" json:"os_family"`
CPEs []CPE `xml:"cpe" json:"cpes"`
}
// OSFamily returns the OS family in an enumerated format.
func (o OSClass) OSFamily() family.OSFamily {
return family.OSFamily(o.Family)
}
// OSFingerprint is the actual fingerprint string of an operating system.
type OSFingerprint struct {
Fingerprint string `xml:"fingerprint,attr" json:"fingerprint"`
}
// Distance is the amount of hops to a particular host.
type Distance struct {
Value int `xml:"value,attr" json:"value"`
}
// Uptime is the amount of time the host has been up.
type Uptime struct {
Seconds int `xml:"seconds,attr" json:"seconds"`
Lastboot string `xml:"lastboot,attr" json:"last_boot"`
}
// Sequence represents a detected sequence.
type Sequence struct {
Class string `xml:"class,attr" json:"class"`
Values string `xml:"values,attr" json:"values"`
}
// TCPSequence represents a detected TCP sequence.
type TCPSequence struct {
Index int `xml:"index,attr" json:"index"`
Difficulty string `xml:"difficulty,attr" json:"difficulty"`
Values string `xml:"values,attr" json:"values"`
}
// IPIDSequence represents a detected IP ID sequence.
type IPIDSequence Sequence
// TCPTSSequence represents a detected TCP TS sequence.
type TCPTSSequence Sequence
// Trace represents the trace to a host, including the hops.
type Trace struct {
Proto string `xml:"proto,attr" json:"proto"`
Port int `xml:"port,attr" json:"port"`
Hops []Hop `xml:"hop" json:"hops"`
}
// Hop is an IP hop to a host.
type Hop struct {
TTL float32 `xml:"ttl,attr" json:"ttl"`
RTT string `xml:"rtt,attr" json:"rtt"`
IPAddr string `xml:"ipaddr,attr" json:"ip_addr"`
Host string `xml:"host,attr" json:"host"`
}
// Times contains time statistics for an nmap scan.
type Times struct {
SRTT string `xml:"srtt,attr" json:"srtt"`
RTT string `xml:"rttvar,attr" json:"rttv"`
To string `xml:"to,attr" json:"to"`
}
// Stats contains statistics for an nmap scan.
type Stats struct {
Finished Finished `xml:"finished" json:"finished"`
Hosts HostStats `xml:"hosts" json:"hosts"`
}
// Finished contains detailed statistics regarding a finished scan.
type Finished struct {
Time Timestamp `xml:"time,attr" json:"time"`
TimeStr string `xml:"timestr,attr" json:"time_str"`
Elapsed float32 `xml:"elapsed,attr" json:"elapsed"`
Summary string `xml:"summary,attr" json:"summary"`
Exit string `xml:"exit,attr" json:"exit"`
ErrorMsg string `xml:"errormsg,attr" json:"error_msg"`
}
// HostStats contains the amount of up and down hosts and the total count.
type HostStats struct {
Up int `xml:"up,attr" json:"up"`
Down int `xml:"down,attr" json:"down"`
Total int `xml:"total,attr" json:"total"`
}
// Timestamp represents time as a UNIX timestamp in seconds.
type Timestamp time.Time
// ParseTime converts a UNIX timestamp string to a time.Time.
func (t *Timestamp) ParseTime(s string) error {
timestamp, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
*t = Timestamp(time.Unix(timestamp, 0))
return nil
}
// FormatTime formats the time.Time value as a UNIX timestamp string.
func (t Timestamp) FormatTime() string {
return strconv.FormatInt(time.Time(t).Unix(), 10)
}
// MarshalJSON implements the json.Marshaler interface.
func (t Timestamp) MarshalJSON() ([]byte, error) {
return []byte(t.FormatTime()), nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (t *Timestamp) UnmarshalJSON(b []byte) error {
return t.ParseTime(string(b))
}
// MarshalXMLAttr implements the xml.MarshalerAttr interface.
func (t Timestamp) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
if time.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) (err error) {
return t.ParseTime(attr.Value)
}
// Parse takes a byte array of nmap xml data and unmarshal it into a Run struct.
func Parse(content []byte, result *Run) error {
result.rawXML = content
err := xml.Unmarshal(content, result)
return err
}