sm4rtn0bit4

setcookies / getcookies using Python CGI

Jul 30th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.95 KB | None | 0 0
  1. # Defining Cookie Generator
  2. # HTTP Header begins with cookie
  3. def setcookies(user):
  4.     username=user
  5.     # Generating Cookie
  6.     #
  7.     import os
  8.     from cgi_session import CGISession
  9.     #
  10.     #   Genrating Random Session Key
  11.     #
  12.     session=CGISession()
  13.     uuid=str(session.getID())         # This will generate random string used as key
  14.     os.chmod('/tmp/pycgiession_%s'%(uuid),644)
  15.     session.setParam('secure','True')
  16.     session.setParam('username','%s'%(username))
  17.     session.setParam('name','HTTP_COOKIE')
  18.     session.setParam('uuid','%s'%(uuid))
  19.     print("Set-Cookie: session_cookie \r domain = 'kali.example.com ' name = 'HTTP_COOKIE' username = %s uuid = %s ; secure = 'True'\r\n\r\n"%(username,uuid))
  20.  
  21. ########################################################################################################
  22. # Getting cookies
  23. def getcookies():
  24.     # Import
  25.     import os,cgi,cgitb
  26.     from cgi_session import CGISession  # This class file uploaded here:- https://pastebin.com/L4JwHmCg
  27.     from os import environ
  28.     #
  29.     cookiesDict = {}
  30.     if 'HTTP_COOKIE' in os.environ:
  31.         result='True'
  32.         cookies = '%s'%os.environ['HTTP_COOKIE']
  33.         c = cookies.split('; ')
  34.         c_temp=[]
  35. usage        for i in range(len(c)):
  36.             c_temp.append(c[i].split(' '))
  37.         c1=tuple(c_temp)
  38.         st=c1[0]
  39.         cookiesDict[st[4]] = st[6]  #cookie name    # Squencing according to the above function setcookies(user)
  40.         cookiesDict[st[7]] = st[9]  #username       # This will raise error till you exec it on APACHE Server
  41.         cookiesDict[st[10]] = st[12]    #uuid
  42.     else:
  43.         result='False'
  44.         return result
  45.     return cookiesDict
  46. '''
  47. Usage:
  48. >>>setcookies(username)
  49. >>>cookies=getcookies()
  50. >>>cookies['username']
  51. test
  52. >>>cookies['uuid']
  53. 0ea3d78b80d32056a80367c74248a71a
  54. >>>cookies['name']              # It's just an extra entry if u wanna use it ...
  55. HTTP_COOKIE            
  56. '''
  57. # On Server side cookie-file can be found in /tmp
Add Comment
Please, Sign In to add comment