-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathhelper.py
260 lines (235 loc) · 11 KB
/
helper.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
import requests
import urllib3
import json
import base64
import time
import string
import random
from bs4 import BeautifulSoup
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
class CheggHelper:
def __init__(self):
self.log_url = 'https://proxy.chegg.com/oidc/token'
self.mfa_url = 'https://proxy.chegg.com/oidc/mfa'
self.verify_mfa = 'https://proxy.chegg.com/oidc/token'
self.getAns = 'https://proxy.chegg.com/mobile-study-bff/graphql/'
self.tbs = 'https://proxy.chegg.com/v1/tbs/_/solution'
self.heads = {
'user-agent': 'CheggApp/3.39.0 (com.chegg.mobile.consumer; build:3.39.0.0; iOS 14.5.0) Alamofire/5.2.2',
'authorization': 'Basic MFQxOE5HYmFsUURGYzBnWkh6b3ZwZVJkN0E1Y3BMQ3g6dnRnamFZa3Ric2p4OUFPUg==',
'Content-Type': 'application/x-www-form-urlencoded',
'Connection': 'keep-alive'
}
self.web_heads = {
'authority': 'www.chegg.com',
'sec-ch-ua': '"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'sec-fetch-site': 'none',
'sec-fetch-mode': 'navigate',
'sec-fetch-user': '?1',
'sec-fetch-dest': 'document',
'accept-language': 'en-US,en;q=0.9',
}
def try2FA(self, mfaToken, factors):
for indd, i in enumerate(factors):
print('[*] ' + str(indd + 1) + '. ' + i['channelType'] + ' ' + i['name'])
print('[*] Enter Channel id to continue')
idd = int(input())
f_id = factors[idd - 1]['id']
f_channel = factors[idd - 1]['channelType']
data = {
'factor_id': f_id,
'mfa_token': mfaToken
}
res = requests.post(url=self.mfa_url, data=data, headers=self.heads, verify=False)
js = res.json()
if js['httpCode'] == 200:
mfaEventId = js['mfaEventId']
print('[*] OTP Sent')
worked = False
while worked != True:
print('[*] Enter OTP: ', end='')
otp_code = input()
data2 = {
'mfa_event_id': mfaEventId,
'source_page': 'ios CheggApp 3.36.0|cs',
'source_product': 'ios|cs',
'mfa_code': otp_code,
'mfa_token': mfaToken,
'grant_type': 'mfa_code'
}
res2 = requests.post(url=self.verify_mfa, data=data2, headers=self.heads, verify=False)
js1 = res2.json()
if 'error' in res2.text:
print(js1['error'])
elif js1['access_token'] != None:
worked = True
access_token = js1['access_token']
refresh_token = js1['refresh_token']
id_token = js1['id_token']
with open('settings.json', 'w') as fp:
data = {
'access_token': access_token,
'refresh_token': refresh_token,
'id_token': id_token
}
fp.write(json.dumps(data))
print('[*] Setup Completed')
else:
print(res2.text)
else:
print(res.text)
def getRefreshToken(self):
with open('settings.json', 'r') as fp:
js = json.loads(fp.read())
data = {
'source_page': 'ios CheggApp 3.36.0|cs',
'source_product': 'ios|cs',
'grant_type': 'refresh_token',
'refresh_token': js['refresh_token']
}
res1 = requests.post(url=self.log_url, data=data, headers=self.heads, verify=False)
js = res1.json()
if 'access_token' in res1.text:
if js['access_token'] != None:
access_token = js['access_token']
refresh_token = js['refresh_token']
id_token = js['id_token']
with open('settings.json', 'w') as fp:
data = {
'access_token': access_token,
'refresh_token': refresh_token,
'id_token': id_token
}
fp.write(json.dumps(data))
elif 'error' in res1.text:
print(js['error'])
else:
print(res1.text)
def chkTokenValid(self, access_token):
ex = access_token.split('.')[1] + '=='
js = json.loads(base64.b64decode(ex))
exp = js['exp']
if exp > int(time.time()):
return True
else:
return False
def getQAns(self, ques_url):
with open('settings.json', 'r') as fp:
js = json.loads(fp.read())
access_token = js['access_token']
if self.chkTokenValid(access_token):
pass
else:
self.getRefreshToken()
return self.getQuesAns(ques_url)
def getQuesAns(self, q_url):
try:
res = requests.get(url=q_url, headers=self.web_heads, verify=False)
if 'questions-and-answers' in q_url:
try:
soup = BeautifulSoup(res.text, 'html.parser')
dd = soup.find('script', attrs={'id': '__NEXT_DATA__'})
js = json.loads(dd.text)
q_id = js['props']['pageProps']['questionData']['uuid']
except:
q_id = res.text.split('questionUuid":"')[1].split('"}')[0]
# print(q_id)
datax = {
'id': 'getQuestionByUuid',
'operationName': 'getQuestionByUuid',
'variables': {
'questionUuid': q_id
}
}
with open('settings.json', 'r') as fp:
jsd = json.loads(fp.read())
access_token = jsd['access_token']
heads = {
'user-agent': 'CheggApp/3.39.0 (com.chegg.mobile.consumer; build:3.39.0.0; iOS 14.5.0) Alamofire/5.2.2',
'authorization': 'Basic MFQxOE5HYmFsUURGYzBnWkh6b3ZwZVJkN0E1Y3BMQ3g6dnRnamFZa3Ric2p4OUFPUg==',
'Content-Type': 'application/json',
'Connection': 'keep-alive',
'access_token': access_token
}
res = requests.post(url=self.getAns, json=datax, headers=heads, verify=False)
js = res.json()
totalAns = len(js['data']['getQuestionByUuid']['answers'])
ans = ''
filename = './answers/answer_' + ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(10)) + '.html'
if len(js['data']['getQuestionByUuid']['answers']) > 0:
if js['data']['getQuestionByUuid']['answers'][0]['accessDetails']['hasAccess']:
for indd, i in enumerate(js['data']['getQuestionByUuid']['answers']):
ans += '<br><h1>Answer - ' + str(indd + 1) + '</h1><br>' + i['body']
with open(filename, 'w', encoding='utf-8') as fp:
fp.write(str(ans))
return True, filename, 'Success'
else:
return False, '', 'You dont have access'
else:
return False, '', 'Not Answered Yet!!'
else:
problemId = res.text.split('"problemId":"')[1].split('",')[0]
isbn13 = res.text.split('"isbn13":"')[1].split('",')[0]
datax = {
'problemId': problemId,
'isbn13': isbn13,
'userAgent': 'Mobile'
}
heads = {
'user-agent': 'CheggApp/3.39.0 (com.chegg.mobile.consumer; build:3.39.0.0; iOS 14.5.0) Alamofire/5.2.2',
'authorization': 'Basic MFQxOE5HYmFsUURGYzBnWkh6b3ZwZVJkN0E1Y3BMQ3g6dnRnamFZa3Ric2p4OUFPUg==',
'Content-Type': 'application/json',
'Connection': 'keep-alive'
}
resp1 = requests.post(url=self.tbs, json=datax, headers=heads, verify=False)
js1 = resp1.json()
if js1['httpCode'] == 200:
if len(js1['result']['solutions']) > 0:
filename = './answers/answer_' + ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(10)) + '.html'
datac = ''
for indd, x in enumerate(js1['result']['solutions'][0]['steps']):
datac += '<br><H1>Step - ' + str(indd + 1) + '<br>' + requests.get(x['link'], verify=False).text
with open(filename, 'w', encoding='utf-8') as fp:
fp.write(str(datac))
return True, filename, 'Success'
else:
return False, '', 'Not answered'
except Exception as e:
return False, '', str(e)
def tryLogin(self, username, password):
data = {
'grant_type': 'password',
'source_page': 'ios CheggApp 3.36.0|cs',
'source_product': 'ios|cs',
'username': username,
'password': password
}
res = requests.post(url=self.log_url, data=data, headers=self.heads, verify=False)
js = res.json()
if res.status_code == 200:
if 'error' in res.text:
print('[*] ' + js['error'])
elif js['access_token'] != None and js['mfaChallengeDetails'] == None:
access_token = js['access_token']
refresh_token = js['refresh_token']
id_token = js['id_token']
with open('settings.json', 'w') as fp:
data = {
'access_token': access_token,
'refresh_token': refresh_token,
'id_token': id_token
}
fp.write(json.dumps(data))
elif js['mfaChallengeDetails'] != None:
mfaToken = js['mfaChallengeDetails']['mfaToken']
userUUID = js['mfaChallengeDetails']['userUuid']
factors = js['mfaChallengeDetails']['factors']
self.try2FA(mfaToken, factors)
else:
print('[*] Unknown Error')
else:
print('[*] ' + js['error'])