-
Notifications
You must be signed in to change notification settings - Fork 0
/
RubenGrandeMuñoz_Yinkana.py
267 lines (205 loc) · 8.08 KB
/
RubenGrandeMuñoz_Yinkana.py
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/python3
# -*- coding: utf-8; mode: python -*-
import socket
import hashlib
import sys
import struct
import base64
import _thread
import urllib.request
import threading
################### COMMON FUNCTIONS ###################
def connect_at_TCP_server(node, port):
clientTCP = socket.socket()
clientTCP.connect((node, port))
return clientTCP
def receive_data(sock, ending):
data = bytes()
keep_receiving = True
while keep_receiving:
data_received = sock.recv(32)
data += data_received
if bytes(ending, encoding='utf-8') in data_received:
keep_receiving = False
return data
def get_next_challenge_identifier(current_challenge_instructions):
return current_challenge_instructions.decode().partition('\n')[0].partition(':')[2]
def receive_until_timeout(sock):
sock.settimeout(1)
bin_data = bytearray()
try:
while True:
bin_data += sock.recv(1024)
except socket.timeout:
print('All data has been received!')
sock.settimeout(None) #Dissabling timeouts on sockets operations for this object
return bin_data
#################### SPECIFIC CHALLENGE 3 FUNCTIONS ###############
def checkPalindrome(text_received):
text_as_list = text_received.decode().split()
palindrome = word_as_list = ''
for word in text_as_list:
if word.isdigit() or len(word) == 1:
continue
else:
word_as_list = list(word)
text_as_list = word_as_list.copy()
text_as_list.reverse()
palindrome_found = (word_as_list == text_as_list)
if palindrome_found:
palindrome = word
break
return palindrome
def invert_word(word):
word_list = list(word)
word_list.reverse()
return ''.join(word_list)
def get_before_palindrome(data, palindrome):
data_list = data.decode().split()
while data_list.pop() != palindrome:
pass
return data_list
#################### SPECIFIC CHALLENGE 5 FUNCTIONS ###############
# Copyright (C) 2009-2020 David Villa Alises
def sum16(data):
if len(data) % 2:
data = b'\0' + data
return sum(struct.unpack('!%sH' % (len(data) // 2), data))
def cksum(data):
sum_as_16b_words = sum16(data)
sum_1s_complement = sum16(struct.pack('!L', sum_as_16b_words))
_1s_complement = ~sum_1s_complement & 0xffff
return _1s_complement
#################### CHALLENGES SOLVING FUNCTIONS ###############
def solve_challenge0():
sock = connect_at_TCP_server('node1', 2000)
print(sock.recv(1024).decode())
sock.send('ruben.grande'.encode())
challenge1_instructions = sock.recv(1024)
print(challenge1_instructions.decode())
identifier = challenge1_instructions.decode().partition('\n')[0]
sock.close()
solve_challenge1(identifier)
def solve_challenge1(identifier):
myUDPServer = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
myUDPServer.bind(('', 40976))
myUDPServer.sendto(('40976 '+identifier).encode(), ('node1', 3000))
challenge2_instructions, server = myUDPServer.recvfrom(1024)
print(challenge2_instructions.decode())
identifier2 = get_next_challenge_identifier(challenge2_instructions)
solve_challenge2(identifier2)
def solve_challenge2(identifier2):
clientTCP = connect_at_TCP_server('node1', 4001)
data = receive_data(clientTCP, ' 0 ')
numbers = data.decode().split()
cont = 0
for number in numbers:
if int(number) == 0:
break
else:
cont += 1
message = str(identifier2 + ' ' + str(cont))
clientTCP.sendall(message.encode())
clientTCP.recv(256)
challenge3_instructions = receive_data(clientTCP, '>')
print(challenge3_instructions.decode())
clientTCP.close()
identifier3 = get_next_challenge_identifier(challenge3_instructions)
solve_challenge3(identifier3)
def solve_challenge3(identifier3):
clientTCP = connect_at_TCP_server('node1', 6000)
data = bytes()
index = 0
final_list = []
palindrome = ''
while True:
received = clientTCP.recv(1024)
data += received
palindrome = checkPalindrome(data)
if palindrome != '':
print('Palindrome is : ', palindrome, '\n')
final_list = get_before_palindrome(data, palindrome)
break
for word in final_list:
if word.isdigit():
pass
else:
final_list[index] = invert_word(word)
index += 1
message = ' '.join(final_list)
clientTCP.sendall(' '.join(final_list).encode())
clientTCP.sendall(('--'+identifier3+'--').encode())
challenge4_instructions = receive_data(clientTCP, '>')
print(challenge4_instructions.decode())
clientTCP.close()
identifier4 = get_next_challenge_identifier(challenge4_instructions)
solve_challenge4(identifier4)
def solve_challenge4(identifier4):
clientTCP = connect_at_TCP_server('node1', 10001)
clientTCP.sendall(identifier4.encode())
bin_data = receive_until_timeout(clientTCP)
# Removing the size of the bin file encoded in ASCII
for element in bin_data:
if bin_data.pop(0) == ord(':'): break
sha_sum = hashlib.sha1(bin_data)
clientTCP.sendall(sha_sum.digest())
challenge5_instructions = receive_data(clientTCP,'>')
print(challenge5_instructions.decode())
clientTCP.close()
identifier5 = get_next_challenge_identifier(challenge5_instructions)
solve_challenge5(identifier5)
def solve_challenge5(identifier5):
clientUDP = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
base64_id = base64.b64encode(bytes(identifier5, encoding='utf-8'))
message = struct.pack('!3sbhH%ds' % len(base64_id),b'WYP', 0, 0, 0, base64_id)
checksum = cksum(message)
message = struct.pack('!3sbhH%ds' % len(base64_id), b'WYP', 0, 0, checksum, base64_id)
clientUDP.sendto(message, ('node1',7001))
reply = clientUDP.recv(2048)
WYP_reply = struct.unpack('!3sbHH%ds' % (len(reply)-8), reply)
challenge6_instructions = base64.b64decode(WYP_reply[4]) #Decoding reply's payload
print(challenge6_instructions.decode())
clientUDP.close()
identifier6 = get_next_challenge_identifier(challenge6_instructions)
solve_challenge6(identifier6)
def solve_challenge6(identifier6):
sender = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Communication with node1
web_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
web_server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
web_server.bind(('',50000))
sender.connect(('node1',8003))
message = identifier6 + ' 50000'
sender.sendall(message.encode())
web_server.listen(25)
try:
web_server.settimeout(2)
while True:
client_connection, client = web_server.accept()
thread = threading.Thread(target=serve_resource, args=(client_connection,))
thread.start()
thread.join()
except socket.timeout:
pass
web_server.close()
sender.close()
def serve_resource(client_connection, url = 'https://uclm-arco.github.io/ietf-clone/rfc'):
request = client_connection.recv(1024).decode()
method, resource = request.split(' ')[0] , request.split(' ')[1]
resource_url = url+resource
if method == 'POST':
identifier7 = request.partition('code:')[2].partition('\n')[0]
print(request.partition('code:')[2])
solve_challenge7(identifier7)
return
reply_header = "HTTP/1.1 200 OK\r\n" +"Content-Type: text/plain\r\n" +"\r\n"
url_content = urllib.request.urlopen(resource_url).read()
HTTP_reply = reply_header.encode("utf-8") + url_content
client_connection.sendall(HTTP_reply)
client_connection.close()
def solve_challenge7(identifier7):
final_socket = connect_at_TCP_server('node1', 33333)
final_socket.sendall(identifier7.encode())
print(final_socket.recv(1024).decode())
print('[2020 GYMKANA\'S END!!!!!]')
if __name__ == '__main__':
solve_challenge0()