Thursday, 8 August 2013

Python Socket - quick client notes

Hi all, these are my personal notes.

So I can print them out and sticky tape them to my wall.

Please note: A fantastic tutorial I reference from is BinaryTides. Found here 

I am breaking this tutorial up into my own notes, to jog my memory. So it is not a tutorial for others to follow, however if it is useful, its useful ! :)


Now "haydn" (aka me), python network programming requires a socket.



test


Now we need to get an IP address of a remote host/url
as such we connect to google

Note to self: Copy pasting looks way better than creating via Gist, saves time too!!
host = 'www.google.com'
try:
    remote_ip = socket.gethostbyname( host )
except socket.gaierror:
    #could not resolve
    print 'Hostname could not be resolved. Exiting'
    sys.exit()
     
print 'Ip address of ' + host + ' is ' + remote_ip





Now we have the IP address we can connect to it on a certain port
simply use s.connect


#Connect to remote server
s.connect((remote_ip , port))
print 'Socket Connected to ' + host + ' on ip ' + remote_ip




But we need to add the port we wish to connect to

host = 'www.google.com'
port = 80


Thats making a socket and connecting to a server.

To Send data we do 

sendall(message) 


message = "GET / HTTP/1.1\r\n\r\n"


try :

    #Set the whole string
    s.sendall(message)
except socket.error:
    #Send failed
    print 'Send failed'
    sys.exit()
print 'Message send successfully'



To receive data:
s.recv(4096) 


#Now receive data

reply = s.recv(4096)


print reply



Closing a socket
s.close()

Saturday, 3 August 2013

SPSE Update - all things ahoy

Hi Everyone,

Sorry for not posting! I really am. It is not because I have not been working on SPSE, I so have!'


I just wanted to throw an update of how I have been travelling.

So in the other posts I worked on Scapy, a multithreaded port scanner.

Well so far I have worked in multi threading a bit more; adding lock and Queue. Which are really great.
Have also created an Nmap script (not finished) that takes an IP range then multi threads each IP into a thread an outputs into an individual file.

Very useful for me, as I had scanned 200 IP addresses and wanted an easy way top find each IP.

Hard than it sounds

Now... A script that sends syntax to the terminal for nmap, multi threads and prints out a seperate file for each. Doesn't hard hey.

Sending to terminal, sending nmap syntax, multi threading were all easy, as it was just an extension of what I had done in SPSE. However, the logic and tiny details of taking the IP range, cuting it up, converting the IP range part to integer, then doing the range for the IP range and turning it back into a string AND pjoining it back to a full IP address was more difficult/time consuming.


What I mean is that IP range 100-200 as in : 192.168.0.100-200 had to have the 100-200 cut from the whole IP. 100 and 200 had to be converted to an interger.

Then it was range from 100 to 200 add onto the original IP. example 192.168.0.111.