-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfb_session.py
148 lines (116 loc) · 3.24 KB
/
fb_session.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python2.7
import requests
from bs4 import BeautifulSoup
import sys
import SocketServer
import getpass
DEFAULT_PORT = 6666
BASE_URL = "http://m.facebook.com/"
LOGIN_INFO_PATH = "testinfo"
global session
def main():
# Create FB Session and handler
global session
session = getSession()
if session == False:
print "Session aborted.", '\n'
return
# Get port
port = DEFAULT_PORT
if len(sys.argv) > 1: port = int(sys.argv[1])
# Start socket server on given port
try:
print "Starting socket server on port", port
print "To start poking people, open a new terminal instance, and"
print "run the autopoke.py script"
server = SocketServer.TCPServer(('localhost', port), FB_Session_Handler)
server.serve_forever()
except:
print "An error has occured."
print "Server has been shut down."
'''
Class for handling FB Session requests.
'''
class FB_Session_Handler(SocketServer.BaseRequestHandler):
def handle(self):
global session
data = self.request.recv(1024).strip()
print
print "Received request from", self.client_address[0]
# Attempt to load the data as a dictionary and load request type
req_type = ""
payload = {}
try:
payload = eval(data)
req_type = payload['type']
except:
print "Improperly formatted payload; could not parse."
return
# Handle a GET request
if req_type == 'GET':
try:
url = payload['url']
req = session.get(url)
response = req.text.encode('utf-8')
print "Handled GET request."
self.request.sendall(response)
return
except:
print "Failed to handle GET request."
return
'''
Gets the user's email and password.
Uses this information to create and return an FB session.
'''
def getSession(debug = False):
email = ""
password = ""
# Get username and password depending on whether in debug mode or not
print '\n', "Please provide your Facebook credentials."
email = raw_input("Email Address: ")
password = getpass.getpass("FB Password: ")
print
# Create FB Session
session = login(email, password)
if session == -1:
print "Connection failed."
return False
elif session == -2:
print "Authentication failed."
return False
elif session == -3:
print "Security check was given."
return False
return session
'''
Logs into Facebook with the given email and password.
Returns a Requests session for further requests.
Returns error code -1 for failed connection and -2 for bad login credentials.
Returns -3 if a security check was given.
'''
def login(email, password):
try:
loginURL = BASE_URL + 'login.php'
# Sent GET Request to get form data
req = requests.get(loginURL)
soup = BeautifulSoup(req.text)
# Add form data to data dictionary
data = {}
dataInputs = [tag for tag in soup.form.children if tag.name == 'input']
for tag in dataInputs:
name = tag['name']
data[name] = soup('input', {'name' : name})[0]['value']
# Add email and password
data['email'] = email
data['pass'] = password
data['login'] = 'Log In'
# Create session
session = requests.Session()
req = session.post(loginURL, data)
soup = BeautifulSoup(req.text)
# Return things
if "Welcome" in soup.title.text: return -2
elif "Security" in soup.title.text: return -3
return session
except: return -1
if __name__ == "__main__": main()