|
| 1 | +package burpxml |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/csv" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | +) |
| 9 | + |
| 10 | +type Request struct { |
| 11 | + Base64Encoded string `xml:"base64,attr" json:"base64_encoded,omitempty"` |
| 12 | + Raw string `xml:",chardata" json:"raw,omitempty"` |
| 13 | + Body string `xml:"-" json:"body,omitempty"` |
| 14 | +} |
| 15 | + |
| 16 | +type Response struct { |
| 17 | + Base64Encoded string `xml:"base64,attr" json:"base64_encoded,omitempty"` |
| 18 | + Raw string `xml:",chardata" json:"raw,omitempty"` |
| 19 | + Body string `xml:"-" json:"body,omitempty"` |
| 20 | +} |
| 21 | + |
| 22 | +type Host struct { |
| 23 | + IP string `xml:"ip,attr" json:"ip,omitempty"` |
| 24 | + Name string `xml:",chardata" json:"name,omitempty"` |
| 25 | +} |
| 26 | + |
| 27 | +type Item struct { |
| 28 | + Time string `xml:"time" json:"time,omitempty"` |
| 29 | + URL string `xml:"url" json:"url,omitempty"` |
| 30 | + Host Host `xml:"host" json:"host,omitzero"` |
| 31 | + Port string `xml:"port" json:"port,omitempty"` |
| 32 | + Protocol string `xml:"protocol" json:"protocol,omitempty"` |
| 33 | + Path string `xml:"path" json:"path,omitempty"` |
| 34 | + Extension string `xml:"extension" json:"extension,omitempty"` |
| 35 | + Request Request `xml:"request" json:"request,omitzero"` |
| 36 | + Status string `xml:"status" json:"status,omitempty"` |
| 37 | + ResponseLength string `xml:"responselength" json:"response_length,omitempty"` |
| 38 | + MimeType string `xml:"mimetype" json:"mime_type,omitempty"` |
| 39 | + Response Response `xml:"response" json:"response,omitzero"` |
| 40 | + Comment string `xml:"comment" json:"comment,omitempty"` |
| 41 | +} |
| 42 | + |
| 43 | +type Items struct { |
| 44 | + Items []Item `xml:"item" json:"items,omitempty"` |
| 45 | +} |
| 46 | + |
| 47 | +func (items *Items) ToJSON(w io.Writer) error { |
| 48 | + enc := json.NewEncoder(w) |
| 49 | + enc.SetIndent("", " ") |
| 50 | + if err := enc.Encode(items); err != nil { |
| 51 | + return fmt.Errorf("json encode: %w", err) |
| 52 | + } |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +type CSVOptions struct { |
| 57 | + ExcludeRequest bool |
| 58 | + ExcludeResponse bool |
| 59 | +} |
| 60 | + |
| 61 | +func (items *Items) ToCSV(w io.Writer, opts CSVOptions) error { |
| 62 | + enc := csv.NewWriter(w) |
| 63 | + defer enc.Flush() |
| 64 | + |
| 65 | + for _, item := range items.Items { |
| 66 | + record := item.toRecord(opts) |
| 67 | + if err := enc.Write(record); err != nil { |
| 68 | + return fmt.Errorf("csv write: %w", err) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return enc.Error() |
| 73 | +} |
| 74 | + |
| 75 | +func (item *Item) toRecord(opts CSVOptions) []string { |
| 76 | + record := []string{ |
| 77 | + item.Time, |
| 78 | + item.URL, |
| 79 | + item.Host.Name, |
| 80 | + item.Host.IP, |
| 81 | + item.Port, |
| 82 | + item.Protocol, |
| 83 | + item.Path, |
| 84 | + item.Extension, |
| 85 | + } |
| 86 | + |
| 87 | + if !opts.ExcludeRequest { |
| 88 | + record = append(record, item.Request.content()) |
| 89 | + } |
| 90 | + |
| 91 | + record = append(record, item.Status, item.ResponseLength, item.MimeType) |
| 92 | + |
| 93 | + if !opts.ExcludeResponse { |
| 94 | + record = append(record, item.Response.content()) |
| 95 | + } |
| 96 | + |
| 97 | + record = append(record, item.Comment) |
| 98 | + return record |
| 99 | +} |
| 100 | + |
| 101 | +func (r *Request) content() string { |
| 102 | + if r.Body != "" { |
| 103 | + return r.Body |
| 104 | + } |
| 105 | + return r.Raw |
| 106 | +} |
| 107 | + |
| 108 | +func (r *Response) content() string { |
| 109 | + if r.Body != "" { |
| 110 | + return r.Body |
| 111 | + } |
| 112 | + return r.Raw |
| 113 | +} |
0 commit comments