Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions parsers/burp/xml/strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package burpxml

import "fmt"

func (i Item) ToStrings(noReq, noResp bool) []string {
arr := []string{
i.Time,
i.URL,
i.Host.Name,
i.Host.IP,
i.Port,
i.Protocol,
i.Path,
i.Extension,
}

if !noReq {
arr = append(arr, i.Request.ToStrings()...)
}

arr = append(arr, []string{
i.Status,
i.ResponseLength,
i.MimeType,
}...)

if !noResp {
arr = append(arr, i.Response.ToStrings()...)
}

arr = append(arr, i.Comment)
return arr
}

func (r Request) ToStrings() []string {
if r.Body != "" {
return []string{r.Body}
}
return []string{r.Base64Encoded, r.Raw}
}

func (r Response) ToStrings() []string {
if r.Body != "" {
return []string{r.Body}
}
return []string{r.Base64Encoded, r.Raw}
}

func (i Item) FlatString() string {
return fmt.Sprintf(`%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s`,
i.Time,
i.URL,
i.Host.Name,
i.Host.IP,
i.Port,
i.Protocol,
i.Path,
i.Extension,
i.Request.FlatString(),
i.Status,
i.ResponseLength,
i.MimeType,
i.Response.FlatString(),
i.Comment,
)
}

func (r Request) FlatString() string {
if r.Body != "" {
return r.Body
}
return fmt.Sprintf(`%s,%s`, r.Base64Encoded, r.Raw)
}

func (r Response) FlatString() string {
if r.Body != "" {
return r.Body
}
return fmt.Sprintf(`%s,%s`, r.Base64Encoded, r.Raw)
}

func (i Item) String() string {
return fmt.Sprintf(`Item{
Time = %s,
Url = %s,
Host = %s,
IP = %s,
Port = %s,
Proto = %s,
Path = %s,
Ext = %s,
%s,
Status = %s,
RespLen = %s,
MIME = %s,
%s,
Comment = %s,
}`,
i.Time,
i.URL,
i.Host.Name,
i.Host.IP,
i.Port,
i.Protocol,
i.Path,
i.Extension,
i.Request.String(),
i.Status,
i.ResponseLength,
i.MimeType,
i.Response.String(),
i.Comment,
)
}

func (r Request) String() string {
s := "Request{\n"
if r.Body != "" {
s += fmt.Sprintf(" Body = %s,\n", r.Body)
} else {
s += fmt.Sprintf(" Base64 = %s,\nBody = %s,\n", r.Base64Encoded, r.Raw)
}
s += "}"
return s
}

func (r Response) String() string {
s := "Response{\n"
if r.Body != "" {
s += fmt.Sprintf(" Body = %s,\n", r.Body)
} else {
s += fmt.Sprintf(" Base64 = %s,\nBody = %s,\n", r.Base64Encoded, r.Raw)
}
s += "}"
return s
}
113 changes: 113 additions & 0 deletions parsers/burp/xml/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package burpxml

import (
"encoding/csv"
"encoding/json"
"fmt"
"io"
)

type Request struct {
Base64Encoded string `xml:"base64,attr" json:"base64_encoded,omitempty"`
Raw string `xml:",chardata" json:"raw,omitempty"`
Body string `xml:"-" json:"body,omitempty"`
}

type Response struct {
Base64Encoded string `xml:"base64,attr" json:"base64_encoded,omitempty"`
Raw string `xml:",chardata" json:"raw,omitempty"`
Body string `xml:"-" json:"body,omitempty"`
}

type Host struct {
IP string `xml:"ip,attr" json:"ip,omitempty"`
Name string `xml:",chardata" json:"name,omitempty"`
}

type Item struct {
Time string `xml:"time" json:"time,omitempty"`
URL string `xml:"url" json:"url,omitempty"`
Host Host `xml:"host" json:"host,omitzero"`
Port string `xml:"port" json:"port,omitempty"`
Protocol string `xml:"protocol" json:"protocol,omitempty"`
Path string `xml:"path" json:"path,omitempty"`
Extension string `xml:"extension" json:"extension,omitempty"`
Request Request `xml:"request" json:"request,omitzero"`
Status string `xml:"status" json:"status,omitempty"`
ResponseLength string `xml:"responselength" json:"response_length,omitempty"`
MimeType string `xml:"mimetype" json:"mime_type,omitempty"`
Response Response `xml:"response" json:"response,omitzero"`
Comment string `xml:"comment" json:"comment,omitempty"`
}

type Items struct {
Items []Item `xml:"item" json:"items,omitempty"`
}

func (items *Items) ToJSON(w io.Writer) error {
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
if err := enc.Encode(items); err != nil {
return fmt.Errorf("json encode: %w", err)
}
return nil
}

type CSVOptions struct {
ExcludeRequest bool
ExcludeResponse bool
}

func (items *Items) ToCSV(w io.Writer, opts CSVOptions) error {
enc := csv.NewWriter(w)
defer enc.Flush()

for _, item := range items.Items {
record := item.toRecord(opts)
if err := enc.Write(record); err != nil {
return fmt.Errorf("csv write: %w", err)
}
}

return enc.Error()
}

func (item *Item) toRecord(opts CSVOptions) []string {
record := []string{
item.Time,
item.URL,
item.Host.Name,
item.Host.IP,
item.Port,
item.Protocol,
item.Path,
item.Extension,
}

if !opts.ExcludeRequest {
record = append(record, item.Request.content())
}

record = append(record, item.Status, item.ResponseLength, item.MimeType)

if !opts.ExcludeResponse {
record = append(record, item.Response.content())
}

record = append(record, item.Comment)
return record
}

func (r *Request) content() string {
if r.Body != "" {
return r.Body
}
return r.Raw
}

func (r *Response) content() string {
if r.Body != "" {
return r.Body
}
return r.Raw
}
70 changes: 70 additions & 0 deletions parsers/burp/xml/xml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package burpxml

import (
"encoding/base64"
"encoding/xml"
"fmt"
"io"
"strconv"
)

type XMLParseOptions struct {
DecodeBase64 bool
}

func ParseXML(r io.Reader, opts XMLParseOptions) (*Items, error) {
var items Items
if err := xml.NewDecoder(r).Decode(&items); err != nil {
return nil, fmt.Errorf("xml decode: %w", err)
}

if !opts.DecodeBase64 {
return &items, nil
}

for i := range items.Items {
if err := decodeItemBodies(&items.Items[i]); err != nil {
return nil, err
}
}

return &items, nil
}

func decodeItemBodies(item *Item) error {
decoded, err := decodeBase64Field(item.Request.Base64Encoded, item.Request.Raw)
if err != nil {
return fmt.Errorf("decode request: %w", err)
}
item.Request.Body = decoded

decoded, err = decodeBase64Field(item.Response.Base64Encoded, item.Response.Raw)
if err != nil {
return fmt.Errorf("decode response: %w", err)
}
item.Response.Body = decoded

return nil
}

func decodeBase64Field(base64Flag, raw string) (string, error) {
if base64Flag == "" {
return raw, nil
}

isBase64, err := strconv.ParseBool(base64Flag)
if err != nil {
return "", fmt.Errorf("parse base64 flag: %w", err)
}

if !isBase64 {
return raw, nil
}

decoded, err := base64.StdEncoding.DecodeString(raw)
if err != nil {
return "", fmt.Errorf("base64 decode: %w", err)
}

return string(decoded), nil
}
Loading
Loading