|
2 | 2 | package fortinet
|
3 | 3 |
|
4 | 4 | import (
|
5 |
| - "bytes" |
6 |
| - "crypto/tls" |
7 |
| - "encoding/binary" |
8 |
| - "net" |
9 |
| - |
10 |
| - "github.com/vulncheck-oss/go-exploit/output" |
11 |
| - "github.com/vulncheck-oss/go-exploit/protocol" |
| 5 | + "bytes" |
| 6 | + "crypto/tls" |
| 7 | + "encoding/binary" |
| 8 | + "net" |
12 | 9 |
|
| 10 | + "github.com/vulncheck-oss/go-exploit/output" |
| 11 | + "github.com/vulncheck-oss/go-exploit/protocol" |
13 | 12 | )
|
14 | 13 |
|
15 |
| -// Creates a Fortinet FGFM message. The format is closed source, but research by BF, Watchtowr, and Rapid7 have helped uncover the basic message header structure |
16 |
| -// |
| 14 | +// Creates and sends a Fortinet FGFM message to a FortiManager. |
| 15 | +// The format is closed source, but research by BF, Watchtowr, and Rapid7 have helped uncover the basic message header structure: |
17 | 16 | // [4 bytes of magic header]
|
18 | 17 | // [4 bytes of total request length]
|
19 |
| -// [n bytes request body data] |
20 |
| - |
| 18 | +// [n bytes request body data]. |
21 | 19 | func SendFGFMMessage(conn net.Conn, payload string) bool {
|
22 |
| - message := make([]byte, 0) |
23 |
| - // add magic header |
24 |
| - message = append(message, []byte("\x36\xe0\x11\x00")...) |
25 |
| - // build the total length field |
26 |
| - totalLengthField := make([]byte, 4) |
27 |
| - length := len(payload) + 8 |
28 |
| - binary.BigEndian.PutUint32(totalLengthField, uint32(length)) |
29 |
| - message = append(message, totalLengthField...) |
30 |
| - // add payload |
31 |
| - message = append(message, []byte(payload)...) |
| 20 | + message := make([]byte, 0) |
| 21 | + // add magic header |
| 22 | + message = append(message, []byte("\x36\xe0\x11\x00")...) |
| 23 | + // build the total length field |
| 24 | + totalLengthField := make([]byte, 4) |
| 25 | + length := len(payload) + 8 |
| 26 | + binary.BigEndian.PutUint32(totalLengthField, uint32(length)) |
| 27 | + message = append(message, totalLengthField...) |
| 28 | + // add payload |
| 29 | + message = append(message, []byte(payload)...) |
32 | 30 |
|
33 |
| - return protocol.TCPWrite(conn, message) |
| 31 | + return protocol.TCPWrite(conn, message) |
34 | 32 | }
|
35 | 33 |
|
| 34 | +// Reads response from a FortiManager. |
36 | 35 | func ReadFGFMMessage(conn net.Conn) ([]byte, bool) {
|
37 |
| - magic, ok := protocol.TCPReadAmount(conn, 4) |
38 |
| - if !ok || !bytes.Equal(magic, []byte("\x36\xe0\x11\x00")) { |
39 |
| - output.PrintFrameworkError("Failed to read server response with expected header") |
40 |
| - return nil, false |
41 |
| - } |
42 |
| - size, ok := protocol.TCPReadAmount(conn, 4) |
43 |
| - if !ok { |
44 |
| - output.PrintFrameworkError("Failed to read server response length") |
45 |
| - return nil, false |
46 |
| - } |
| 36 | + magic, ok := protocol.TCPReadAmount(conn, 4) |
| 37 | + if !ok || !bytes.Equal(magic, []byte("\x36\xe0\x11\x00")) { |
| 38 | + output.PrintFrameworkError("Failed to read server response with expected header") |
47 | 39 |
|
48 |
| - readSize := int(binary.BigEndian.Uint32(size)) |
49 |
| - data, ok := protocol.TCPReadAmount(conn, readSize-8) |
50 |
| - if !ok { |
51 |
| - output.PrintFrameworkError("Failed to read server response data") |
52 |
| - return nil, false |
53 |
| - } |
54 |
| - |
55 |
| - return data, true |
| 40 | + return nil, false |
| 41 | + } |
| 42 | + size, ok := protocol.TCPReadAmount(conn, 4) |
| 43 | + if !ok { |
| 44 | + output.PrintFrameworkError("Failed to read server response length") |
| 45 | + |
| 46 | + return nil, false |
| 47 | + } |
| 48 | + |
| 49 | + readSize := int(binary.BigEndian.Uint32(size)) |
| 50 | + data, ok := protocol.TCPReadAmount(conn, readSize-8) |
| 51 | + if !ok { |
| 52 | + output.PrintFrameworkError("Failed to read server response data") |
| 53 | + |
| 54 | + return nil, false |
| 55 | + } |
| 56 | + |
| 57 | + return data, true |
56 | 58 | }
|
57 | 59 |
|
58 | 60 | // Fortimanager requires a connecting Fortigate instance to have a cert.
|
59 | 61 | // SSL is optional here so you have the choice to sign the traffic from the go-exploit framework,
|
60 | 62 | // or so you can send the exploit network traffic through a proxy like socat to sign the traffic for you.
|
61 |
| -// Benefits to this include being able to generate pcaps of the unencrypted traffic |
| 63 | +// Benefits to this include being able to generate pcaps of the unencrypted traffic |
62 | 64 | // between go-exploit and your proxy.
|
63 | 65 | // See CVE-2024-47575 for additional information.
|
64 | 66 | func Connect(host string, port int, ssl bool, certFile string, keyFile string) (net.Conn, bool) {
|
65 |
| - if ssl { |
66 |
| - cert, err := tls.LoadX509KeyPair(certFile, keyFile) |
67 |
| - if err != nil { |
68 |
| - output.PrintFrameworkError("Failed to load x509 Key Pair") |
69 |
| - output.PrintfFrameworkDebug("Failed to load x509 Key Pair with error: %s", err) |
| 67 | + if ssl { |
| 68 | + cert, err := tls.LoadX509KeyPair(certFile, keyFile) |
| 69 | + if err != nil { |
| 70 | + output.PrintFrameworkError("Failed to load x509 Key Pair") |
| 71 | + output.PrintfFrameworkDebug("Failed to load x509 Key Pair with error: %s", err) |
70 | 72 |
|
71 |
| - return nil, false |
72 |
| - } |
73 |
| - cfg := &tls.Config{Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true} |
| 73 | + return nil, false |
| 74 | + } |
| 75 | + cfg := &tls.Config{Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true} |
74 | 76 |
|
75 |
| - conn, ok := protocol.TCPConnect(host, port) |
76 |
| - if !ok { |
77 |
| - return nil, false |
78 |
| - } |
79 |
| - return tls.Client(conn, cfg), true |
| 77 | + conn, ok := protocol.TCPConnect(host, port) |
| 78 | + if !ok { |
| 79 | + return nil, false |
80 | 80 | }
|
81 | 81 |
|
82 |
| - return protocol.TCPConnect(host, port) |
| 82 | + return tls.Client(conn, cfg), true |
| 83 | + } |
83 | 84 |
|
| 85 | + return protocol.TCPConnect(host, port) |
84 | 86 | }
|
0 commit comments