Instantly share code, notes, and snippets.
Last active
July 29, 2020 23:09
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save jjam3774/e639a8a22be98563e511 to your computer and use it in GitHub Desktop.
Update Version Jason Orana and Jeffrey James script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
''' | |
A utility that will decode, encode F5 Cookie info | |
Perform dns lookups | |
Reverse dns lookups | |
Pull port,instance and hostname info | |
Refer to: | |
http://support.f5.com/kb/en-us/solutions/public/6000/900/sol6917.html | |
''' | |
import urllib2 | |
import os | |
import struct | |
import socket | |
import re | |
URL = None | |
class F5Util(): | |
def get_cookie(self, debug = ''): | |
'Pull Cookie info from link' | |
if debug == '': | |
debug = raw_input('which debug level 0 or 2: ').strip() | |
if debug.strip() == '0': | |
url = "http://wn.getitgirl.com" | |
fetch_url = urllib2.build_opener(urllib2.HTTPHandler(debuglevel=0)) | |
################################### | |
# | |
# DEFINE THE USER AGENT | |
# | |
################################### | |
fetch_url.addheaders = [('User-agent', 'Mozilla/5.0')] | |
results = fetch_url.open(urllib2.Request(url)) | |
cookies = results.info()['Set-Cookie'].split() | |
return cookies | |
else: | |
url = "http://www.getitgirl.com" | |
fetch_url = urllib2.build_opener(urllib2.HTTPHandler(debuglevel=2)) | |
################################### | |
# | |
# DEFINE THE USER AGENT | |
# | |
################################### | |
fetch_url.addheaders = [('User-agent', 'Mozilla/5.0')] | |
results = fetch_url.open(urllib2.Request(url)) | |
cookies = results.info()['Set-Cookie'].split() | |
for i in cookies: | |
print i | |
return cookies | |
def encode_ip(self): | |
''' | |
** Remove later | |
Test function for helping understand F5/W2A IPv4 address encoding | |
algorithm | |
''' | |
# Step 1 - Convert each octet value to equivalent 1-byte hex value | |
print "==== STEP 1 ====" | |
ip_address = raw_input("Input IP to encode: ") #'10.1.1.100' | |
print "IP Address: [ %s ] " % (ip_address) | |
split_ip = ip_address.split('.') | |
print "Split ip: %s" % (split_ip) | |
# Step 2 - Reverse the order of the hex bytes and concatenate | |
# to make one 4-byte hex value | |
print "=== STEP 2 ===" | |
hexed_ip = [] | |
for ip in split_ip[::-1]: | |
hexed_ip.append(hex(int(ip))) | |
print "Converted and reversed ip 2 hex: %s" % (hexed_ip) | |
first_hex_num = hexed_ip.pop(0) | |
hexed_ip = ''.join(hexed_ip).replace('x','') | |
concat_hex_num = "%s%s" % (first_hex_num, hexed_ip) | |
print "Concatenated hex: [ %s ]" % (concat_hex_num) | |
# Step 3 - Convert the resulting 4-byte hexadecimal value to its | |
# decimal equivalent | |
print "=== STEP 3 ===" | |
decimal_num = int(concat_hex_num, 0) | |
print "Converted hex to decimal: [ %s ]" % (decimal_num) | |
def encode_port(self, port = ''): | |
if port == '': | |
port = raw_input("Input Port number: ") | |
#Encode the port | |
enc_port = ((int(port) << 8) | (int(port) >> 8)) & 0xFFFF | |
hex_enc_port = hex(enc_port) | |
hex_port = hex(int(port.strip())) | |
print "The Hex value of %s: " % hex_port | |
print "Encoded Port is: %s" % enc_port | |
print "The Hex value is: %s" % hex_enc_port | |
def decode_ip(self, encoded_string = ''): | |
if encoded_string == '': | |
encoded_string = raw_input("Input String to Decode: ") | |
######################################################### | |
#Split The String Up to be Decoded | |
######################################################### | |
(self.host, self.port, self.end) = encoded_string.split('.') | |
######################################################### | |
#Run host through struct as an unsigned short integer | |
######################################################### | |
(self.a, self.b, self.c, self.d) = [ord(self.i) for self.i in struct.pack("<I", int(self.host))] | |
print "[*] Decoded IP: %s.%s.%s.%s.\n" % (self.a,self.b,self.c,self.d) | |
self.decode_port(encoded_string) | |
return "%s.%s.%s.%s.\n" % (self.a,self.b,self.c,self.d) | |
def decode_port(self,encoded_string): | |
######################################################### | |
#Split The String Up to be Decoded | |
######################################################### | |
(host, port, end) = encoded_string.split('.') | |
######################################################### | |
#Run the port through struct as an unsigned short integer | |
######################################################### | |
(v) = [ord(j) for j in struct.pack("<H", int(port))] | |
######################################################### | |
#Cater the output to satisfy the unsigned short integer output | |
######################################################### | |
p = "0x%02X%02X" % (v[0],v[1]) | |
print "[*] Decoded port: %s\n" % (int(p,16)) | |
def decode_cookie(self, url = ''): | |
import urllib2 | |
import re | |
if url == '': | |
url = raw_input("Please input link: ") | |
#url = "http://www.neimanmarcus.com" | |
fetch_url = urllib2.build_opener(urllib2.HTTPHandler(debuglevel=0)) | |
################################### | |
# | |
# DEFINE THE USER AGENT | |
# | |
################################### | |
fetch_url.addheaders = [('User-agent', 'Mozilla/5.0')] | |
results = fetch_url.open(urllib2.Request(url)) | |
##################################### | |
# | |
# GET THE SERVER NAME AND PORT | |
# | |
##################################### | |
for i in results.readlines(): | |
if re.search("nmoapp", i): | |
print i.strip() | |
if re.search("ServerPort", i): | |
print i.strip() | |
print("---------") | |
def display_cookie(self): | |
''' | |
Displays cookie | |
''' | |
pass | |
def default(self): | |
print('Invalid Choice..') | |
def get_hostname(self, add = ''): | |
if add == '': | |
add = raw_input("Please input IP Address: ") | |
print('-'*10) | |
print socket.gethostbyaddr(add.strip())[0] | |
print('-'*10) | |
def get_ip(self, host = ''): | |
#host = raw_input("Input Hostname") | |
if host == '': | |
host = raw_input("Please input hostname: ").strip() | |
print('-'*10) | |
print socket.gethostbyname(host.strip()) | |
print('-'*10) | |
################################## | |
# | |
# | |
################################## | |
a = F5Util() | |
if __name__ == "__main__": | |
choice = raw_input(""" | |
1. GetCookie Info | |
2. Encode IP | |
3. Decode IP | |
4. Host and Port Info | |
5. Get Hostname | |
6. Get IP of Hostname | |
q. Quit | |
""") | |
while choice != 'q': | |
menu = { | |
'1' : a.get_cookie, | |
'2' : a.encode_ip, | |
'3' : a.decode_ip, | |
'4' : a.decode_cookie, | |
'5' : a.get_hostname, | |
'6' : a.get_ip, | |
}.get(choice, a.default)() | |
choice = raw_input(""" | |
1. GetCookie Info | |
2. Encode IP | |
3. Decode IP | |
4. Host and Port Info | |
5. Get Hostname | |
6. Get IP of Hostname | |
q. Quit | |
""") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment