Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Sunday, September 11, 2011

Twython: Python API for Twitter

pretty neat, easy to use API for twitter, its twython, I got it installed and working in less that 2 minutes:

Pre-requesites: Python already installed on the box.

$ sudo easy_install twython
$ python
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05)
>>> from twython import Twython
>>> twitter = Twython()
>>> results = twitter.searchTwitter(q="NASA", rpp='10')
>>> for tweet in results['results']:
... print "User: %s \n Tweet: %s" % (tweet['from_user'], tweet['text'])
...
User: CarlaEid
Tweet: Reading: http://t.co/1Rlpmr0 #Arabic #NASA
User: raccoonTweetzz
Tweet: If NASA sends a pregnant woman into space and gives birth...is the baby an alien? (•͡.̮ •͡ )
User: JHarley36
Tweet: @PrettyMoneyPaid I've been working for NASA.
User: chrome_ghost
Tweet: 9.11 as seen by the only american not on earth at the time. http://t.co/SInyMAh
User: sebargue
Tweet: RT @monterocnn: Así se vieron ataques contra Nueva York desde el espacio. Video: http://t.co/x0BB9tm
User: supermorgy
Tweet: @mariefrance16 Nasa work ako Marie. Downtime kami. Kaya nag hahabol productivity. Will watch it with honnie on Saturday!
User: bongiss
Tweet: @Lalalishh nasa new york ka? Haha
User: UNIVERSITAM
Tweet: UNIVERSITAM: LA NASA LANZA LAS SONDAS GRAIL CON DESTINO A LA LUNA http://t.co/ebBGucY
User: taroshaw
Tweet: RT @Mrkat0: NASAの人工衛星落下へ 重さ6トン、月末にも http://t.co/lKXubuz
ジョー、君はどこに落ちたい…?
User: hugonz
Tweet: Y no, la NASA no cuenta. RT @circulobastiat: "Los grandes avances de la civilización jamás han venido deun gobierno central" Milton Friedman
>>>



Tuesday, August 16, 2011

Getting HTTP headers and searching for X pattern in the body content with Python

I've done this small script to get the HTTP headers, get the server response and then look for x text:


#!/usr/bin/python
import sys
import httplib
from urlparse import urlparse

#Initializing some vars
target_address=""
resource = ""
conn = ""
res = ""


def UserInput():
global target_address
global resource
i = 0

while True:
if i >= 2:
print "[-] Don't try to be sneaky if you want to test, provide a valid URL, I'm exiting..."
sys.exit();

url = raw_input("Enter a valid URL to Test: ")
if url and "http://" not in url:
print "[-] you need to follow RFC 1808 when working with URLs, but not worries, I've corrected it for you"
url = "http://" + url
o = urlparse(url)
#Validating that user's input has a resource to GET /something.some
if not o.path or o.path == "/":
print "[-] Nothing to do, you need to provide a valid URL and RESOURCE to test i.e http://www.test.com/resource.htm, I'm exiting..."
sys.exit()
#all good and set so lets assign them
target_address = o.netloc
resource = o.path
break
i +=1


def Connection():
global conn
global res
print "\n[?] Trying to connect to: " + target_address
print "[?] Trying to GET: " + resource
conn = httplib.HTTPConnection(target_address,timeout=5)
conn.request("GET", resource)
res = conn.getresponse()
#Goal 1: Print the response of the server:
print "[*] Server Response: " + str(res.status) + " Details: " +res.reason
#Goal 2: Print the response of the server:
print "\n[*] Server HEADER Response:"
for i, (header, value) in enumerate(res.getheaders()):
print "%s: %s" % (header.capitalize(), value.capitalize())

def CheckXSS():
#Goal 3: check if there is an XSS in the body
xss = "alert---document.cookie---" # <- i had to modify the actual text since it seems blogger doesn't like script tags
if xss in res.read():
print "\n[*] WARNING: XSS detected in HTTP response body!, this guy knows to how to get it done!"
else:
print "\n[*] XSS was not found in the body. "

try:
UserInput()
Connection()
#if there is a valid resource and exists, we check it.
if res.status == 200:
CheckXSS()
else:
print "\n[-] Nothing to do, you need to provide a valid URL and RESOURCE to test i.e http://www.test.com/resource.htm, exiting"
conn.close()
print "[*] Done!"
except Exception as msg:
print ("\n[-] There is a Problem, Check OSI tier 8 and try again\n[-] Error Details: %s" % msg)






As always, feedback is appreciate, cut me some slack though, I dont code everyday just from time to time.

Bash script to put all the info from separate files in one

So, I had this directory tree full of python scripts I've written but I wanted to put them all in one file to make it easy to read/find stuff, so I was trying to think a way to cut myself some slack, so I ended up writing this small piece of code in bash to get it done:


#!/bin/bash
for filex in $(ls *.py);do
echo "working on file: $filex"
echo -e "\n----------$filex-------------------\n" >> allTheCode.txt
cat $filex >> allTheCode.txt
echo -e "\n-----------------------------------\n" >> allTheCode.txt
done


I know this looks ugly, however it works :), if you know a better way just drop me a comment!