Beispiel #1
0
    def run(self):
        try:
            while True:
                # retrieve the data sent by the client and check that they actually sent something (including "")
                # data = self.connection.recv(consts.BUFFER_SIZE).decode()  # note that the string is encoded as an aob

                oldTime = int(round(time.time() * 1000000))
                # reading = True
                # frags = []
                # while reading:
                #     chunk = self.connection.recv(4096).decode()
                #     if chunk[len(chunk) - 1:len(chunk)] == '\0':
                #         reading = False
                #         frags.append(chunk[0:len(chunk) - 1])  # remove the null, it'll mess up NetworkObject.initialize()
                #     else:
                #         frags.append(chunk)
                # data = ''.join(frags)

                # data = self.connection.recv(4096).decode()
                reading = True
                frags = []
                while reading:
                    chunk = self.connection.recv(4096).decode()

                    # recv can continuously return empty strings, depending on what the client does, without the check it'll go into an infinite loop
                    # this is fixable clientside, but that opens the server up to XSS, better to fix it here and let the client do w/e
                    if chunk[len(chunk) - 1:len(chunk)] == '\0' or chunk == "":
                        reading = False
                        frags.append(
                            chunk[0:len(chunk) - 1]
                        )  # remove the null, it'll mess up NetworkObject.initialize()
                    else:
                        frags.append(chunk)
                data = "".join(frags)

                # data = data[0:len(data) - 1]
                print("Time elapsed:",
                      int(round(time.time() * 1000000)) - oldTime)

                if not data:
                    self.connection.send(
                        str.encode(consts.ERROR_UNKNOWN + '\0'))
                else:
                    networkObject = NetworkObject()
                    networkObject.initialize(data)

                    result = self.parseNetworkObject(networkObject)

                    self.connection.send(str.encode(result.serialize() +
                                                    '\0'))  # send result back
        except socket.error as e:
            if (e.errno == errno.EPIPE
                ):  # broken pipe error, not sure where this is defined
                print("[-] Client disconnected.")
            else:
                raise
        except Exception as e:
            print(e)
        finally:
            self.connection.close()
Beispiel #2
0
    def sendString(self, string):
        ret = NetworkObject()
        try:
            if self.noServer:
                self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                self.sslSock = ssl.wrap_socket(
                    self.socket,
                    cert_reqs=ssl.CERT_REQUIRED,
                    ca_certs="network/certs/serverCert.pem")
                self.sslSock.connect((consts.TCP_IP, consts.TCP_PORT))
                self.noServer = False

            self.sslSock.send(str.encode(string + '\0'))

            reading = True
            frags = []
            while reading:
                chunk = self.sslSock.recv(4096).decode()
                if chunk[len(chunk) - 1:len(chunk)] == '\0' or chunk == "":
                    reading = False
                    frags.append(
                        chunk[0:len(chunk) - 1]
                    )  # remove the null, it'll mess up NetworkObject.initialize()
                else:
                    frags.append(chunk)
            response = ''.join(frags)

            ret.initialize(response)
        except ConnectionRefusedError:
            self.noServer = True
            ret.error = consts.ERROR_CONNECTION_FAILED
        finally:
            return ret
    def test_initialize_withStrPayload(self):
        no = NetworkObject("a", "b", "c", "d", "e")

        no.initialize('{"key": "v", "error": "w", "command": "x", "payload": "y", "table": "z"}')

        self.assertEqual(no.key, "v")
        self.assertEqual(no.error, "w")
        self.assertEqual(no.command, "x")
        self.assertEqual(no.payload, "y")
        self.assertEqual(no.table, "z")
Beispiel #4
0
    def sendString(self, string):
        ret = NetworkObject()
        try:
            if self.noServer:
                self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                self.sslSock = ssl.wrap_socket(
                    self.socket,
                    cert_reqs=ssl.CERT_REQUIRED,
                    ca_certs="network/certs/serverCert.pem")
                self.sslSock.connect((consts.TCP_IP, consts.TCP_PORT))
                self.noServer = False

            self.sslSock.write(str.encode(string))
            response = self.sslSock.recv(self.bufferSize).decode(
            )  # make sure to convert from aob to string
            ret.initialize(response)
        except ConnectionRefusedError:
            self.noServer = True
            ret.error = consts.ERROR_CONNECTION_FAILED
        finally:
            return ret