-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path200_Math.pl
More file actions
166 lines (130 loc) · 5.07 KB
/
Copy path200_Math.pl
File metadata and controls
166 lines (130 loc) · 5.07 KB
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
#! usr/bin/perl --
# Created by: Kevin Dolphin, NULLify
# Description: Intended for the NULLify 2013: A H4CK Odyssey CTF.
#---------------------------------------
use IO::Socket::INET;
use Modern::Perl;
use sigtrap 'handler' => \&sigtrap, 'HUP', 'INT','ABRT','QUIT','TERM';
use feature ":5.10";
my ($randNum1, $randNum2, $randOP, $value, $count, $UIV, @time);
my ($corCTR, $MAX, $PID, $SECONDS, $gotKey);
my ($socket, $client_socket, $peeraddy, $peerport);
my ($pid, $localHost, $localPort, $reuse);
# Init ----------------------------------------------------------
$randNum1=$randNum2=$randOP=();
$value=$count=$UIV=();
$gotKey = $corCTR = 0;
$MAX = 1; # Determine ___DAY OF____
$SECONDS = 5;
$localHost = '127.0.0.1'; # ___CHANGE DAY OF _____
$localPort = '11235';
$PID = 2500; # Arbitrary
# ----------------------------------------------------------------
# Key goes here --------------------------------------------------
my $key = "key{KEYKEYKEYKEYKEY}";
# ----------------------------------------------------------------
# Code Start ===================================================================================================================
$| = 1; # Flush
$socket = new IO::Socket::INET(
LocalHost=>$localHost, # Host, us
LocalPort=>$localPort, # port
Proto=>'tcp', # Type
Listen=>5,
Reuse=>5,
) or die "We have an error in socket creation: $!\n";
@time = localtime(time);
open STDERR, '>>', "./math_errorlog.txt"; # Log all the things
printf STDOUT ("Server has started. We are listening on $localPort (%02d:%02d:%02d)\n",($time[2] % 12), $time[1], $time[0]); # Confirm server start
printf STDERR ("Server has started. We are listening on $localPort (%02d:%02d:%02d)\n",($time[2] % 12), $time[1], $time[0]);
while(1){#... Accept
while($client_socket=$socket->accept()){
if ($pid = fork){ next; } # Create a new process
else { unless(defined $pid){ die "You dun screwed up\n"; } } # Don't mess this up
# -- Grab record keeping info
if ($client_socket){@time=localtime(time);} # Grab local time for record keeping
$peeraddy=$client_socket->peerhost(); # Grab IP
$peerport=$client_socket->peerport(); # Grab port
printf ("We have a connection from $peeraddy:$peerport at (%02d:%02d:%02d)\n", ($time[2]%12), $time[1], $time[0]);
printf STDERR ("We have a connection from $peeraddy:$peerport at (%02d:%02d:%02d)\n", ($time[2]%12), $time[1], $time[0]);
# -- End record keeping
print $client_socket "Welcome to Kevin's calculator (2.0)!\nI fixed some bugs from last time, apparently it was broken(?), who knew?\n";
print $client_socket "This time I can guarantee that this is just a simple calculator, good luck!\nYou have $MAX problems to do in $SECONDS seconds.\n\n";
select $client_socket;
alarm ($SECONDS);
local $SIG{ALRM} = sub { print "\nYou have run out of time. Quitting.\n"; &end};
while ($count++<$MAX){
$randNum1 = int(rand(100));
$randNum2 = int(rand(100));
$randOP = int(rand(3));
$UIV = "";
given ($randOP) {
when (1) { # Addition
$value = $randNum1 + $randNum2;
print "$count. $randNum1 + $randNum2\n";
print "Value equals: ";
$UIV = <$client_socket>;
if (defined($UIV)){
if ($UIV =~ /-?\d+/){
chomp ($UIV);
}
else{ &error; }
}
$corCTR++ if ($value == $UIV);
}
when (2) { # Subtraction
$value = $randNum1 - $randNum2;
print "$count. $randNum1 - $randNum2\n";
print "Value equals: ";
$UIV = <$client_socket>;
if (defined($UIV)){
if ($UIV =~ /-?\d+/){
chomp ($UIV);
}
else{ &error; }
}
$corCTR++ if ($value == $UIV);
}
default{ # Multiplication
$value = $randNum1 * $randNum2;
print "$count. $randNum1 * $randNum2\n";
print "Value equals: ";
$UIV = <$client_socket>;
if (defined($UIV)){
if ($UIV =~ /-?\d+/){
chomp ($UIV);
}
else{ &error; }
}
$corCTR++ if ($value == $UIV);
}
}
}
printf ("You scored a percentage of: %2.02f\n", ($corCTR/$MAX));
if (($corCTR/$MAX) == 1.00){
printf "$key\n";
$gotKey = 1;
}
else { printf "Sorry, percentage wasn't high enough. Better luck next time!\n"; }
&end;
}
}
sub end{
select STDOUT;
@time = localtime(time); my $hr=($time[2]%12); my $min=$time[1]; my $sec=$time[0];
printf ("$peeraddy:$peerport exiting with key value of $gotKey (%02d:%02d:%02d)\n", ($time[2]%12), $time[1], $time[0]); # Tell ourselves someone is exiting
shutdown($client_socket, 2); # Exit the client
$client_socket->close(); # Close client socket
printf STDERR ("$peeraddy:$peerport ($gotKey) has disconnected at %02d:%02d:%02d",$hr,$min,$sec); # Log our disconnects
die("\n");
}
sub error{
printf "\nYou used an invalid structure to answer the question. \nThis could mean that you used something other than the desired input.\nThe program will now exit.\n";
&end;
}
sub sigtrap(){
select STDOUT;
@time = localtime(time); # Logs
printf STDOUT "\nCaught interrupt PID ($$) on parent (%02d:%02d:%02d)\n", ($time[2]%12), $time[1], $time[0];
printf STDERR "\nCaught interrupt PID ($$) on parent (%02d:%02d:%02d)\n", ($time[2]%12), $time[1], $time[0];
exit();
}