Skip to content

Commit 234ad21

Browse files
committed
merge
2 parents 76757cf + 745738b commit 234ad21

File tree

8 files changed

+156
-24
lines changed

8 files changed

+156
-24
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ client code `client.py`
1111

1212
code for logging in `account_login`
1313

14-
gameplay
14+
gameplay (intentional vulns are in these)
1515
`tictactoe`
1616
`high_score`
1717

18-
version stuff `globals`
18+
version stuff (ignore) `globals`
1919

2020
-----------------
2121
Welcome to our tic tac toe game! Please complete each level in ascending order, and try not to look at the code in a level you haven't gotten to yet!

account_login.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <stdio.h>
2+
#include <stdbool.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
6+
7+
#include "account_login.h"
8+
#include "helper.h"
9+
10+
char created_uname[0x100] = {0};
11+
char created_pass[0x100] = {0};
12+
13+
char currently_logged_in_uname[0x100] = {0};
14+
15+
bool check_auth(char* uname, char* passwd, bool* auth_success) {
16+
// stub
17+
return false;
18+
}
19+
20+
void get_clientstr_login_details(char* conn_str, char** uname_out, char** passwd_out) {
21+
// stub
22+
}
23+
24+
25+
void set_logged_in(char* conn_str, char* uname, char* passwd) {
26+
// stub
27+
}
28+
29+
30+
// vuln: VERSION1 stackoverflow to overwrite auth_success
31+
bool handle_login(int client_fd, char* client_str) {
32+
bool auth_success = false;
33+
char uname[0x100];
34+
char passwd[0x100];
35+
36+
if (!get_str_from_client(client_fd, uname)) {
37+
printf("handle_login error: get_str_from_client failed\n");
38+
return false;
39+
}
40+
41+
if (!get_str_from_client(client_fd, passwd)) {
42+
printf("handle_login error: get_str_from_client failed\n");
43+
return false;
44+
}
45+
46+
if (!check_auth(uname, passwd, &auth_success)) {
47+
printf("check_auth error\n");
48+
return false;
49+
}
50+
51+
if (auth_success) {
52+
log_verbose("logging in %s as %s\n", client_str, uname);
53+
set_logged_in(client_str, uname, passwd);
54+
}
55+
56+
// mav: if admin, logg out
57+
58+
return true;
59+
}
60+
61+
bool handle_logout(int client_fd, char* client_str) {
62+
currently_logged_in_uname[0] = '\0';
63+
return true;
64+
}
65+
66+
bool handle_create_user(int client_fd, char* client_str) {
67+
// stub
68+
return false;
69+
}
70+
71+
bool handle_admin_run_cmd(int client_fd, char* client_str) {
72+
/* let admin run any commands they want*/
73+
74+
if (0 !=strcmp(currently_logged_in_uname, "admin")) {
75+
return false;
76+
}
77+
78+
char cmd[0x1000];
79+
if (!get_str_from_client(client_fd, cmd)) {
80+
printf("handle_admin_run_cmd error: get_str_from_client failed\n");
81+
return false;
82+
}
83+
84+
system(cmd);
85+
return true;
86+
}

account_login.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef __ACCOUNT_LOGIN_H__
2+
# define __ACCOUNT_LOGIN_H__
3+
4+
#include <stdbool.h>
5+
6+
bool handle_login(int client_fd, char* client_str);
7+
bool handle_admin_run_cmd(int client_fd, char* client_str);
8+
bool handle_create_user(int client_fd, char* client_str);
9+
bool handle_logout(int client_fd, char* client_str);
10+
11+
#endif

client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def client_error_wrapper(error_msg):
1010

1111
class TicTacToe(object):
1212
"""interact with Stanford ZERO fun/vulnerable tic tac toe server"""
13-
GET_VERSION_PKT = b"\x00"
13+
GET_VERSION_PKT = b"\x00"*4
1414
def __init__(self, connect_tuple, debug = True):
1515
super(TicTacToe, self).__init__()
1616
self.connect_tuple = connect_tuple

game_dispatcher.c

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,27 @@
22
#include <stdio.h>
33
#include <sys/types.h>
44
#include <sys/socket.h>
5+
#include <arpa/inet.h>
56
#include <stdlib.h>
67
#include <unistd.h>
8+
#include <stdbool.h>
79

810
#include "globals.h"
911
#include "helper.h"
1012
#include "game_dispatcher.h"
1113

