Author Topic: [Python] nc-lp.py - Netcat-like listener  (Read 2403 times)

0 Members and 1 Guest are viewing this topic.

Offline frog

  • Knight
  • **
  • Posts: 232
  • Cookies: 16
    • View Profile
[Python] nc-lp.py - Netcat-like listener
« on: June 11, 2014, 11:55:39 am »
Code: (python) [Select]
#!/usr/bin/env python
##
### nc-lp.py - listen for a tcp connection on a certain port
##
#
import sys,socket,re

def usage():
    print("""\n    nc-lp.py - listen for incoming TCP connection
           usage: python nc-lp.py <port>""")

def listen(lport):
    lsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    lsocket.bind(("", int(lport)))
    lsocket.listen(1)
    print("[+] Listening on port " + lport)
    connection, address = lsocket.accept()
    print("[*] Connection from " + str(address[0]))

    while True:
        try:
            incoming = connection.recv(2048)
            print(str(incoming))
            outgoing = raw_input("@" + str(address[0]) + ">")

            while outgoing == "":
                outgoing = raw_input("@" + str(address[0]) + ">")

            if outgoing == "exit":
                print("[!] Exiting..")
                exit()

            connection.send(outgoing)
        except socket.error:
            print("[!] Error: Connection lost")
            exit()
        except socket.timeout:
            print("[!] Error: Connection timed out")
            exit()

def main():
    if len(sys.argv) < 2:
        usage()
        exit()

    search = re.compile("([0-9]{1,5})")
    lport = sys.argv[1]

    if search.match(lport):
        try:
            listen(lport)
        except OverflowError:
            print("[!] Error: port must be between 1-65535")
    else:
        print("[!] Error: port must be between 1-65535")

if __name__ == "__main__":
    main()
« Last Edit: June 12, 2014, 03:47:17 am by frog »

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: [Python] nc-lp.py - Netcat-like listener
« Reply #1 on: June 12, 2014, 04:01:49 am »
Nice. I'm actually about to get started on a Ruby script that is telnet-like, so this is interesting. Even if it is in a different language then the one I had planned on using.
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline DeXtreme

  • Peasant
  • *
  • Posts: 95
  • Cookies: 8
  • I was there and you never knew.
    • View Profile
    • My Designs
Re: [Python] nc-lp.py - Netcat-like listener
« Reply #2 on: June 12, 2014, 05:03:04 am »
Cool.I was also gonna write something similar also in python but to execute shell commands on remote systems.

Offline frog

  • Knight
  • **
  • Posts: 232
  • Cookies: 16
    • View Profile
Re: [Python] nc-lp.py - Netcat-like listener
« Reply #3 on: June 12, 2014, 06:21:35 am »
@lucid, Ruby is a cool looking language but it's like perl/python combined with a goofy-ass syntax. Your post reminds me of an article from 2010 in a hakin9 magazine. I was able to find it. The code in there is very comprehensive. Check it out. http://www.slideshare.net/keith55/manipulating-the-network-with-packet-fu

@DeXtreme, I was thinking the same thing(using python for a remote shell of some sort). The only real application I could think of for such a thing would be for the Apple computers on display at Best Buy. You could make the mac 'say' things when people walk by.

This is really just so I can test a reverse shell being dropped from a Teensy. I'm going to encode the reverse-shell binary(hex or base64) and store it in the Teensy's flashmem during runtime. I will make a separate thread with the details.

https://evilzone.org/projects-and-discussion/teensy-dropper-project-details-and-progress/msg84126/#msg84126
« Last Edit: June 12, 2014, 06:51:58 am by frog »

Offline DeXtreme

  • Peasant
  • *
  • Posts: 95
  • Cookies: 8
  • I was there and you never knew.
    • View Profile
    • My Designs
Re: [Python] nc-lp.py - Netcat-like listener
« Reply #4 on: June 12, 2014, 06:08:40 pm »
@lucid, Ruby is a cool looking language but it's like perl/python combined with a goofy-ass syntax. Your post reminds me of an article from 2010 in a hakin9 magazine. I was able to find it. The code in there is very comprehensive. Check it out. http://www.slideshare.net/keith55/manipulating-the-network-with-packet-fu

@DeXtreme, I was thinking the same thing(using python for a remote shell of some sort). The only real application I could think of for such a thing would be for the Apple computers on display at Best Buy. You could make the mac 'say' things when people walk by.

This is really just so I can test a reverse shell being dropped from a Teensy. I'm going to encode the reverse-shell binary(hex or base64) and store it in the Teensy's flashmem during runtime. I will make a separate thread with the details.

https://evilzone.org/projects-and-discussion/teensy-dropper-project-details-and-progress/msg84126/#msg84126


Sounds great. Heading over there now ;)