1 /***************************************************************************
2  * ncat_core.h                                                             *
3  ***********************IMPORTANT NMAP LICENSE TERMS************************
4  *                                                                         *
5  * The Nmap Security Scanner is (C) 1996-2020 Insecure.Com LLC ("The Nmap  *
6  * Project"). Nmap is also a registered trademark of the Nmap Project.     *
7  *                                                                         *
8  * This program is distributed under the terms of the Nmap Public Source   *
9  * License (NPSL). The exact license text applying to a particular Nmap    *
10  * release or source code control revision is contained in the LICENSE     *
11  * file distributed with that version of Nmap or source code control       *
12  * revision. More Nmap copyright/legal information is available from       *
13  * https://nmap.org/book/man-legal.html, and further information on the    *
14  * NPSL license itself can be found at https://nmap.org/npsl. This header  *
15  * summarizes some key points from the Nmap license, but is no substitute  *
16  * for the actual license text.                                            *
17  *                                                                         *
18  * Nmap is generally free for end users to download and use themselves,    *
19  * including commercial use. It is available from https://nmap.org.        *
20  *                                                                         *
21  * The Nmap license generally prohibits companies from using and           *
22  * redistributing Nmap in commercial products, but we sell a special Nmap  *
23  * OEM Edition with a more permissive license and special features for     *
24  * this purpose. See https://nmap.org/oem                                  *
25  *                                                                         *
26  * If you have received a written Nmap license agreement or contract       *
27  * stating terms other than these (such as an Nmap OEM license), you may   *
28  * choose to use and redistribute Nmap under those terms instead.          *
29  *                                                                         *
30  * The official Nmap Windows builds include the Npcap software             *
31  * (https://npcap.org) for packet capture and transmission. It is under    *
32  * separate license terms which forbid redistribution without special      *
33  * permission. So the official Nmap Windows builds may not be              *
34  * redistributed without special permission (such as an Nmap OEM           *
35  * license).                                                               *
36  *                                                                         *
37  * Source is provided to this software because we believe users have a     *
38  * right to know exactly what a program is going to do before they run it. *
39  * This also allows you to audit the software for security holes.          *
40  *                                                                         *
41  * Source code also allows you to port Nmap to new platforms, fix bugs,    *
42  * and add new features.  You are highly encouraged to submit your         *
43  * changes as a Github PR or by email to the dev@nmap.org mailing list     *
44  * for possible incorporation into the main distribution. Unless you       *
45  * specify otherwise, it is understood that you are offering us very       *
46  * broad rights to use your submissions as described in the Nmap Public    *
47  * Source License Contributor Agreement. This is important because we      *
48  * fund the project by selling licenses with various terms, and also       *
49  * because the inability to relicense code has caused devastating          *
50  * problems for other Free Software projects (such as KDE and NASM).       *
51  *                                                                         *
52  * The free version of Nmap is distributed in the hope that it will be     *
53  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of  *
54  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties,        *
55  * indemnification and commercial support are all available through the    *
56  * Npcap OEM program--see https://nmap.org/oem.                            *
57  *                                                                         *
58  ***************************************************************************/
59 
60 /* $Id: ncat_core.h 38078 2020-10-02 16:12:22Z dmiller $ */
61 
62 #ifndef NCAT_CORE_H
63 #define NCAT_CORE_H
64 
65 #include "nsock.h"
66 #include "nbase.h"
67 #include "util.h"
68 #include "sockaddr_u.h"
69 
70 /* Maximum size of the srcaddrs array. In this case two because we can only have
71    a IPV4 INADDR_ANY and a IPV6 in6addr_any at most or a user defined address */
72 #define NUM_LISTEN_ADDRS 2
73 
74 /* Structure to store a linked list of resolved addresses. */
75 struct sockaddr_list {
76     union sockaddr_u addr;
77     size_t addrlen;
78     struct sockaddr_list* next;
79 };
80 
81 extern union sockaddr_u listenaddrs[NUM_LISTEN_ADDRS];
82 extern int num_listenaddrs;
83 
84 extern union sockaddr_u srcaddr;
85 extern size_t srcaddrlen;
86 
87 extern struct sockaddr_list *targetaddrs;
88 
89 enum exec_mode {
90     EXEC_PLAIN,
91     EXEC_SHELL,
92     EXEC_LUA,
93 };
94 
95 /* Proxy DNS resolution options (mask bits) */
96 #define PROXYDNS_LOCAL  1
97 #define PROXYDNS_REMOTE 2
98 
99 struct options {
100     unsigned int portno;
101 
102     int verbose;
103     int debug;
104     char *target;
105     int af;
106     /* IPPROTO_TCP, IPPROTO_SCTP, or IPPROTO_UDP */
107     int proto;
108     int broker;
109     int listen;
110     int keepopen;
111     int sendonly;
112     int recvonly;
113     int noshutdown;
114     int telnet;
115     int linedelay;
116     int chat;
117     int nodns;
118     const char *normlog;
119     const char *hexlog;
120     int normlogfd;
121     int hexlogfd;
122     int append;
123     int idletimeout;
124     int crlf;
125     /* Were any hosts specifically allowed? If so, deny all others. */
126     int allow;
127     int deny;
128     struct addrset *allowset;
129     struct addrset *denyset;
130     int httpserver;
131     int nsock_engine;
132     /* Output messages useful for testing to stderr? */
133     int test;
134 
135     /* Loose source-routing stuff */
136     struct in_addr srcrtes[8];
137     int numsrcrtes;
138     int srcrteptr;
139 
140     /* Maximum number of simultaneous connections */
141     int conn_limit;
142     int conntimeout;
143 
144     /* When execmode == EXEC_LUA, cmdexec is the name of the file to run. */
145     char *cmdexec;
146     enum exec_mode execmode;
147     char *proxy_auth;
148     char *proxytype;
149     char *proxyaddr;
150     int proxydns;
151 
152     int ssl;
153     char *sslcert;
154     char *sslkey;
155     int sslverify;
156     char *ssltrustfile;
157     char *sslciphers;
158     char* sslservername;
159     char *sslalpn;
160     int zerobyte;
161 };
162 
163 extern struct options o;
164 
165 /* The time the program was started, for exit statistics in connect mode. */
166 extern struct timeval start_time;
167 
168 /* Initializes global options to their default values. */
169 void options_init(void);
170 
171 /* Resolves the given hostname or IP address with getaddrinfo, and stores the
172    first result (if any) in *ss and *sslen. The value of port will be set in the
173    appropriate place in *ss; set to 0 if you don't care. af may be AF_UNSPEC, in
174    which case getaddrinfo may return e.g. both IPv4 and IPv6 results; which one
175    is first depends on the system configuration. Returns 0 on success, or a
176    getaddrinfo return code (suitable for passing to gai_strerror) on failure.
177    *ss and *sslen are always defined when this function returns 0.
178 
179    If the global o.nodns is true, then do not resolve any names with DNS. */
180 int resolve(const char *hostname, unsigned short port,
181             struct sockaddr_storage *ss, size_t *sslen, int af);
182 
183 /* Resolves the given hostname or IP address with getaddrinfo, and stores the
184    first result (if any) in *ss and *sslen. The value of port will be set in the
185    appropriate place in *ss; set to 0 if you don't care. af may be AF_UNSPEC, in
186    which case getaddrinfo may return e.g. both IPv4 and IPv6 results; which one
187    is first depends on the system configuration. Returns 0 on success, or a
188    getaddrinfo return code (suitable for passing to gai_strerror) on failure.
189    *ss and *sslen are always defined when this function returns 0.
190 
191    Resolve the hostname with DNS only if global o.proxydns includes PROXYDNS_LOCAL. */
192 int proxyresolve(const char *hostname, unsigned short port,
193             struct sockaddr_storage *ss, size_t *sslen, int af);
194 
195 /* Resolves the given hostname or IP address with getaddrinfo, and stores
196    all results into a linked list.
197    The rest of behavior is same as resolve(). */
198 int resolve_multi(const char *hostname, unsigned short port,
199         struct sockaddr_list *sl, int af);
200 
201 void free_sockaddr_list(struct sockaddr_list *sl);
202 
203 int fdinfo_close(struct fdinfo *fdn);
204 int fdinfo_recv(struct fdinfo *fdn, char *buf, size_t size);
205 int fdinfo_send(struct fdinfo *fdn, const char *buf, size_t size);
206 int fdinfo_pending(struct fdinfo *fdn);
207 
208 int ncat_recv(struct fdinfo *fdn, char *buf, size_t size, int *pending);
209 int ncat_send(struct fdinfo *fdn, const char *buf, size_t size);
210 
211 /* Broadcast a message to all the descriptors in fds. Returns -1 if any of the
212    sends failed. */
213 extern int ncat_broadcast(fd_set *fds, const fd_list_t *fdlist, const char *msg, size_t size);
214 
215 /* Do telnet WILL/WONT DO/DONT negotiations */
216 extern void dotelnet(int s, unsigned char *buf, size_t bufsiz);
217 
218 /* sleep(), usleep(), msleep(), Sleep() -- all together now, "portability".
219  *
220  * There is no upper or lower limit to the delayval, so if you pass in a short
221  * length of time <100ms, then you're likely going to get odd results.
222  * This is because the Linux timeslice is 10ms-200ms. So don't expect
223  * it to return for at least that long.
224  *
225  * Block until the specified time has elapsed, then return 1.
226  */
227 extern int ncat_delay_timer(int delayval);
228 
229 /* Open a logfile for writing.
230  * Return the open file descriptor. */
231 extern int ncat_openlog(const char *logfile, int append);
232 
233 extern void ncat_log_send(const char *data, size_t len);
234 
235 extern void ncat_log_recv(const char *data, size_t len);
236 
237 extern int ncat_hostaccess(char *matchaddr, char *filename, char *remoteip);
238 
239 /* Make it so that line endings read from a console are always \n (not \r\n).
240    Defined in ncat_posix.c and ncat_win.c. */
241 extern void set_lf_mode(void);
242 
243 extern int getaddrfamily(const char *addr);
244 extern int setenv_portable(const char *name, const char *value);
245 extern void setup_environment(struct fdinfo *fdinfo);
246 
247 #endif
248