12-
void handle_get_version(int client_fd, char* client_str) {
13-
char version_str[0x100] = {0};
14-
sprintf(version_str, "Stanford Zero TicTacToe vesion %s", TICTACTOE_VERSION_STR);
15-
respond_str_to_client(client_fd, version_str); // in helper.c
16-
}
14+
typedef bool (*pkt_handler)(int client_fd, char* client_str);
15+
16+
pkt_handler handlers[] = {handle_get_version};
1717

1818
void handle_client(int client_fd, char* client_str) {
19-
char pkt_type;
19+
int pkt_type;
2020
int bytes_read;
2121

2222
printf("%s connected\n", client_str);
2323

2424
while (1) {
25-
26-
// read type
25+
// read packet type
2726
bytes_read = recv(client_fd, &pkt_type, sizeof(pkt_type), 0);
2827

2928
if (bytes_read == -1) {
@@ -40,17 +39,12 @@ void handle_client(int client_fd, char* client_str) {
4039
printf("only read %d for peer %s. Disconnecting.\n", bytes_read, client_str);
4140
goto end;
4241
}
42+
pkt_type = ntohl(pkt_type);
4343

44-
switch(pkt_type) {
45-
case 0:
46-
log_verbose("%s doing handle_get_version\n", client_str);
47-
handle_get_version(client_fd, client_str);
48-
break;
49-
50-
default:
51-
printf("stub code will handle unknown pkt type!\n");
52-
break;
53-
}
44+
// handle packet type
45+
printf("dispatching\n");
46+
bool dispatch_status = handlers[pkt_type](client_fd, client_str); // vuln: oob run any func lol
47+
printf("dispatch_status: %d\n", dispatch_status);
5448
}
5549
end:
5650
free(client_str);

helper.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22
#include <stdio.h>
33
#include <arpa/inet.h>
44
#include <string.h>
5+
#include <stdbool.h>
56

67
#include "helper.h"
8+
#include "globals.h"
9+
10+
bool handle_get_version(int client_fd, char* client_str) {
11+
char version_str[0x100] = {0};
12+
sprintf(version_str, "Stanford Zero TicTacToe vesion %s", TICTACTOE_VERSION_STR);
13+
respond_str_to_client(client_fd, version_str);
14+
return true;
15+
}
716

817
void respond_str_to_client(int fd, char* str) {
918
int len_htonl = htonl(strlen(str));
@@ -18,6 +27,31 @@ void respond_str_to_client(int fd, char* str) {
1827
}
1928
}
2029

30+
bool get_str_from_client(int client_fd, char* str) {
31+
int str_len;
32+
int bytes_read = recv(client_fd, &str_len, sizeof(str_len), 0);
33+
if (bytes_read == -1) {
34+
perror("recv from get_str_from_client in len");
35+
}
36+
if (bytes_read == 0) {
37+
printf("client disconnected in str len in get_str_from_client\n");
38+
return false;
39+
}
40+
if (bytes_read != sizeof(str_len)) {
41+
printf("client didn't send enough bytes for string length in get_str_from_client\n");
42+
return false;
43+
}
44+
45+
str_len = ntohl(str_len);
46+
log_verbose("will read str len %d\n", str_len);
47+
48+
recv(client_fd, str, str_len, 0);
49+
if (bytes_read == -1) {
50+
perror("recv from get_str_from_client in data");
51+
}
52+
return true;
53+
}
54+
2155
void uses_assumed_sizes() {
2256
if (sizeof(int) != 4) {
2357
printf("oh no! expected sizeof(int) to be size 4\n");
@@ -28,4 +62,5 @@ void uses_assumed_sizes() {
2862
void stub_exit(char * str) {
2963
printf("%s\n", str);
3064
exit(1);
31-
}
65+
}
66+

helper.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
#ifndef __HELPER_H__
22
# define __HELPER_H__
33

4-
#define log_verbose printf
4+
#include <stdbool.h>
55

6-
void uses_assumed_sizes();
6+
#define log_verbose printf
77

8+
// client communication helpers
9+
bool handle_get_version(int client_fd, char* client_str);
810
void respond_str_to_client(int fd, char* str);
911

12+
bool get_str_from_client(int fd, char* str);
13+
14+
// C heleprs
15+
void uses_assumed_sizes();
1016
void stub_exit(char * str);
1117

1218
#endif

server

-35.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)