Port scanner in Python


SUBMITTED BY: alemotta

DATE: March 14, 2017, 4:36 p.m.

FORMAT: Text only

SIZE: 4.6 kB

HITS: 811

  1. Port scanner in Python
  2. Disclaimer
  3. This program is intended for individuals to test their own equipment for weak
  4. security, and the author will take no responsibility if it is put to any other use
  5. Overview
  6. This post will show how you can make a small and easy-to-use port scanner program
  7. written in Python.
  8. There are many ways of doing this with Python, and I'm going to do it using the
  9. built-in module Socket.
  10. Sockets
  11. The socket module in Python provides access to the BSD socket interface.
  12. It includes the socket class, for handling the actual data channel, and functions
  13. for network-related tasks such as converting a server’s name to an address and
  14. formatting data to be sent across the network. Source
  15. Sockets are widely used on the Internet, as they are behind any kind of
  16. network communications done by your computer.
  17. The INET sockets, account for at least 99% of the sockets in use.
  18. The web browser’s that you use opens a socket and connects to the web server.
  19. Any network communication goes through a socket.
  20. For more reading about the socket module, please see the official documentation.
  21. Socket functions
  22. Before we get started with our sample program, let's see some of the socket
  23. functions we are going to use.
  24. sock = socket.socket (socket_family, socket_type)
  25. Syntax for creating a socket
  26. sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
  27. Creates a stream socket
  28. AF_INET
  29. Socket Family (here Address Family version 4 or IPv4)
  30. SOCK_STREAM
  31. Socket type TCP connections
  32. SOCK_DGRAM
  33. Socket type UDP connections
  34. gethostbyname("host")
  35. Translate a host name to IPv4 address format
  36. socket.gethostbyname_ex("host")
  37. Translate a host name to IPv4 address format, extended interface
  38. socket.getfqdn("8.8.8.8")
  39. Get the fqdn (fully qualified domain name)
  40. socket.gethostname()
  41. Returns the hostname of the machine..
  42. socket.error
  43. Exception handling
  44. Making a program using Python Sockets
  45. How to make a simple port scanner program in Python
  46. This small port scanner program will try to connect on every port you define for
  47. a particular host.
  48. The first thing we must do is import the socket library and other libraries that
  49. we need.
  50. Open up an text editor, copy & paste the code below. Save the file as:
  51. "portscanner.py" and exit the editor
  52. #!/usr/bin/env python
  53. import socket
  54. import subprocess
  55. import sys
  56. from datetime import datetime
  57. # Clear the screen
  58. subprocess.call('clear', shell=True)
  59. # Ask for input
  60. remoteServer = raw_input("Enter a remote host to scan: ")
  61. remoteServerIP = socket.gethostbyname(remoteServer)
  62. # Print a nice banner with information on which host we are about to scan
  63. print "-" * 60
  64. print "Please wait, scanning remote host", remoteServerIP
  65. print "-" * 60
  66. # Check what time the scan started
  67. t1 = datetime.now()
  68. # Using the range function to specify ports (here it will scans all ports between 1 and 1024)
  69. # We also put in some error handling for catching errors
  70. try:
  71. for port in range(1,1025):
  72. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  73. result = sock.connect_ex((remoteServerIP, port))
  74. if result == 0:
  75. print "Port {}: Open".format(port)
  76. sock.close()
  77. except KeyboardInterrupt:
  78. print "You pressed Ctrl+C"
  79. sys.exit()
  80. except socket.gaierror:
  81. print 'Hostname could not be resolved. Exiting'
  82. sys.exit()
  83. except socket.error:
  84. print "Couldn't connect to server"
  85. sys.exit()
  86. # Checking the time again
  87. t2 = datetime.now()
  88. # Calculates the difference of time, to see how long it took to run the script
  89. total = t2 - t1
  90. # Printing the information to screen
  91. print 'Scanning Completed in: ', total
  92. Sample output
  93. Let's run the program and see how an output can look like
  94. $ python portscanner.py
  95. Enter a remote host to scan: www.your_host_example.com
  96. ------------------------------------------------------------
  97. Please wait, scanning remote host xxxx.xxxx.xxxx.xxxx
  98. ------------------------------------------------------------
  99. Port 21: Open
  100. Port 22: Open
  101. Port 23: Open
  102. Port 80: Open
  103. Port 110: Open
  104. Port 111: Open
  105. Port 143: Open
  106. Port 443: Open
  107. Port 465: Open
  108. Port 587: Open
  109. Port 993: Open
  110. Port 995: Open
  111. Scanning Completed in: 0:06:34.705170
  112. Disclaimer
  113. This program is intended for individuals to test their own equipment for weak
  114. security, and the author will take no responsibility if it is put to any other use

comments powered by Disqus