/src/PcapPlusPlus/Packet++/header/TelnetLayer.h
Line | Count | Source |
1 | | #pragma once |
2 | | |
3 | | #include "Layer.h" |
4 | | |
5 | | #include <algorithm> |
6 | | #include <iterator> |
7 | | |
8 | | /// @file |
9 | | |
10 | | /// @namespace pcpp |
11 | | /// @brief The main namespace for the PcapPlusPlus lib |
12 | | namespace pcpp |
13 | | { |
14 | | /// Class for representing the Telnet Layer |
15 | | class TelnetLayer : public Layer |
16 | | { |
17 | | private: |
18 | | // Position iterator for next command |
19 | | size_t lastPositionOffset; |
20 | | |
21 | | // Returns distance to next IAC |
22 | | size_t distanceToNextIAC(uint8_t* startPos, size_t maxLength) const; |
23 | | // Returns length of provided field |
24 | | size_t getFieldLen(uint8_t* startPos, size_t maxLength) const; |
25 | | // Get position of next data field |
26 | | uint8_t* getNextDataField(uint8_t* pos, size_t len) const; |
27 | | // Get position of next command field |
28 | | uint8_t* getNextCommandField(uint8_t* pos, size_t len) const; |
29 | | // Get options of provided field |
30 | | int16_t getSubCommand(const uint8_t* pos, size_t len) const; |
31 | | // Get data of provided field |
32 | | uint8_t* getCommandData(uint8_t* pos, size_t& slen) const; |
33 | | |
34 | | public: |
35 | | /// Telnet Command Indicator |
36 | | enum class TelnetCommand : int |
37 | | { |
38 | | /// Indicator to parser reached end of packet |
39 | | TelnetCommandEndOfPacket = -1, |
40 | | |
41 | | /// End of file |
42 | | EndOfFile = 236, |
43 | | /// Suspend current process |
44 | | Suspend, |
45 | | /// Abort Process |
46 | | Abort, |
47 | | /// End of Record |
48 | | EndOfRecordCommand, |
49 | | /// Marks the end of a Telnet option subnegotiation, used with the SB code to specify more specific option |
50 | | /// parameters. |
51 | | SubnegotiationEnd, |
52 | | /// Null command; does nothing. |
53 | | NoOperation, |
54 | | /// Used to mark the end of a sequence of data that the recipient should scan for urgent Telnet commands. |
55 | | DataMark, |
56 | | /// Represents the pressing of the “break” or “attention” key on the terminal. |
57 | | Break, |
58 | | /// Tells the recipient to interrupt, abort, suspend or terminate the process currently in use. |
59 | | InterruptProcess, |
60 | | /// Instructs the remote host to continue running the current process, but discard all remaining output from |
61 | | /// it. This may be needed if a program starts to send unexpectedly large amounts of data to the user. |
62 | | AbortOutput, |
63 | | /// May be used to check that the remote host is still “alive”. When this character is sent the remote host |
64 | | /// returns some type of output to indicate that it is still functioning. |
65 | | AreYouThere, |
66 | | /// Instructs the recipient to delete the last undeleted character from the data stream. Used to “undo” the |
67 | | /// sending of a character. |
68 | | EraseCharacter, |
69 | | /// Tells the recipient to delete all characters from the data stream back to (but not including) the last |
70 | | /// end of line (CR+LF) sequence. |
71 | | EraseLine, |
72 | | /// Used in Telnet half-duplex mode to signal the other device that it may transmit. |
73 | | GoAhead, |
74 | | /// Marks the beginning of a Telnet option subnegotiation, used when an option requires the client and |
75 | | /// server to exchange parameters. |
76 | | Subnegotiation, |
77 | | /// Indicates that the device sending this code is willing to perform or continue performing a particular |
78 | | /// option. |
79 | | WillPerform, |
80 | | /// Indicates that the device sending this code is either not willing to perform a particular option, or is |
81 | | /// now refusing to continue to perform it. |
82 | | WontPerform, |
83 | | /// Requests that the other device perform a particular option or confirms the expectation that the other |
84 | | /// device will perform that option. |
85 | | DoPerform, |
86 | | /// Specifies that the other party not perform an option, or confirms a device’s expectation that the other |
87 | | /// party not perform an option. |
88 | | DontPerform, |
89 | | /// Precedes command values 240 through 254 as described above. A pair of IAC bytes in a row represents the |
90 | | /// data value 255. |
91 | | InterpretAsCommand |
92 | | }; |
93 | | |
94 | | /// Telnet Options |
95 | | enum class TelnetOption : int |
96 | | { |
97 | | /// Internal return for no option detected |
98 | | TelnetOptionNoOption = -1, |
99 | | |
100 | | /// Binary Transmission RFC856 https://www.iana.org/go/rfc856 |
101 | | TransmitBinary = 0, |
102 | | /// Echo RFC857 https://www.iana.org/go/rfc857 |
103 | | Echo, |
104 | | /// Reconnection |
105 | | Reconnection, |
106 | | /// Suppress Go Ahead RFC858 https://www.iana.org/go/rfc858 |
107 | | SuppressGoAhead, |
108 | | /// Negotiate approximate message size |
109 | | ApproxMsgSizeNegotiation, |
110 | | /// Status RFC859 https://www.iana.org/go/rfc859 |
111 | | Status, |
112 | | /// Timing Mark RFC860 https://www.iana.org/go/rfc860 |
113 | | TimingMark, |
114 | | /// RCTE, Remote Controlled Transmission and Echo RFC726 https://www.iana.org/go/rfc726 |
115 | | RemoteControlledTransAndEcho, |
116 | | /// Output Line Width |
117 | | OutputLineWidth, |
118 | | /// Output Page Size |
119 | | OutputPageSize, |
120 | | /// NAOCRD, Negotiate About Output Carriage-Return Disposition RFC652 https://www.iana.org/go/rfc652 |
121 | | OutputCarriageReturnDisposition, |
122 | | /// NAOHTS, Negotiate About Output Horizontal Tabstops RFC653 https://www.iana.org/go/rfc653 |
123 | | OutputHorizontalTabStops, |
124 | | /// NAOHTD, Negotiate About Output Horizontal Tab Disposition RFC654 https://www.iana.org/go/rfc654 |
125 | | OutputHorizontalTabDisposition, |
126 | | /// NAOFFD, Negotiate About Output Formfeed Disposition RFC655 https://www.iana.org/go/rfc655 |
127 | | OutputFormfeedDisposition, |
128 | | /// NAOVTS, Negotiate About Vertical Tabstops RFC656 https://www.iana.org/go/rfc656 |
129 | | OutputVerticalTabStops, |
130 | | /// NAOVTD, Negotiate About Output Vertcial Tab Disposition RFC657 https://www.iana.org/go/rfc657 |
131 | | OutputVerticalTabDisposition, |
132 | | /// NAOLFD, Negotiate About Output Linefeed Disposition RFC658 https://www.iana.org/go/rfc658 |
133 | | OutputLinefeedDisposition, |
134 | | /// Extended ASCII RFC698 https://www.iana.org/go/rfc698 |
135 | | ExtendedASCII, |
136 | | /// Logout RFC727 https://www.iana.org/go/rfc727 |
137 | | Logout, |
138 | | /// BM, Byte Macro RFC735 https://www.iana.org/go/rfc735 |
139 | | ByteMacro, |
140 | | /// Data Entry Terminal RFC1043 - RFC732 https://www.iana.org/go/rfc1043 https://www.iana.org/go/rfc732 |
141 | | DataEntryTerminal, |
142 | | /// SUPDUP RFC736 - RFC734 https://www.iana.org/go/rfc736 https://www.iana.org/go/rfc734 |
143 | | SUPDUP, |
144 | | /// SUPDUP Output RFC749 https://www.iana.org/go/rfc749 |
145 | | SUPDUPOutput, |
146 | | /// Send Location RFC779 https://www.iana.org/go/rfc779 |
147 | | SendLocation, |
148 | | /// Terminal Type RFC1091 https://www.iana.org/go/rfc1091 |
149 | | TerminalType, |
150 | | /// End of record RFC885 https://www.iana.org/go/rfc885 |
151 | | EndOfRecordOption, |
152 | | /// TUID, TACACS User Identification RFC927 https://www.iana.org/go/rfc927 |
153 | | TACACSUserIdentification, |
154 | | /// OUTMRK, Output Marking RFC933 https://www.iana.org/go/rfc933 |
155 | | OutputMarking, |
156 | | /// TTYLOC, Terminal Location Number RFC946 https://www.iana.org/go/rfc946 |
157 | | TerminalLocationNumber, |
158 | | /// Telnet 3270 Regime RFC1041 https://www.iana.org/go/rfc1041 |
159 | | Telnet3270Regime, |
160 | | /// X.3 PAD RFC1053 https://www.iana.org/go/rfc1053 |
161 | | X3Pad, |
162 | | /// NAWS, Negotiate About Window Size RFC1073 https://www.iana.org/go/rfc1073 |
163 | | NegotiateAboutWindowSize, |
164 | | /// Terminal Speed RFC1079 https://www.iana.org/go/rfc1079 |
165 | | TerminalSpeed, |
166 | | /// Remote Flow Control RFC1372 https://www.iana.org/go/rfc1372 |
167 | | RemoteFlowControl, |
168 | | /// Line Mode RFC1184 https://www.iana.org/go/rfc1184 |
169 | | Linemode, |
170 | | /// X Display Location RFC1096 https://www.iana.org/go/rfc1096 |
171 | | XDisplayLocation, |
172 | | /// Environment Option RFC1408 https://www.iana.org/go/rfc1408 |
173 | | EnvironmentOption, |
174 | | /// Authentication Option RFC2941 https://www.iana.org/go/rfc2941 |
175 | | AuthenticationOption, |
176 | | /// Encryption Option RFC2946 https://www.iana.org/go/rfc2946 |
177 | | EncryptionOption, |
178 | | /// New Environment Option RFC1572 https://www.iana.org/go/rfc1572 |
179 | | NewEnvironmentOption, |
180 | | /// TN3270E RFC2355 https://www.iana.org/go/rfc2355 |
181 | | TN3270E, |
182 | | /// X Server Authentication |
183 | | XAuth, |
184 | | /// Charset RFC2066 https://www.iana.org/go/rfc2066 |
185 | | Charset, |
186 | | /// RSP, Telnet Remote Serial Port |
187 | | TelnetRemoteSerialPort, |
188 | | /// Com Port Control Option RFC2217 https://www.iana.org/go/rfc2217 |
189 | | ComPortControlOption, |
190 | | /// Telnet Suppress Local Echo |
191 | | TelnetSuppressLocalEcho, |
192 | | /// Telnet Start TLS |
193 | | TelnetStartTLS, |
194 | | /// Kermit RFC2840 https://www.iana.org/go/rfc2840 |
195 | | Kermit, |
196 | | /// Send URL |
197 | | SendURL, |
198 | | /// Forward X Server |
199 | | ForwardX, |
200 | | |
201 | | /// Telnet Option Pragma Logon |
202 | | TelOptPragmaLogon = 138, |
203 | | /// Telnet Option SSPI Logon |
204 | | TelOptSSPILogon, |
205 | | /// Telnet Option Pragma Heartbeat |
206 | | TelOptPragmaHeartbeat, |
207 | | |
208 | | /// Extended option list |
209 | | ExtendedOptions = 255 |
210 | | }; |
211 | | |
212 | | /// A constructor that creates the layer from an existing packet raw data |
213 | | /// @param[in] data A pointer to the raw data |
214 | | /// @param[in] dataLen Size of the data in bytes |
215 | | /// @param[in] prevLayer A pointer to the previous layer |
216 | | /// @param[in] packet A pointer to the Packet instance where layer will be stored in |
217 | | TelnetLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) |
218 | 0 | : Layer(data, dataLen, prevLayer, packet, Telnet) |
219 | 0 | { |
220 | 0 | lastPositionOffset = SIZE_MAX; |
221 | 0 | }; |
222 | | |
223 | | /// Get the Telnet data as readable string |
224 | | /// @param[in] removeEscapeCharacters Whether non-alphanumerical characters should be removed or not |
225 | | /// @return Full payload as readable string, empty if Telnet packet contains control commands/options. |
226 | | std::string getDataAsString(bool removeEscapeCharacters = true) const; |
227 | | |
228 | | /// Get the total number of detected Telnet commands |
229 | | /// @return size_t The number of Telnet commands |
230 | | size_t getTotalNumberOfCommands(); |
231 | | |
232 | | /// Returns the number of occurrences of provided command |
233 | | /// @param[in] command Telnet command to count |
234 | | /// @return size_t Number of occurrences of command |
235 | | size_t getNumberOfCommands(TelnetCommand command); |
236 | | |
237 | | /// Returns the first command of packet |
238 | | /// @return TelnetCommand First detected command value, TelnetCommandEndOfPacket if there is no command field |
239 | | TelnetCommand getFirstCommand(); |
240 | | |
241 | | /// Returns the next command of packet. Uses an internal iterator. The iterator resets when reached end of |
242 | | /// packet. |
243 | | /// @return TelnetCommand Detected command value, TelnetCommandEndOfPacket if reached the end of packet. |
244 | | TelnetCommand getNextCommand(); |
245 | | |
246 | | /// Returns the option of current command. Uses an internal iterator. Iterator can be moved with getNextCommand |
247 | | /// @return TelnetOption Option of current command |
248 | | TelnetOption getOption(); |
249 | | |
250 | | /// Returns the option of provided command. It will return option of first occurrence of the command |
251 | | /// @param[in] command Telnet command to search |
252 | | /// @return TelnetOption Option of the command. Returns TelnetOptionNoOption if the provided command not found. |
253 | | TelnetOption getOption(TelnetCommand command); |
254 | | |
255 | | /// Returns the data of current command. Uses an internal iterator. Iterator can be moved with getNextCommand |
256 | | /// @param[out] length Length of the data of current command |
257 | | /// @return uint8_t* Pointer to the data of current command. nullptr if there is no data for this command. |
258 | | uint8_t* getOptionData(size_t& length); |
259 | | |
260 | | /// Returns the data of provided command. It will return data of first occurrence of the command |
261 | | /// @param[in] command Telnet command to search |
262 | | /// @param[out] length Length of the data of current command |
263 | | /// @return uint8_t* Pointer to the data of current command. nullptr if there is no data for this command or if |
264 | | /// can't find the command. |
265 | | uint8_t* getOptionData(TelnetCommand command, size_t& length); |
266 | | |
267 | | /// Convert the Telnet Command to readable string |
268 | | /// @param[in] val Value of the command |
269 | | /// @return The Telnet Command as readable string |
270 | | static std::string getTelnetCommandAsString(TelnetCommand val); |
271 | | |
272 | | /// Convert the Telnet option to readable string |
273 | | /// @param[in] val Value of the option |
274 | | /// @return The Telnet Option as readable string |
275 | | static std::string getTelnetOptionAsString(TelnetOption val); |
276 | | |
277 | | /// A static method that checks whether the port is considered as Telnet |
278 | | /// @param[in] port The port number to be checked |
279 | | static bool isTelnetPort(uint16_t port) |
280 | 0 | { |
281 | 0 | return port == 23; |
282 | 0 | } |
283 | | |
284 | | /// A static method that takes a byte array and detects whether it is a Telnet message |
285 | | /// @param[in] data A byte array |
286 | | /// @param[in] dataSize The byte array size (in bytes) |
287 | | /// @return True if the data is identified as Telnet message |
288 | | static bool isDataValid(const uint8_t* data, size_t dataSize) |
289 | 0 | { |
290 | 0 | return data && dataSize; |
291 | 0 | } |
292 | | |
293 | | // overridden methods |
294 | | |
295 | | /// Parses the next layer. Telnet is the always last so does nothing for this layer |
296 | | void parseNextLayer() override |
297 | 0 | {} |
298 | | |
299 | | /// @return Get the size of the layer |
300 | | size_t getHeaderLen() const override |
301 | 0 | { |
302 | 0 | return m_DataLen; |
303 | 0 | } |
304 | | |
305 | | /// Does nothing for this layer |
306 | | void computeCalculateFields() override |
307 | 0 | {} |
308 | | |
309 | | /// @return The OSI layer level of Telnet (Application Layer). |
310 | | OsiModelLayer getOsiModelLayer() const override |
311 | 0 | { |
312 | 0 | return OsiModelApplicationLayer; |
313 | 0 | } |
314 | | |
315 | | /// @return Returns the protocol info as readable string |
316 | | std::string toString() const override; |
317 | | |
318 | | /// @struct SerializedFields |
319 | | /// Fields written by TelnetLayer's serializeLayer(), in addition to |
320 | | /// Layer::SerializedFields. |
321 | | struct SerializedFields : Layer::SerializedFields |
322 | | { |
323 | | /// @return All field descriptors for TelnetLayer |
324 | | static std::vector<FieldDescriptor> all() |
325 | 0 | { |
326 | 0 | auto result = Layer::SerializedFields::all(); |
327 | 0 | std::initializer_list<FieldDescriptor> extra{ Data, Commands }; |
328 | 0 | std::copy(extra.begin(), extra.end(), std::back_inserter(result)); |
329 | 0 | return result; |
330 | 0 | } |
331 | | |
332 | | /// @struct CommandObject |
333 | | /// Fields describing one element of the Commands array |
334 | | struct CommandObject : ObjectFieldDescriptor<CommandObject> |
335 | | { |
336 | | using ObjectFieldDescriptor::ObjectFieldDescriptor; |
337 | | |
338 | | /// @return All field descriptors for one Telnet command |
339 | | static std::vector<FieldDescriptor> all() |
340 | 0 | { |
341 | 0 | return { Command, CommandName, Option, OptionName }; |
342 | 0 | } |
343 | | |
344 | | /// @brief Telnet command code, written as a number (see TelnetCommand) |
345 | | static const FieldDescriptor Command; |
346 | | |
347 | | /// @brief Telnet command as a readable string (see getTelnetCommandAsString()) |
348 | | static const FieldDescriptor CommandName; |
349 | | |
350 | | /// @brief Telnet option code, written as a number (see TelnetOption). Omitted when the |
351 | | /// command has no option. |
352 | | static const FieldDescriptor Option; |
353 | | |
354 | | /// @brief Telnet option as a readable string (see getTelnetOptionAsString()). Omitted when |
355 | | /// the command has no option. |
356 | | static const FieldDescriptor OptionName; |
357 | | }; |
358 | | |
359 | | /// @brief Telnet user data as a readable string (see getDataAsString()). Empty for |
360 | | /// control-only packets. |
361 | | static const FieldDescriptor Data; |
362 | | |
363 | | /// @brief Array of Telnet commands in packet order. Elements are described by CommandObject. |
364 | | static const FieldDescriptor Commands; |
365 | | }; |
366 | | |
367 | | protected: |
368 | | void serializeLayer(ObjectScope& serializer) const override; |
369 | | }; |
370 | | |
371 | | } // namespace pcpp |