#!/usr/bin/env python
#-*- coding:utf-8 -*-

# Internal IP Address Leakage Scanner
# Copyright (C) 2011  Sertan Kolat (@warex) - sertan@olympos.org

# version 0.1
# last update: Mar 18, 2011

import sys, getopt, socket, re
from string import strip

wordlist = ['', 'images', 'img', 'aspnet_client', '_private', 'js']


def usage():
	print "Scans web servers for Internal IP Address Disclosure Vulnerability"
	print "\nUsage: ./scanner-http-internal-ip-disclosure.py [options] target"
	print "\nOptions:"
	print "-h, --help	Show usage and exit"
	print "-p PORT		Server port (default 80)"
	print "-s			Connect using SSL (no cert verification)"
	print "-w WORDLIST	Wordlist file containing directories to enumerate"
	print "				(default internal wordlist)"
	print "\nExamples:"
	print "./scanner-http-internal-ip-disclosure.py www.sertankolat.com"
	print "./scanner-http-internal-ip-disclosure.py -w myDirList www.sertankolat.com"
	print "./scanner-http-internal-ip-disclosure.py -s -p 443 www.sertankolat.com"


def loadWordlist(f):
	try:
		w = ['']
		fileWL=open(f, 'r')
		for line in fileWL.readlines():
			line = strip(line)
			if len(line)>0:
				if line[0] != '#':
					w.append(line)
		return w
	except IOError, msg:
		print msg
		sys.exit(1)
	else:
		fileWL.close()


def search(text):
	# searches for internal IP addresses in a given text
	
	# My regex for matching RFC1918 IP addresses
	# I hate false-positives ;)
	# (?:10\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|192\.168|172\.0?(?:1[6-9]|2[0-9]|3[01]))(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){2}
	
	if ('10.' in text or '172.' in text or '192.168.' in text):
		_re_private_IP = re.compile(r"(?:10\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|192\.168|172\.0?(?:1[6-9]|2[0-9]|3[01]))(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){2}")
		match = _re_private_IP.search(text)
		if match:
			return True, match.group()
	else:
		return False, None


def request(target, port, ssl, directory):
	data = "GET /%s HTTP/1.0\r\n\n" % directory
	
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	try:
		s.settimeout(6)
		s.connect((target, int(port)))
		if ssl:
			ssl_sock = socket.ssl(s)
			ssl_sock.write(data)
			response = ssl_sock.read(1024)
		else:
			s.send(data)
			response = s.recv(1024)
	except socket.error, msg:
		print 'Error connecting to %s on port %s' % (target, port)
		print msg
		sys.exit(1)
	finally:
		s.close()

	return response


def test(target, port, ssl, wordlist):
	for word in wordlist:
		response = request(target, port, ssl, word)
		result, IP = search(response)
		if result:
			if target != IP:
				print 'Web server internal IP address disclosure'
				print 'Target: %s:%s, Internal IP: %s, Directory: /%s)' % \
												(target, port, IP, word)
				sys.exit(0)


if __name__ == '__main__':
	if len(sys.argv)<2:
		usage()
		sys.exit(2)
	
	try:
		opts, args = getopt.getopt(sys.argv[1:], "hsp:w:", ["help"])
	except getopt.GetoptError:
		usage()
		sys.exit(2)
		
	if len(args)>0:
		target = args[0]
	else:
		print 'No targets specified! (-h for help)'
		sys.exit(2)
	
	wordlistfile = ''
	port = 80
	ssl = False
	
	for o, a in opts:
		if o in ("-h", "--help"):
			usage()
			sys.exit(2)
		if o == "-w":
			wordlist = loadWordlist(a)
		if o == "-p":
			port = a
		if o == "-s":
			ssl = True
		
	test(target, port, ssl, wordlist)

