-
Notifications
You must be signed in to change notification settings - Fork 1
/
fuzzclient.pl
executable file
·139 lines (134 loc) · 4.36 KB
/
fuzzclient.pl
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
#!/usr/bin/perl
######################################################################
# Copyright (C) 2013 GeNUA mbH, 85551 Kirchheim, Germany.
# All rights reserved. Alle Rechte vorbehalten.
######################################################################
use strict;
use warnings;
use Socket;
use IO::Select;
use POSIX;
use feature 'switch';
my $BUFSIZE = 4096;
sub fuzzsock {
my $sock = shift;
my $proto = getprotobyname('tcp');
socket($sock, AF_INET, SOCK_STREAM, $proto)
or die "Could not create socket: $!";
setsockopt($sock, SOL_SOCKET, SO_REUSEADDR, 1);
fcntl($sock, F_SETFL(), O_NONBLOCK());
return $sock;
}
sub main {
my %verbs = ('accept' => 1, 'connect' => 1, 'close' => 1, 'bind' => 1, 'sendto' => 1,
'listen' => 1, 'recv' => 1, 'send' => 1, 'setsockopt' => 1, 'status' => 1, 'reset' => 1);
my $port = $ARGV[0];
print "This is the Fuzzing Client\n";
my $proto = getprotobyname('tcp');
socket(my $sock_control, AF_INET, SOCK_STREAM, $proto)
or die "Could not create socket: $!";
bind($sock_control, sockaddr_in($port, INADDR_ANY)) or die "bind failed: $!";
listen($sock_control, 5) or die "listen failed: $!";
setsockopt($sock_control, SOL_SOCKET, SO_REUSEADDR, 1) or die "Setsockopt failed: $!";
print "Listening on port $port\n";
my $sock;
my $select = IO::Select->new();
$select->add($sock_control);
my ($contr_port, $addr);
my $conn_control;
my $conn;
while(1) {
my @ready = $select->can_read(500);
foreach my $r (@ready) {
if ($r == $sock_control) {
accept($conn_control, $sock_control);
$select->add($conn_control);
my $iaddr = getpeername($conn_control);
($contr_port, $addr) = sockaddr_in($iaddr);
$sock = fuzzsock($sock);
print "Connection from: ".inet_ntoa($addr)." ".$contr_port."\n";
} else {
recv($r, my $msg, $BUFSIZE, 0);
if (!$msg) {
$select->remove($r);
close($r);
print "closed control connection\n";
close($sock);
$sock = fuzzsock($sock);
next;
}
my @cmds = $msg =~ /(\S+)/g;
my $verb = $cmds[0] if $cmds[0] or next;
my $arg = $cmds[1] if $cmds[1];
if (exists $verbs{$verb}) {
given ($verb) {
when ('accept') {
if (accept($sock, $sock)) {
send($conn_control, "accept OK\n", 0);
} else {
send($conn_control, $!."\n", 0);
}
}
when ('connect') {
if (connect($sock, sockaddr_in($arg, $addr))) {
send($conn_control, "connect OK\n", 0);
} else {
send($conn_control, $!."\n", 0);
}
}
when ('close') {
if (close($sock)) {
$sock = fuzzsock($sock);
send($conn_control, "close OK\n", 0);
} else {
send($conn_control, $!."\n", 0);
}
}
when ('bind') {
if (bind($sock, sockaddr_in($arg, INADDR_ANY))) {
send($conn_control, "bind OK\n", 0);
} else {
send($conn_control, $!."\n", 0);
}
}
when ('listen') {
if (listen($sock, 1)) {
send($conn_control, "listen OK\n", 0);
} else {
send($conn_control, $!."\n", 0);
}
}
when ('recv') {
if (recv($sock, my $recv_msg, $BUFSIZE, 0)) {
send($conn_control, "recv OK\n", 0);
} else {
send($conn_control, $!."\n", 0);
}
}
when ('send') {
if (send($sock, $arg, 0)) {
send($conn_control, "send OK\n", 0);
} else {
send($conn_control, $!."\n", 0);
}
}
when ('sendto') {
if (send($sock, $arg, 0, sockaddr_in($arg, $addr))) {
send($conn_control, "sendto OK\n", 0);
} else {
send($conn_control, $!."\n", 0);
}
}
when ('reset') {
close($sock);
send($conn_control, "reset OK\n", 0);
$sock = fuzzsock($sock);
}
default {next;}
}
}
}
}
}
}
&main();