This repository has been archived by the owner on Jul 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
knock.c
210 lines (191 loc) · 5.04 KB
/
knock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Rus V. Brushkoff
*
* ported to mingw, compile with:
*
* x86_64-w64-mingw32-gcc knock.c -lws2_32 -o knock.exe
*
* knock.c
*
* Copyright (c) 2004-2012 by Judd Vinet <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#ifdef __WIN32__
# include <winsock2.h>
# include <ws2tcpip.h>
#else
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <resolv.h>
# include <sys/socket.h>
#endif
#if defined(__FreeBSD__) || defined(__APPLE__)
#include <netinet/in.h>
#endif
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <fcntl.h>
static char version[] = "0.7";
#define PROTO_TCP 1
#define PROTO_UDP 2
/* function prototypes */
void vprint(char *fmt, ...);
void ver();
void usage();
int o_verbose = 0;
int o_udp = 0;
int o_delay = 0;
int main(int argc, char** argv)
{
int sd;
struct sockaddr_in addr;
int opt, optidx = 1;
static struct option opts[] =
{
{"verbose", no_argument, 0, 'v'},
{"udp", no_argument, 0, 'u'},
{"delay", required_argument, 0, 'd'},
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'V'},
{0, 0, 0, 0}
};
#ifdef __WIN32__
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) == SOCKET_ERROR) {
fprintf (stderr, "Error initialising WSA.\n");
return -1;
}
#endif
while((opt = getopt_long(argc, argv, "vud:hV", opts, &optidx))) {
if(opt < 0) {
break;
}
switch(opt) {
case 0: break;
case 'v': o_verbose = 1; break;
case 'u': o_udp = 1; break;
case 'd': o_delay = (int)atoi(optarg); break;
case 'V': ver();
case 'h': /* fallthrough */
default: usage();
}
}
if((argc - optind) < 2) {
usage();
}
if(o_delay < 0) {
fprintf(stderr, "error: delay cannot be negative\n");
exit(1);
}
struct addrinfo hints, *res;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_RAW;
if (getaddrinfo(argv[optind++], NULL, &hints, &res)) {
fprintf(stderr, "Cannot resolve hostname [%s]\n", argv[optind-1]);
exit(1);
}
for(; optind < argc; optind++) {
unsigned short port, proto = PROTO_TCP;
char *ptr, *arg = strdup(argv[optind]);
if((ptr = strchr(arg, ':'))) {
*ptr = '\0';
port = atoi(arg);
arg = ++ptr;
if(!strcmp(arg, "udp")) {
proto = PROTO_UDP;
} else {
proto = PROTO_TCP;
}
} else {
port = atoi(arg);
}
if(o_udp || proto == PROTO_UDP) {
sd = socket(PF_INET, SOCK_DGRAM, 0);
if(sd == -1) {
fprintf(stderr, "Cannot open UDP socket\n");
exit(1);
}
} else {
#ifdef __WIN32__
u_long flags;
#else
int flags;
#endif
sd = socket(PF_INET, SOCK_STREAM, 0);
if(sd == -1) {
fprintf(stderr, "Cannot open TCP socket\n");
exit(1);
}
#ifdef __WIN32__
flags = 1;
ioctlsocket(sd, FIONBIO, &flags);
#else
flags = fcntl(sd, F_GETFL, 0);
fcntl(sd, F_SETFL, flags | O_NONBLOCK);
#endif
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = ((const struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr; //*((long*)host->h_addr_list[0]);
addr.sin_port = htons(port);
if(o_udp || proto == PROTO_UDP) {
vprint("hitting udp %s:%u\n", inet_ntoa(addr.sin_addr), port);
int total = sendto(sd, "", 1, 0, (struct sockaddr*)&addr, sizeof(addr));
} else {
vprint("hitting tcp %s:%u\n", inet_ntoa(addr.sin_addr), port);
connect(sd, (struct sockaddr*)&addr, sizeof(struct sockaddr));
}
close(sd);
usleep(1000*o_delay);
}
return(0);
}
void vprint(char *fmt, ...)
{
va_list args;
if(o_verbose) {
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
fflush(stdout);
}
}
void usage() {
printf("usage: knock [options] <host> <port[:proto]> [port[:proto]] ...\n");
printf("options:\n");
printf(" -u, --udp make all ports hits use UDP (default is TCP)\n");
printf(" -d, --delay <t> wait <t> milliseconds between port hits\n");
printf(" -v, --verbose be verbose\n");
printf(" -V, --version display version\n");
printf(" -h, --help this help\n");
printf("\n");
printf("example: knock myserver.example.com 123:tcp 456:udp 789:tcp\n");
printf("\n");
exit(1);
}
void ver() {
printf("knock %s\n", version);
printf("Copyright (C) 2004-2012 Judd Vinet <[email protected]>\n");
exit(0);
}
/* vim: set ts=2 sw=2 noet: */