Python For Security

Hello reader,

Chances are you have wondered how some programming languages are implemented for security. C languages running behind the scenes on many heuristics based systems are becoming antiquated and scripting languages can perform so many tasks now that it almost becomes easier to have snippets for security tasks instead of whole programs.

Python Snippets

Python is a scripting language not unlike bash or php in that it doesn’t really require a compiler but rather just an interpreter to function. A snippet can be executed from within a shell, as a process by a daemon, or even from within the IDE itself.

Example of a python port scanner, I found this example some time back – I didn’t write it but I know it works fairly well in geany, ninja, and various other IDE. If you do attempt to copy this script remember to save with the extension .py as in scanner.py or it will simply not function.

#!/usr/bin/env python
import socket
import subprocess
import sys
from datetime import datetime

# Clear the screen
subprocess.call(‘clear’, shell=True)

# Ask for input
remoteServer = raw_input(“Enter a remote host to scan: “)
remoteServerIP = socket.gethostbyname(remoteServer)

# Print a nice banner with information on which host we are about to scan
print “-” * 60
print “Please wait, scanning remote host”, remoteServerIP
print “-” * 60

# Check what time the scan started
t1 = datetime.now()

# Using the range function to specify ports (here it will scans all ports between 1 and 1024)

# We also put in some error handling for catching errors

try:
for port in range(1,1025):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((remoteServerIP, port))
if result == 0:
print “Port {}: Open”.format(port)
sock.close()

except KeyboardInterrupt:
print “You pressed Ctrl+C”
sys.exit()

except socket.gaierror:
print ‘Hostname could not be resolved. Exiting’
sys.exit()

except socket.error:
print “Couldn’t connect to server”
sys.exit()

# Checking the time again
t2 = datetime.now()

# Calculates the difference of time, to see how long it took to run the script
total = t2 – t1

# Printing the information to screen
print ‘Scanning Completed in: ‘, total

To date I’ve found at least 35-40 snippets that can act as replacements for whole programs when it comes to security. I’m learning to implement quick changes to them for specific needs and incorporating php snippets as well.

A quick tip for output – much like in bash you can output to a text file at need by using the > symbol. Example output > python.txt

Show Your Friends & Followers!
About @AlwaysInTao 52 Articles
Author of 20 books and over 1000 articles, programmer in 8+ languages, website and software developer, Linux administrator. Certified ethical hacker

22 Comments

  1. I have recently started a web site, the information you offer on this website has helped me tremendously. Thank you for all of your time & work. “The achievements of an organization are the results of the combined effort of each individual.” by Vince Lombardi.

  2. Woah! I’m really loving the template/theme of this blog. It’s simple, yet effective. A lot of times it’s difficult to get that “perfect balance” between user friendliness and visual appeal. I must say you have done a amazing job with this. Also, the blog loads super quick for me on Opera. Excellent Blog!

  3. When do you think this Real Estate market will go back up? Or is it still too early to tell? We are seeing a lot of housing foreclosures in Casselberry Florida. What about you? Would love to get your feedback on this.

  4. I really like your blog.. very nice colors & theme. Did you make this website yourself or did you hire someone to do it for you? Plz answer back as I’m looking to design my own blog and would like to know where u got this from. thanks a lot

  5. I precisely had to say thanks yet again. I do not know the things I would have gone through in the absence of the type of methods documented by you concerning my question. This was a alarming condition for me, nevertheless looking at the very skilled strategy you dealt with that made me to leap for delight. Extremely thankful for your support and expect you are aware of an amazing job you are always carrying out instructing many others through a blog. I’m certain you haven’t met any of us.

  6. Youre so cool! I dont suppose Ive learn something like this before. So good to search out any person with some original ideas on this subject. realy thank you for starting this up. this web site is something that’s wanted on the net, someone with just a little originality. helpful job for bringing something new to the web!

  7. I wanted to write down a simple comment to say thanks to you for all the unique solutions you are sharing on this site. My time-consuming internet lookup has at the end of the day been paid with awesome facts and strategies to talk about with my family. I ‘d tell you that most of us readers are really endowed to live in a fabulous community with so many wonderful professionals with great guidelines. I feel rather happy to have used your entire web page and look forward to some more fabulous times reading here. Thank you once more for all the details.

  8. I precisely desired to say thanks once more. I’m not certain what I could possibly have tried in the absence of the type of suggestions revealed by you directly on this problem. It seemed to be a daunting issue in my opinion, but being able to view a specialized form you dealt with that made me to leap over delight. I’m just thankful for your service and thus sincerely hope you recognize what a great job that you’re putting in instructing the mediocre ones with the aid of your webpage. I know that you’ve never come across all of us.

  9. I would like to express some appreciation to you for bailing me out of this particular trouble. After surfing around throughout the world wide web and seeing principles which were not productive, I was thinking my life was gone. Existing without the approaches to the problems you have resolved as a result of the write-up is a critical case, and the ones that could have in a negative way damaged my career if I hadn’t come across your web blog. Your own personal knowledge and kindness in maneuvering all things was very useful. I am not sure what I would’ve done if I hadn’t encountered such a point like this. I can now relish my future. Thanks a lot very much for your expert and sensible guide. I will not think twice to suggest the sites to any individual who should receive support about this area.

Leave a Reply

Your email address will not be published.


*