Advertisement
Guest User

Untitled

a guest
May 16th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.93 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import MySQLdb
  4. import re, hashlib, os
  5.  
  6. re_cookie = re.compile(r'^a:[34]:\{i:0;(?:i:\d{1,6}|s:[1-8]):"(\d{1,8})";i:1;s:(?:0|40):"([a-fA-F0-9]{40})?";i:2;[id]:\d{1,14};(?:i:3;i:\d;)?\}$i')
  7. re_smf_cookie = re.compile(r'^SMFCookie716=(.+)$')
  8.  
  9. db=MySQLdb.connect(host="localhost", user="maxim", passwd="", db="test")
  10. cur = db.cursor()
  11. def fetch_user_data(uid):
  12.     cur.execute("select passwd, password_salt, is_activated, member_name from smf_members where id_member=%s", (uid,))
  13.     try:
  14.         return cur.fetchone()
  15.     except:
  16.         return None
  17.  
  18. """
  19. get user id and password hash from cookie
  20. fetch user from db
  21. compare cookie password hash against fetched data
  22. ( this code was stolen from smf/Load.php/loadUserSettings() )
  23. """
  24. def check_user(cookie):
  25.     global re_cookie
  26.     m = re_cookie.match(cookie)
  27.     if not m:
  28.         return 0
  29.     cookie_uid, cookie_password = m.group(1,2)
  30.     cookie_uid = int(cookie_uid)
  31.  
  32.     if cookie_uid==0 or len(cookie_password)!=40:
  33.         return False
  34.  
  35.     user = fetch_user_data(cookie_uid)
  36.  
  37.     check = False
  38.     sha = hashlib.sha1()
  39.     sha.update(user[0] + user[1])
  40.     if (sha.hexdigest() == cookie_password) and (user[2]==1 or user[2]==11):
  41.         check = True
  42.  
  43.     # return username
  44.     return check and user[3]
  45.  
  46. def get_smf_cookie():
  47.     global re_smf_cookie
  48.     cookies = os.environ['HTTP_COOKIE'] # ???
  49.     for c in re.split('; ', cookies):
  50.         m = re_smf_cookie.match(c)
  51.         if m:
  52.             return m.group(1)
  53.     return None
  54.  
  55. def get_messages():
  56.     cookie = get_smf_cookie()
  57.     if not cookie:
  58.         # not logged in?
  59.         return {}
  60.     username = check_user(cookie)
  61.     if not username:
  62.         # not logged in/unapproved/blocked/etc
  63.         # return some intro page
  64.         return {}
  65.     # user is valid one
  66.     return {'username' username, 'messages': messages} # <-- your messages here. username can be used as a message prompt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement