Writing Basic Security Tools Using Python: Ali Al-Shemery
Writing Basic Security Tools Using Python: Ali Al-Shemery
Ali Al-Shemery
aka B!n@ry, @binaryz0ne
Cited [2]
Cited
[1]
Outline
About Python
Python Basics
Types
Controls
Python Functions and Modules
Python Tips and Tricks
Coding for Penetration Testers
4
Ali Al-Shemery, @binaryz0ne 4
About Python
Interactive Interpreter
Text Editors
Vim, Nano,
Geany (my favorite)
Gedit, Kate,
Notepad++, etc
Ali Al-Shemery, @binaryz0ne 10
Python Basics
Integers (int)
>>> httpPort=80
>>> Subnet=24
Strings (str)
>>> url=http://www.linuxac.org/
Ali Al-Shemery, @binaryz0ne 11
Playing with Strings
String Concatenation
>>> userName = binary
>>> domainName = linuxac.org
>>> userEmail = userName + @ + domainName
>>> userEmail
binary@linuxac.org
>>> website="http://www.linuxac.org/"
>>> param="?p=123"
>>> url = "".join([website,param])
>>> url
'http://www.linuxac.org/?p=123'
Ali Al-Shemery, @binaryz0ne 13
Python Lists
String formatting
>>> pList = [21,22,25,80]
>>> for member in pList:
... print "This is port number %d" % member
...
This is port number 21
This is port number 22
This is port number 25
This is port number 80 Ali Al-Shemery, @binaryz0ne 18
Python Tips and Tricks Cont.
>>> userEmail
'binary@linuxac.org'
>>> type(userEmail)
<type 'str'>
>>> f.close()
Ali Al-Shemery, @binaryz0ne 23
Creating Functions
def checkPortNumber(port):
if port > 65535 or port < 0:
return False
else:
return True
if len(sys.argv) > 1:
print You passed", len(sys.argv)-1, "arguments. They are:"
for arg in sys.argv[1:]:
print arg
else:
print No arguments passed!
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("www.linuxac.org", 80))
s.send('GET / HTTP/1.1\r\nHost: www.linuxac.org\r\n\r\n')
data = s.recv(2048)
s.close()
print data
ROT13
#!/usr/bin/python
code = raw_input("Enter the data you wish to be encoded to
Base64")
answer=code.encode(rot13','strict')
print answer
Ali Al-Shemery, @binaryz0ne Cited [2]
45
Exploit Development
#!/usr/bin/python
import socket
host = target
port = <port#>
cmd = initial command
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
buffer = buffer to send
shellcode = shellcode
Payload = cmd + buffer + shellcode
print "\n Any status message \n
s.connect((host,port))
data = s.recv(1024)
s.send(payload +\n)
s.close
Ali Al-Shemery, @binaryz0ne 46
Packet Crafting with Scapy
Scapy Overview
>>> sr1(IP(dst="192.168.122.101")
/TCP(dport=90,flags="S"))