power.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. # Import modules
  4. from core.logger import Log
  5. from os import system, getlogin
  6. from dbus import SystemBus, Interface
  7. from core.messages import services as Messages
  8. """
  9. Author : LimerBoy
  10. github.com/LimerBoy/BlazeRAT
  11. Notes :
  12. The file is needed
  13. to control system power.
  14. """
  15. # System bus
  16. system_bus = SystemBus()
  17. lg = system_bus.get_object(
  18. "org.freedesktop.login1",
  19. "/org/freedesktop/login1")
  20. power_management = Interface(lg, "org.freedesktop.login1.Manager")
  21. # PowerOff, Reboot, Suspend
  22. def _GetMethod(name: str):
  23. return power_management.get_dbus_method(name)
  24. # Shutdown computer
  25. def Shutdown():
  26. _GetMethod("PowerOff")(True)
  27. # Restart computer
  28. def Restart():
  29. _GetMethod("Reboot")(True)
  30. # Suspend computer
  31. def Suspend():
  32. _GetMethod("Suspend")(True)
  33. # Log out from current user
  34. def LogOut():
  35. return system("pkill -KILL -u " + getlogin()) == 0
  36. """ Handle telegram command """
  37. def Handle(callback: dict, bot) -> None:
  38. action = callback.data.split("_")[1]
  39. chatid = callback.from_user.id
  40. Log("Power >> Send power event " + action, chatid)
  41. bot.send_message(chatid, Messages.power_received % action)
  42. try:
  43. if action == "SHUTDOWN":
  44. Shutdown()
  45. elif action == "REBOOT":
  46. Restart()
  47. elif action == "SUSPEND":
  48. Suspend()
  49. elif action == "LOGOUT":
  50. LogOut()
  51. except Exception as error:
  52. ex = f"Power >> Error while running {action} action\n{error}"
  53. Log(ex, chatid)
  54. bot.send_message(chatid, ex)