-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
273 lines (232 loc) · 7.48 KB
/
content.js
File metadata and controls
273 lines (232 loc) · 7.48 KB
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
264
265
266
267
268
269
270
271
272
273
// Sentinel Content Script
// Executes in the context of web pages
// Initialize when the content script is injected
initialize();
// Main initialization function
function initialize() {
// Avoid running in frames or iframes
if (window.self !== window.top) {
return;
}
console.log('Sentinel content script initialized');
// Perform page analysis
setTimeout(() => {
analyzePageContent();
}, 1000);
// Add listeners for dynamic content changes
observeDomChanges();
}
// Analyze the current page content for security issues
function analyzePageContent() {
try {
// Check for common security risks in the page
const securityRisks = [];
// 1. Check for mixed content
if (window.location.protocol === 'https:') {
const mixedContent = checkMixedContent();
if (mixedContent.count > 0) {
securityRisks.push({
type: 'mixed_content',
severity: 'medium',
details: `Found ${mixedContent.count} mixed content resources (HTTP resources on an HTTPS page)`
});
}
}
// 2. Check for suspicious forms
const suspiciousForms = checkSuspiciousForms();
if (suspiciousForms.length > 0) {
securityRisks.push({
type: 'suspicious_forms',
severity: 'high',
details: `Found ${suspiciousForms.length} suspicious forms that may collect sensitive information`
});
}
// 3. Check for dangerous links
const dangerousLinks = checkDangerousLinks();
if (dangerousLinks.count > 0) {
securityRisks.push({
type: 'dangerous_links',
severity: 'medium',
details: `Found ${dangerousLinks.count} potentially dangerous outbound links`
});
}
// Send results back to the extension
if (securityRisks.length > 0) {
chrome.runtime.sendMessage({
action: 'contentAnalysisResults',
url: window.location.href,
risks: securityRisks
});
}
} catch (error) {
console.error('Error analyzing page content:', error);
}
}
// Check for mixed content (HTTP resources on HTTPS pages)
function checkMixedContent() {
let count = 0;
const mixedContentElements = [];
// Check images
document.querySelectorAll('img[src^="http:"]').forEach(el => {
count++;
mixedContentElements.push({
type: 'image',
url: el.src
});
});
// Check scripts
document.querySelectorAll('script[src^="http:"]').forEach(el => {
count++;
mixedContentElements.push({
type: 'script',
url: el.src
});
});
// Check stylesheets
document.querySelectorAll('link[rel="stylesheet"][href^="http:"]').forEach(el => {
count++;
mixedContentElements.push({
type: 'stylesheet',
url: el.href
});
});
// Check iframes
document.querySelectorAll('iframe[src^="http:"]').forEach(el => {
count++;
mixedContentElements.push({
type: 'iframe',
url: el.src
});
});
return {
count,
elements: mixedContentElements
};
}
// Check for suspicious forms (login forms, payment forms, etc.)
function checkSuspiciousForms() {
const suspiciousForms = [];
// Get all forms
document.querySelectorAll('form').forEach(form => {
const inputs = form.querySelectorAll('input');
// Check for sensitive input types
const hasPasswordField = Array.from(inputs).some(input =>
input.type === 'password'
);
const hasCreditCardField = Array.from(inputs).some(input =>
input.name && input.name.toLowerCase().match(/credit|card|cc|ccnum|cardnumber/)
);
const hasSSNField = Array.from(inputs).some(input =>
input.name && input.name.toLowerCase().match(/ssn|social|security/)
);
// Check form action
const formAction = form.action;
const isSecureSubmission = formAction && formAction.startsWith('https:');
// Determine if form is suspicious
if ((hasPasswordField || hasCreditCardField || hasSSNField) && !isSecureSubmission) {
suspiciousForms.push({
element: form,
sensitiveData: {
password: hasPasswordField,
creditCard: hasCreditCardField,
ssn: hasSSNField
},
action: formAction || 'No action specified'
});
}
});
return suspiciousForms;
}
// Check for dangerous outbound links
function checkDangerousLinks() {
const dangerousLinks = [];
const currentDomain = window.location.hostname;
// Suspicious TLDs
const suspiciousTlds = [
'.xyz', '.info', '.top', '.tk', '.ml', '.ga', '.cf', '.gq',
'.work', '.click', '.loan', '.date', '.racing', '.download'
];
// Suspicious keywords in URLs
const suspiciousKeywords = [
'free', 'prize', 'winner', 'bitcoin', 'crypto', 'wallet',
'login', 'signin', 'account', 'verify', 'password', 'bank'
];
// Check all links
document.querySelectorAll('a[href]').forEach(link => {
try {
const href = link.href;
// Skip non-HTTP links
if (!href.startsWith('http')) return;
const linkUrl = new URL(href);
const linkDomain = linkUrl.hostname;
// Skip internal links
if (linkDomain === currentDomain) return;
// Check for suspicious TLDs
const hasSuspiciousTld = suspiciousTlds.some(tld =>
linkDomain.endsWith(tld)
);
// Check for suspicious keywords in path
const hasSuspiciousKeyword = suspiciousKeywords.some(keyword =>
linkUrl.pathname.toLowerCase().includes(keyword)
);
if (hasSuspiciousTld || hasSuspiciousKeyword) {
dangerousLinks.push({
element: link,
url: href,
suspicious: {
tld: hasSuspiciousTld,
keyword: hasSuspiciousKeyword
}
});
}
} catch (e) {
// Skip invalid URLs
}
});
return {
count: dangerousLinks.length,
links: dangerousLinks
};
}
// Monitor DOM changes to analyze dynamic content
function observeDomChanges() {
// Create a MutationObserver to watch for significant DOM changes
const observer = new MutationObserver(mutations => {
let shouldAnalyze = false;
for (const mutation of mutations) {
// If nodes are being added
if (mutation.addedNodes.length > 0) {
for (const node of mutation.addedNodes) {
// Only re-analyze for element nodes with significant content
if (node.nodeType === Node.ELEMENT_NODE) {
// Check if this is a significant change (form, iframe, script, or many links)
if (node.tagName === 'FORM' ||
node.tagName === 'IFRAME' ||
node.tagName === 'SCRIPT' ||
node.querySelectorAll('a').length > 3) {
shouldAnalyze = true;
break;
}
}
}
}
if (shouldAnalyze) break;
}
if (shouldAnalyze) {
// Debounce the analysis to avoid excessive CPU usage
if (window._sentinelAnalysisTimeout) {
clearTimeout(window._sentinelAnalysisTimeout);
}
window._sentinelAnalysisTimeout = setTimeout(() => {
analyzePageContent();
}, 1000);
}
});
// Start observing
observer.observe(document.body, {
childList: true,
subtree: true,
attributes: false,
characterData: false
});
}