-
Notifications
You must be signed in to change notification settings - Fork 14
/
tcpScanner.zig
205 lines (167 loc) · 7.06 KB
/
tcpScanner.zig
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
const std = @import("std");
const beacon = @import("bof_api").beacon;
const posix = @import("bof_api").posix;
const fmt = std.fmt;
const mem = std.mem;
const net = std.net;
const POLL_TIMEOUT = 300;
pub const linger = extern struct {
l_onoff: i32,
l_linger: i32,
};
fn extractPorts(allocator: mem.Allocator, port_spec: []const u8) ![]u16 {
var list = std.ArrayList(u16).init(allocator);
defer list.deinit();
var iter = mem.tokenize(u8, port_spec, ",");
while (iter.next()) |port_set| {
if (mem.containsAtLeast(u8, port_set, 1, "-")) {
// we're dealing with a port range, like: 1-3 in a set
var iter2 = mem.tokenize(u8, port_set, "-");
const first_port = fmt.parseInt(
u16,
iter2.next() orelse continue,
10,
) catch continue;
const last_port = fmt.parseInt(
u16,
iter2.next() orelse continue,
10,
) catch continue;
var n = first_port;
while (n <= last_port) {
try list.append(n);
n += 1;
}
} else {
// we're dealing with just one port number in a set
const port = fmt.parseInt(u16, port_set, 10) catch continue;
try list.append(port);
}
}
return list.toOwnedSlice();
}
fn extractIPs(allocator: mem.Allocator, ip_spec: []const u8) ![][]const u8 {
var list = std.ArrayList([]const u8).init(allocator);
defer list.deinit();
// ip_spec contains only single IP - add it to the list and return
if (!mem.containsAtLeast(u8, ip_spec, 1, "-")) {
try list.append(ip_spec);
return list.toOwnedSlice();
}
// splitting IP to get last octet for expansion (IP specification in a form us only supported x.x.x.1-3)
var iter = mem.split(u8, ip_spec, ".");
var i: u32 = 0;
var buf: [32]u8 = undefined;
var buf_index: usize = 0;
while (iter.next()) |ip_octet| {
// badly formatted ip_spec, return empty list
if (mem.eql(u8, ip_spec, ip_octet))
return error.BadData;
@memcpy(buf[buf_index..], ip_octet);
buf_index += ip_octet.len;
buf[buf_index] = '.';
buf_index += 1;
i += 1;
if (i == 3) break;
}
const ip_last_octet = iter.next() orelse return error.BadData;
// Expanding last octet
if (mem.containsAtLeast(u8, ip_last_octet, 1, "-")) {
var iter2 = mem.tokenize(u8, ip_last_octet, "-");
const sFirst_Num = iter2.next() orelse return error.BadData;
const first_num = fmt.parseInt(u16, sFirst_Num, 10) catch return error.BadData;
const sLast_Num = iter2.next() orelse return error.BadData;
const last_num = fmt.parseInt(u16, sLast_Num, 10) catch return error.BadData;
var n = first_num;
while (n <= last_num) {
try list.append(try fmt.allocPrint(allocator, "{s}{d}", .{ buf[0..buf_index], n }));
n += 1;
}
}
return list.toOwnedSlice();
}
pub export fn go(args: ?[*]u8, args_len: i32) callconv(.C) u8 {
if (args_len == 0) {
return 1; // err 1: no argument provided
}
var opt_len: i32 = 0;
const allocator = std.heap.page_allocator;
var parser = beacon.datap{};
// parse 1st (mandatory) argument:
beacon.dataParse(&parser, args, args_len);
const targets_spec = beacon.dataExtract(&parser, &opt_len);
const sTargets_spec = targets_spec.?[0..@as(usize, @intCast(opt_len - 1))];
// spliting IP:port specification argument to IPs and ports parts
var iter = mem.split(u8, sTargets_spec, ":");
const sIP_spec = iter.next() orelse unreachable;
const sPort_spec = iter.next() orelse unreachable;
// IPs to scan
const sIPs = extractIPs(allocator, sIP_spec) catch return 2; // err 2: invalid IPs provided
defer allocator.free(sIPs);
// ports to scan
var sPorts: []u16 = undefined;
sPorts = extractPorts(allocator, sPort_spec) catch return 3; // err 3: invalid port range provided
defer allocator.free(sPorts);
if (sIPs.len == 0 or sPorts.len == 0)
return 4; // err 4: nothing to scan
// preparing sockaddr struct
const family: std.posix.sa_family_t = std.posix.AF.INET;
var sa: net.Address = undefined;
@memset(@as([*]u8, @ptrCast(&sa))[0..@sizeOf(net.Address)], 0);
sa.any.family = family;
const lin = linger {
.l_onoff = 1,
.l_linger = 0,
};
//_ = lin;
// scanning
for (sIPs) |IP| {
_ = beacon.printf(0, "IP: %s\n", IP.ptr);
var dest_addr = net.Address.parseIp(IP, @as(u16, @intCast(0))) catch return 1;
for (sPorts) |port| {
_ = beacon.printf(0, "port: %d\n", port);
// creating socket
const sockfd = std.posix.socket(
std.posix.AF.INET,
std.posix.SOCK.STREAM | std.posix.SOCK.NONBLOCK,
0,
) catch return 1;
defer std.posix.close(sockfd);
dest_addr.setPort(port);
var pfd = [1]std.posix.pollfd{std.posix.pollfd{
.fd = sockfd,
.events = std.posix.POLL.OUT,
.revents = 0,
}};
//EINPROGRESS
// The socket is nonblocking and the connection cannot be
// completed immediately. (UNIX domain sockets failed with
// EAGAIN instead.) It is possible to select(2) or poll(2)
// for completion by selecting the socket for writing. After
// select(2) indicates writability, use getsockopt(2) to read
// the SO_ERROR option at level SOL_SOCKET to determine
// whether connect() completed successfully (SO_ERROR is
// zero) or unsuccessfully (SO_ERROR is one of the usual
// error codes listed here, explaining the reason for the
// failure).
std.posix.connect(sockfd, &dest_addr.any, dest_addr.getOsSockLen()) catch unreachable;
std.posix.setsockopt(sockfd, std.posix.SOL.SOCKET, std.posix.SO.LINGER, std.mem.asBytes(&lin)) catch unreachable;
// use this on Windows: https://github.com/ziglang/zig/blob/956f53beb09c07925970453d4c178c6feb53ba70/lib/std/os/windows.zig#L1687
const nevents = posix.poll(&pfd, POLL_TIMEOUT) catch 0;
_ = beacon.printf(0, "nevents: %d\n", nevents);
if ((pfd[0].revents & std.posix.POLL.OUT) != 0) {
// use this on Windows: https://ziglang.org/documentation/master/std/#std.os.windows.ws2_32.getsockopt
const rc = posix.getsockoptError(sockfd);
if(rc == error.ConnectionRefused) {
_ = beacon.printf(0, "Port %d closed on host %s\n", port, IP.ptr);
}
else
_ = beacon.printf(0, "Port %d opened on host %s\n", port, IP.ptr);
} else {
_ = beacon.printf(0, "Port %d filtered on host %s\n", port, IP.ptr);
}
}
_ = beacon.printf(0, "\n");
}
return 0;
}