-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathbingAuth.py
263 lines (219 loc) · 9.82 KB
/
bingAuth.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#
# developed by Sergey Markelov (2013)
#
import HTMLParser
import random
import urllib
import urllib2
import bingCommon
import helpers
class AuthenticationError(Exception):
def __init__(self, message):
Exception.__init__(self, message)
class HTMLFormInputsParser(HTMLParser.HTMLParser):
def __init__(self):
HTMLParser.HTMLParser.__init__(self)
self.inputs = {}
def handle_starttag(self, tag, attrs):
if tag == 'input':
name = value = ''
for attr in attrs:
if attr[0] == 'name':
name = attr[1]
elif attr[0] == 'value':
value = attr[1]
if name != '' and value != '':
self.inputs[name] = value.encode("utf-8")
class BingAuth:
def __init__(self, opener):
"""
@param opener is an instance of urllib2.OpenerDirector
"""
if opener is None or not isinstance(opener, urllib2.OpenerDirector):
raise TypeError("opener is not an instance of urllib2.OpenerDirector")
self.opener = opener
def __authenticateFacebook(self, login, password):
"""
Authenticates a user on bing.com with his/her Facebook account.
throws AuthenticationError if authentication can not be passed
throws HTMLParser.HTMLParseError
throws urllib2.HTTPError if the server couldn't fulfill the request
throws urllib2.URLError if failed to reach the server
"""
BING_REQUEST_PERMISSIONS = "http://www.bing.com/fd/auth/signin?action=interactive&provider=facebook&return_url=http%3a%2f%2fwww.bing.com%2f&src=EXPLICIT&perms=read_stream%2cuser_photos%2cfriends_photos&sig="
# print "Requesting bing.com"
# request http://www.bing.com
request = urllib2.Request(url = bingCommon.BING_URL, headers = bingCommon.HEADERS)
with self.opener.open(request) as response:
page = helpers.getResponseBody(response)
# get connection URL for provider Facebook
s = page.index('"Facebook":"')
s += len('"Facebook":"')
e = page.index('"', s)
url = page[s:e]
s = url.index('sig=')
s += len('sig=')
e = url.find('&', s)
if e == -1:
e = len(url)
url = BING_REQUEST_PERMISSIONS + url[s:e]
# print "Now requesting facebook authentication page"
# request FACEBOOK_CONNECT_ORIGINAL_URL
request = urllib2.Request(url = url, headers = bingCommon.HEADERS)
request.add_header("Referer", bingCommon.BING_URL)
with self.opener.open(request) as response:
referer = response.geturl()
# get Facebook authenctication form action url
page = helpers.getResponseBody(response)
s = page.index('<form id="login_form"')
s = page.index('action="', s)
s += len('action="')
e = page.index('"', s)
url = page[s:e]
# relative url? add url from the previous response
if url[0:1] == "/":
url = referer + url
# find all html elements which need to be sent to the server
s = page.index('>', s)
s += 1
e = page.index('</form>')
parser = HTMLFormInputsParser()
parser.feed(page[s:e].decode("utf-8"))
parser.close()
parser.inputs["email"] = login
parser.inputs["pass"] = password
# print "Now passing facebook authentication"
# pass facebook authentication
postFields = urllib.urlencode(parser.inputs)
request = urllib2.Request(url, postFields, bingCommon.HEADERS)
request.add_header("Referer", referer)
with self.opener.open(request) as response:
url = response.geturl()
# if that's not bingCommon.BING_URL => authentication wasn't pass => write the page to the file and report
if url.find(bingCommon.BING_URL) == -1:
try:
filename = helpers.dumpErrorPage(helpers.getResponseBody(response))
s = "check " + filename + " file for more information"
except IOError:
s = "no further information could be provided - failed to write a file into " + \
helpers.RESULTS_DIR + " subfolder"
raise AuthenticationError("Authentication has not been passed:\n" + s)
def __authenticateLive(self, login, password):
"""
Authenticates a user on bing.com with his/her Live account.
throws AuthenticationError if authentication can not be passed
throws urllib2.HTTPError if the server couldn't fulfill the request
throws urllib2.URLError if failed to reach the server
"""
# print "Requesting bing.com"
# request http://www.bing.com
request = urllib2.Request(url = bingCommon.BING_URL, headers = bingCommon.HEADERS)
with self.opener.open(request) as response:
page = helpers.getResponseBody(response)
# get connection URL for provider Live
s = page.index('"WindowsLiveId":"')
s += len('"WindowsLiveId":"')
e = page.index('"', s)
url = page[s:e]
request = urllib2.Request(url = url, headers = bingCommon.HEADERS)
request.add_header("Referer", bingCommon.BING_URL)
with self.opener.open(request) as response:
referer = response.geturl()
# get Facebook authenctication form action url
page = helpers.getResponseBody(response)
# get PPFT parameter
s = page.index("sFTTag")
s = page.index('value="', s)
s += len('value="')
e = page.index('"', s)
PPFT = page[s:e]
# get PPSX parameter
s = page.index(",g:'")
s += len(",g:'")
e = page.index("'", s)
PPSX = page[s:e]
# get sso parameter
s = page.index(",W:")
s += len(",W:")
e = page.index(",", s)
sso = int(page[s:e])
# generate ClientLoginTime
clt = 20000 + int(random.uniform(0, 1000))
# generate RenderCompleteTime
renderTime = 130 + int(random.uniform(0, 100))
# generate ResourcesCompleteTime
resourcesTime = renderTime + int(random.uniform(2, 5))
# generate ResourcesCompleteTime
PLT = 870 + int(random.uniform(0, 250))
# get url to post data to
s = page.index(",urlPost:'")
s += len(",urlPost:'")
e = page.index("'", s)
url = page[s:e]
postFields = urllib.urlencode({
"login" : login,
"passwd" : password,
"SI" : "Sign in",
"type" : "11",
"PPFT" : PPFT,
"PPSX" : PPSX,
"idsbho" : "1",
"LoginOptions" : "3",
"sso" : str(sso),
"NewUser" : "1",
"i1" : "0", # ClientUserSaved
"i2" : "1", # ClientMode
"i3" : str(clt), # ClientLoginTime
"i4" : "0", # ClientExplore
"i7" : "0", # ClientOTPRequest
"i12" : "1", # LoginUsedSSL
"i13" : "0", # ClientUsedKMSI
"i14" : str(renderTime), # RenderCompleteTime
"i15" : str(resourcesTime), # RenderCompleteTime
"i16" : str(PLT), # PLT
"i17" : "0", # SRSFailed
"i18" : "__Login_Strings|1,__Login_Core|1," # SRSSuccess
})
# get Passport page
request = urllib2.Request(url, postFields, bingCommon.HEADERS)
with self.opener.open(request) as response:
page = helpers.getResponseBody(response)
s = page.index("<form ")
e = page.index("</form>", s)
e += len("</form>")
parser = HTMLFormInputsParser()
parser.feed(page[s:e].decode("utf-8"))
parser.close()
postFields = urllib.urlencode(parser.inputs)
# finish passing authentication
url = "http://www.bing.com/Passport.aspx?requrl=http%3a%2f%2fwww.bing.com%2f&wa=wsignin1.0"
request = urllib2.Request(url, postFields, bingCommon.HEADERS)
request.add_header("Origin", "https://login.live.com")
with self.opener.open(request) as response:
page = helpers.getResponseBody(response)
url = bingCommon.BING_URL
request = urllib2.Request(url, postFields, bingCommon.HEADERS)
request.add_header("Referer", "http://www.bing.com/Passport.aspx?requrl=http%3a%2f%2fwww.bing.com%2f&wa=wsignin1.0")
with self.opener.open(request) as response:
url = response.geturl()
# if that's not bingCommon.BING_URL => authentication wasn't pass => write the page to the file and report
if url.find(bingCommon.BING_URL) == -1:
try:
filename = helpers.dumpErrorPage(helpers.getResponseBody(response))
s = "check " + filename + " file for more information"
except IOError:
s = "no further information could be provided - failed to write a file into " + \
helpers.RESULTS_DIR + " subfolder"
raise AuthenticationError("Authentication has not been passed:\n" + s)
def authenticate(self, authType, login, password):
"""
throws ValueError if login or password is None
throws AuthenticationError
"""
if login is None: raise ValueError("login is None")
if password is None: raise ValueError("password is None")
try:
authMethod = getattr(self, "_" + self.__class__.__name__ + "__authenticate" + authType)
authMethod(login, password)
except AttributeError:
raise AuthenticationError("Configuration Error: authentication type " + authType + " is not supported")