|
| 1 | +// Package pptp contains the zgrab2 Module implementation for PPTP. |
| 2 | +package pptp |
| 3 | + |
| 4 | +import ( |
| 5 | + "encoding/binary" |
| 6 | + "fmt" |
| 7 | + "net" |
| 8 | + "time" |
| 9 | + |
| 10 | + log "github.com/sirupsen/logrus" |
| 11 | + "github.com/zmap/zgrab2" |
| 12 | +) |
| 13 | + |
| 14 | +// ScanResults is the output of the scan. |
| 15 | +type ScanResults struct { |
| 16 | + // Banner is the initial data banner sent by the server. |
| 17 | + Banner string `json:"banner,omitempty"` |
| 18 | + |
| 19 | + // ControlMessage is the received PPTP control message. |
| 20 | + ControlMessage string `json:"control_message,omitempty"` |
| 21 | +} |
| 22 | + |
| 23 | +// Flags are the PPTP-specific command-line flags. |
| 24 | +type Flags struct { |
| 25 | + zgrab2.BaseFlags |
| 26 | + Verbose bool `long:"verbose" description:"More verbose logging, include debug fields in the scan results"` |
| 27 | +} |
| 28 | + |
| 29 | +// Module implements the zgrab2.Module interface. |
| 30 | +type Module struct { |
| 31 | +} |
| 32 | + |
| 33 | +// Scanner implements the zgrab2.Scanner interface, and holds the state |
| 34 | +// for a single scan. |
| 35 | +type Scanner struct { |
| 36 | + config *Flags |
| 37 | +} |
| 38 | + |
| 39 | +// RegisterModule registers the pptp zgrab2 module. |
| 40 | +func RegisterModule() { |
| 41 | + var module Module |
| 42 | + _, err := zgrab2.AddCommand("pptp", "PPTP", module.Description(), 1723, &module) |
| 43 | + if err != nil { |
| 44 | + log.Fatal(err) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// NewFlags returns the default flags object to be filled in with the |
| 49 | +// command-line arguments. |
| 50 | +func (m *Module) NewFlags() interface{} { |
| 51 | + return new(Flags) |
| 52 | +} |
| 53 | + |
| 54 | +// NewScanner returns a new Scanner instance. |
| 55 | +func (m *Module) NewScanner() zgrab2.Scanner { |
| 56 | + return new(Scanner) |
| 57 | +} |
| 58 | + |
| 59 | +// Description returns an overview of this module. |
| 60 | +func (m *Module) Description() string { |
| 61 | + return "Scan for PPTP" |
| 62 | +} |
| 63 | + |
| 64 | +// Validate flags |
| 65 | +func (f *Flags) Validate(args []string) (err error) { |
| 66 | + return |
| 67 | +} |
| 68 | + |
| 69 | +// Help returns this module's help string. |
| 70 | +func (f *Flags) Help() string { |
| 71 | + return "" |
| 72 | +} |
| 73 | + |
| 74 | +// Protocol returns the protocol identifier for the scanner. |
| 75 | +func (s *Scanner) Protocol() string { |
| 76 | + return "pptp" |
| 77 | +} |
| 78 | + |
| 79 | +// Init initializes the Scanner instance with the flags from the command line. |
| 80 | +func (s *Scanner) Init(flags zgrab2.ScanFlags) error { |
| 81 | + f, _ := flags.(*Flags) |
| 82 | + s.config = f |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +// InitPerSender does nothing in this module. |
| 87 | +func (s *Scanner) InitPerSender(senderID int) error { |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +// GetName returns the configured name for the Scanner. |
| 92 | +func (s *Scanner) GetName() string { |
| 93 | + return s.config.Name |
| 94 | +} |
| 95 | + |
| 96 | +// GetTrigger returns the Trigger defined in the Flags. |
| 97 | +func (scanner *Scanner) GetTrigger() string { |
| 98 | + return scanner.config.Trigger |
| 99 | +} |
| 100 | + |
| 101 | +// PPTP Start-Control-Connection-Request message constants |
| 102 | +const ( |
| 103 | + PPTP_MAGIC_COOKIE = 0x1A2B3C4D |
| 104 | + PPTP_CONTROL_MESSAGE = 1 |
| 105 | + PPTP_START_CONN_REQUEST = 1 |
| 106 | + PPTP_PROTOCOL_VERSION = 0x0100 // Split into two 16-bit values for binary.BigEndian.PutUint16 |
| 107 | +) |
| 108 | + |
| 109 | +// Connection holds the state for a single connection to the PPTP server. |
| 110 | +type Connection struct { |
| 111 | + config *Flags |
| 112 | + results ScanResults |
| 113 | + conn net.Conn |
| 114 | +} |
| 115 | + |
| 116 | +// Create the Start-Control-Connection-Request message |
| 117 | +func createSCCRMessage() []byte { |
| 118 | + message := make([]byte, 156) |
| 119 | + binary.BigEndian.PutUint16(message[0:2], 156) // Length |
| 120 | + binary.BigEndian.PutUint16(message[2:4], PPTP_CONTROL_MESSAGE) // PPTP Message Type |
| 121 | + binary.BigEndian.PutUint32(message[4:8], PPTP_MAGIC_COOKIE) // Magic Cookie |
| 122 | + binary.BigEndian.PutUint16(message[8:10], PPTP_START_CONN_REQUEST) // Control Message Type |
| 123 | + binary.BigEndian.PutUint16(message[10:12], uint16(PPTP_PROTOCOL_VERSION>>16)) // Protocol Version (high 16 bits) |
| 124 | + binary.BigEndian.PutUint16(message[12:14], uint16(PPTP_PROTOCOL_VERSION&0xFFFF)) // Protocol Version (low 16 bits) |
| 125 | + binary.BigEndian.PutUint32(message[14:18], 0) // Framing Capabilities |
| 126 | + binary.BigEndian.PutUint32(message[18:22], 0) // Bearer Capabilities |
| 127 | + binary.BigEndian.PutUint16(message[22:24], 0) // Maximum Channels |
| 128 | + binary.BigEndian.PutUint16(message[24:26], 0) // Firmware Revision |
| 129 | + copy(message[26:90], "ZGRAB2-SCANNER") // Host Name |
| 130 | + copy(message[90:], "ZGRAB2") // Vendor Name |
| 131 | + return message |
| 132 | +} |
| 133 | + |
| 134 | +// Read response from the PPTP server |
| 135 | +func (pptp *Connection) readResponse() (string, error) { |
| 136 | + buffer := make([]byte, 1024) |
| 137 | + pptp.conn.SetReadDeadline(time.Now().Add(5 * time.Second)) |
| 138 | + n, err := pptp.conn.Read(buffer) |
| 139 | + if err != nil { |
| 140 | + return "", err |
| 141 | + } |
| 142 | + return string(buffer[:n]), nil |
| 143 | +} |
| 144 | + |
| 145 | +// Scan performs the configured scan on the PPTP server |
| 146 | +func (s *Scanner) Scan(t zgrab2.ScanTarget) (status zgrab2.ScanStatus, result interface{}, thrown error) { |
| 147 | + var err error |
| 148 | + conn, err := t.Open(&s.config.BaseFlags) |
| 149 | + if err != nil { |
| 150 | + return zgrab2.TryGetScanStatus(err), nil, fmt.Errorf("error opening connection: %w", err) |
| 151 | + } |
| 152 | + cn := conn |
| 153 | + defer func() { |
| 154 | + cn.Close() |
| 155 | + }() |
| 156 | + |
| 157 | + results := ScanResults{} |
| 158 | + |
| 159 | + pptp := Connection{conn: cn, config: s.config, results: results} |
| 160 | + |
| 161 | + // Send Start-Control-Connection-Request message |
| 162 | + request := createSCCRMessage() |
| 163 | + _, err = pptp.conn.Write(request) |
| 164 | + if err != nil { |
| 165 | + return zgrab2.TryGetScanStatus(err), &pptp.results, fmt.Errorf("error sending PPTP SCCR message: %w", err) |
| 166 | + } |
| 167 | + |
| 168 | + // Read the response |
| 169 | + response, err := pptp.readResponse() |
| 170 | + if err != nil { |
| 171 | + return zgrab2.TryGetScanStatus(err), &pptp.results, fmt.Errorf("error reading PPTP response: %w", err) |
| 172 | + } |
| 173 | + |
| 174 | + // Store the banner and control message |
| 175 | + pptp.results.Banner = string(request) |
| 176 | + pptp.results.ControlMessage = response |
| 177 | + |
| 178 | + return zgrab2.SCAN_SUCCESS, &pptp.results, nil |
| 179 | +} |
0 commit comments