| 1 | package net_sniff |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "bytes" |
| 6 | "compress/gzip" |
| 7 | "io/ioutil" |
| 8 | "net" |
| 9 | "net/http" |
| 10 | "strings" |
| 11 | |
| 12 | "github.com/google/gopacket" |
| 13 | "github.com/google/gopacket/layers" |
| 14 | |
| 15 | "github.com/dustin/go-humanize" |
| 16 | |
| 17 | "github.com/evilsocket/islazy/tui" |
| 18 | ) |
| 19 | |
| 20 | type HTTPRequest struct { |
| 21 | Method string `json:"method"` |
| 22 | Proto string `json:"proto"` |
| 23 | Host string `json:"host"` |
| 24 | URL string `json:"url:"` |
| 25 | Headers http.Header `json:"headers"` |
| 26 | ContentType string `json:"content_type"` |
| 27 | Body []byte `json:"body"` |
| 28 | } |
| 29 | |
| 30 | func (r HTTPRequest) IsType(ctype string) bool { |
| 31 | return strings.Contains(r.ContentType, ctype) |
| 32 | } |
| 33 | |
| 34 | type HTTPResponse struct { |
| 35 | Protocol string `json:"protocol"` |
| 36 | Status string `json:"status"` |
| 37 | StatusCode int `json:"status_code"` |
| 38 | Headers http.Header `json:"headers"` |
| 39 | Body []byte `json:"body"` |
| 40 | ContentLength int64 `json:"content_length"` |
| 41 | ContentType string `json:"content_type"` |
| 42 | TransferEncoding []string `json:"transfer_encoding"` |
| 43 | } |
| 44 | |
| 45 | func (r HTTPResponse) IsType(ctype string) bool { |
| 46 | return strings.Contains(r.ContentType, ctype) |
| 47 | } |
| 48 | |
| 49 | func toSerializableRequest(req *http.Request) HTTPRequest { |
| 50 | body := []byte(nil) |
| 51 | ctype := "?" |
| 52 | if req.Body != nil { |
| 53 | body, _ = ioutil.ReadAll(req.Body) |
| 54 | } |
| 55 | |
| 56 | for name, values := range req.Header { |
| 57 | if strings.ToLower(name) == "content-type" { |
| 58 | for _, value := range values { |
| 59 | ctype = value |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return HTTPRequest{ |
| 65 | Method: req.Method, |
| 66 | Proto: req.Proto, |
| 67 | Host: req.Host, |
| 68 | URL: req.URL.String(), |
| 69 | Headers: req.Header, |
| 70 | ContentType: ctype, |
| 71 | Body: body, |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func toSerializableResponse(res *http.Response) HTTPResponse { |
| 76 | body := []byte(nil) |
| 77 | ctype := "?" |
| 78 | cenc := "" |
| 79 | for name, values := range res.Header { |
| 80 | name = strings.ToLower(name) |
| 81 | if name == "content-type" { |
| 82 | for _, value := range values { |
| 83 | ctype = value |
| 84 | } |
| 85 | } else if name == "content-encoding" { |
| 86 | for _, value := range values { |
| 87 | cenc = value |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if res.Body != nil { |
| 93 | body, _ = ioutil.ReadAll(res.Body) |
| 94 | } |
| 95 | |
| 96 | // attempt decompression, but since this has been parsed by just |
| 97 | // a tcp packet, it will probably fail |
| 98 | if body != nil && strings.Contains(cenc, "gzip") { |
| 99 | buffer := bytes.NewBuffer(body) |
| 100 | uncompressed := bytes.Buffer{} |
| 101 | if reader, err := gzip.NewReader(buffer); err == nil { |
| 102 | if _, err = uncompressed.ReadFrom(reader); err == nil { |
| 103 | body = uncompressed.Bytes() |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return HTTPResponse{ |
| 109 | Protocol: res.Proto, |
| 110 | Status: res.Status, |
| 111 | StatusCode: res.StatusCode, |
| 112 | Headers: res.Header, |
| 113 | Body: body, |
| 114 | ContentLength: res.ContentLength, |
| 115 | ContentType: ctype, |
| 116 | TransferEncoding: res.TransferEncoding, |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func httpParser(srcIP, dstIP net.IP, payload []byte, pkt gopacket.Packet, tcp *layers.TCP) bool { |
| 121 | data := tcp.Payload |
| 122 | if req, err := http.ReadRequest(bufio.NewReader(bytes.NewReader(data))); err == nil { |
| 123 | if user, pass, ok := req.BasicAuth(); ok { |
| 124 | NewSnifferEvent( |
| 125 | pkt.Metadata().Timestamp, |
| 126 | "http.request", |
| 127 | srcIP.String(), |
| 128 | req.Host, |
| 129 | toSerializableRequest(req), |
| 130 | "%s %s %s %s%s - %s %s, %s %s", |
| 131 | tui.Wrap(tui.BACKRED+tui.FOREBLACK, "http"), |
| 132 | vIP(srcIP), |
| 133 | tui.Wrap(tui.BACKLIGHTBLUE+tui.FOREBLACK, req.Method), |
| 134 | tui.Yellow(req.Host), |
| 135 | vURL(req.URL.String()), |
| 136 | tui.Bold("USER"), |
| 137 | tui.Red(user), |
| 138 | tui.Bold("PASS"), |
| 139 | tui.Red(pass), |
| 140 | ).Push() |
| 141 | } else { |
| 142 | NewSnifferEvent( |
| 143 | pkt.Metadata().Timestamp, |
| 144 | "http.request", |
| 145 | srcIP.String(), |
| 146 | req.Host, |
| 147 | toSerializableRequest(req), |
| 148 | "%s %s %s %s%s", |
| 149 | tui.Wrap(tui.BACKRED+tui.FOREBLACK, "http"), |
| 150 | vIP(srcIP), |
| 151 | tui.Wrap(tui.BACKLIGHTBLUE+tui.FOREBLACK, req.Method), |
| 152 | tui.Yellow(req.Host), |
| 153 | vURL(req.URL.String()), |
| 154 | ).Push() |
| 155 | } |
| 156 | |
| 157 | return true |
| 158 | } else if res, err := http.ReadResponse(bufio.NewReader(bytes.NewReader(data)), nil); err == nil { |
| 159 | sres := toSerializableResponse(res) |
| 160 | NewSnifferEvent( |
| 161 | pkt.Metadata().Timestamp, |
| 162 | "http.response", |
| 163 | srcIP.String(), |
| 164 | dstIP.String(), |
| 165 | sres, |
| 166 | "%s %s:%d %s -> %s (%s %s)", |
| 167 | tui.Wrap(tui.BACKRED+tui.FOREBLACK, "http"), |
| 168 | vIP(srcIP), |
| 169 | tcp.SrcPort, |
| 170 | tui.Bold(res.Status), |
| 171 | vIP(dstIP), |
| 172 | tui.Dim(humanize.Bytes(uint64(len(sres.Body)))), |
| 173 | tui.Yellow(sres.ContentType), |
| 174 | ).Push() |
| 175 | |
| 176 | return true |
| 177 | } |
| 178 | |
| 179 | return false |
| 180 | } |