Tutorial: Socket Testing Tool Source Code

July 31, 2017 | Views: 4086

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here

This time I will show you here a source code of a socket testing tool written in Python. This is a good, simple tool to test the Internet sockets using the IP of the target host and the port.

Here’s the source code:

`## Socket Testing Tool
 import sys
 import socket
 import getopt
 import threading
 import subprocess

listen = False
 command = False
 upload = False
 execute = ""
 target = ""
 upload_destination = ""
 port = 0

def usage ():
 print "Testing NET Tool"
 print
 print "Usage: socket_testing.py -t target_host -p port"
 print "-l --listen - listen on [host]:[port] for incoming connections"
 print "-e --execute=file_to_run - execute the given file upon receiving a connection"
 print "-c --command - initialize a command shell"
 print "-u --upload=destination - upon receiving connection upload a file and write to [destination]"
 print
 print
 print "Examples: "
 print "socket_testing.py -t 192.168.1.1 -p 10000 -l c"
 print "socket_testing.py -t 192.168.1.1 -p 10000 -l -u=c:\payload.exe"
 print "socket_testing.py -t 192.168.1.1 -p 10000 -l -e="cat /etc/passwd""
 print "echo 'ABCDEFGHI' | ./socket_testing.py -t 192.168.1.3 -p 135"
 sys.exit(0)

def main():
 global listen
 global port
 global execute
 global command
 global upload_destination
 global target

if not len(sys.argv[1:]):
 usage()

try:
 opts, args = getopt.getopt(sys.argv[1:],"hle:t:p:cu:",
 ["help","listen","execute","target","port","command","upload"])
 except getopt.GetoptError as err:
 print str(err)
 usage()

for o,a in opts:
 if o in ("-h","--help"):
 usage()
 elif o in ("-l","--listen"):
 listen = True
 elif o in ("-e", "--execute"):
 execute = a
 elif o in ("-c", "--commandshell"):
 command = True
 elif o in ("-u", "--upload"):
 upload_destination = a
 elif o in ("-t", "--target"):
 target = a
 elif o in ("-p", "--port"):
 port = int(a)
 else:
 assert False,"Unhandled Option"

if not listen and len(target) and port > 0:
 buffer = sys.stdin.read()
 client_sender(buffer)

if listen:
 server_loop()
 main()`

 

I hope you can learn with this tool. There are much more great examples in Python, but this is a small example. If you have questions, comment below or contact me directly.

Share with Friends
FacebookTwitterGoogle+LinkedInEmail
Use Cybytes and
Tip the Author!
Join
Share with Friends
FacebookTwitterGoogle+LinkedInEmail
Ready to share your knowledge and expertise?
6 Comments
  1. I need to get all ip and subnets on network for scanning. Any methods using cmd, powershell, batch, or python recommended. is nmap the only way to go. id like to stick to the command line.

    • To scan all ip and subnets on a network you have a very popular tool called Angry IP Scanner, but this is a GUI tool. If you want a CLI (command-line tool) I recommend you pnscan, it is faster that nmap buit it does just what you want, scan for ip and subnets on a large network.

  2. Hello, I want learn python to write scripts for pentesting, how to learn python?

    • Hello, first of all, it is recommended to learn the principles of pen testing, you have to know what attack methods and how to defend. For python you have to learn the basics and the network libraries to understand how they work.

  3. Use “argparse” to manage the options

  4. Nice script. Thanks!!

Comment on This

You must be logged in to post a comment.

Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

Cybrary On The Go

Get the Cybrary app for Android for online and offline viewing of our lessons.

Get it on Google Play
 

Support Cybrary

Donate Here to Get This Month's Donor Badge

 
Skip to toolbar

We recommend always using caution when following any link

Are you sure you want to continue?

Continue
Cancel