-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
268 lines (240 loc) · 10.1 KB
/
script.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
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
document.addEventListener('DOMContentLoaded', () => {
const inputText = document.querySelector('.js-input-text');
const analyzeBtn = document.querySelector('.js-analyze-btn');
const resultsDiv = document.querySelector('.js-results');
analyzeBtn.addEventListener('click', analyzeInput);
inputText.addEventListener('keyup', (event) => {
if (event.key === 'Enter') {
analyzeInput();
}
});
function analyzeInput() {
const text = inputText.value;
const suspiciousChars = analyzeText(text);
const visuallySimilar = checkVisuallySimilarChars(text);
displayResults(suspiciousChars, visuallySimilar);
}
function analyzeText(text) {
const suspiciousChars = [];
// Regular expression for various categories of suspicious characters
const regex = /[^\u0000-\u007F]|\u200B-\u200D|\u2060|\uFEFF|\u202A-\u202E/g;
// Homoglyphs
const homoglyphs = {
'а': 'a', 'е': 'e', 'о': 'o', 'р': 'p', 'с': 'c', 'у': 'y', 'х': 'x',
'ᴀ': 'a', 'ʙ': 'b', 'ᴄ': 'c', 'ᴅ': 'd', 'ᴇ': 'e', 'ғ': 'f', 'ɢ': 'g',
'ʜ': 'h', 'ɪ': 'i', 'ᴊ': 'j', 'ᴋ': 'k', 'ʟ': 'l', 'ᴍ': 'm', 'ɴ': 'n',
'ᴏ': 'o', 'ᴘ': 'p', 'ǫ': 'q', 'ʀ': 'r', 'ѕ': 's', 'ᴛ': 't', 'ᴜ': 'u',
'ᴠ': 'v', 'ᴡ': 'w', 'х': 'x', 'ʏ': 'y', 'ᴢ': 'z'
};
let match;
while ((match = regex.exec(text)) !== null) {
let charType = "Non-ASCII";
if (/[\u200B-\u200D\u2060\uFEFF]/.test(match[0])) {
charType = "Zero-width";
} else if (/[\u202A-\u202E]/.test(match[0])) {
charType = "Directional formatting";
} else if (Object.keys(homoglyphs).includes(match[0])) {
charType = `Homoglyph (looks like '${homoglyphs[match[0]]}')`;
}
suspiciousChars.push({
char: match[0],
index: match.index,
unicode: match[0].charCodeAt(0).toString(16).padStart(4, '0'),
type: charType
});
}
return suspiciousChars;
}
function checkVisuallySimilarChars(text) {
const similarities = [
{
characterFound: ['I'],
isIn: "Uppercase",
alterForm: { "lowercase": "i" },
confusedWith: 'lowercase "L"'
},
{
characterFound: ['l'],
isIn: "lowercase",
alterForm: { "uppercase": "L" },
confusedWith: 'uppercase "i"'
},
{
characterFound: ['G'],
isIn: "Uppercase",
alterForm: { "lowercase": "g" },
confusedWith: 'the number "6"'
},
{
characterFound: ['g'],
isIn: "lowercase",
alterForm: { "uppercase": "G" },
confusedWith: 'the number "6"'
},
{
characterFound: ['0'],
isIn: "Number",
confusedWith: 'the letter "O" (uppercase) or "o" (lowercase)'
},
{
characterFound: ['O'],
isIn: "Uppercase",
alterForm: { "lowercase": "o" },
confusedWith: 'the number zero "0"'
},
{
characterFound: ['o'],
isIn: "lowercase",
alterForm: { "uppercase": "O" },
confusedWith: 'the number zero "0"'
},
{
characterFound: ['S'],
isIn: "Uppercase",
alterForm: { "lowercase": "s" },
confusedWith: 'the number five "5"'
},
{
characterFound: ['s'],
isIn: "lowercase",
alterForm: { "uppercase": "S" },
confusedWith: 'the number five "5"'
},
{
characterFound: ['Z'],
isIn: "Uppercase",
alterForm: { "lowercase": "z" },
confusedWith: 'the number two "2"'
},
{
characterFound: ['z'],
isIn: "lowercase",
alterForm: { "uppercase": "Z" },
confusedWith: 'the number two "2"'
},
{
characterFound: ['B'],
isIn: "Uppercase",
alterForm: { "lowercase": "b" },
confusedWith: 'the number eight "8"'
},
{
characterFound: ['b'],
isIn: "lowercase",
alterForm: { "uppercase": "B" },
confusedWith: 'the number eight "8"'
}
];
const results = [];
similarities.forEach(sim => {
sim.characterFound.forEach(char => {
const regex = new RegExp(char, 'g');
let match;
while ((match = regex.exec(text)) !== null) {
results.push({
char: match[0],
index: match.index,
...sim
});
}
});
});
return results;
}
function displayResults(suspiciousChars, visuallySimilar) {
resultsDiv.innerHTML = '';
if (suspiciousChars.length === 0 && visuallySimilar.length === 0) {
resultsDiv.innerHTML = '<p>No suspicious characters or visually similar substitutions found.</p>';
return;
}
// Display results for suspicious characters
if (suspiciousChars.length > 0) {
const suspiciousDiv = document.createElement('div');
suspiciousDiv.className = 'result-section__suspicious';
// Show warning message
const warningDiv = document.createElement('div');
warningDiv.className = 'result-section__warning';
warningDiv.innerHTML = `
<strong>Warning:</strong> Suspicious characters detected.
This text may be attempting to deceive you. Exercise caution with any links or email addresses containing these characters.
`;
suspiciousDiv.appendChild(warningDiv);
suspiciousDiv.innerHTML += '<h3>Suspicious Characters:</h3>';
const suspiciousList = document.createElement('ul');
suspiciousList.className = 'result-section__list';
suspiciousChars.forEach(result => {
const listItem = document.createElement('li');
listItem.className = 'result-section__item';
listItem.innerHTML = `
<strong>Character:</strong> ${result.char} <br>
<strong>Position:</strong> ${result.index} <br>
<strong>Unicode:</strong> U+${result.unicode} <br>
<strong>Type:</strong> ${result.type} <br>
<div class="highlighted-text">${highlightChar(inputText.value, result.index, result.char)}</div>
`;
suspiciousList.appendChild(listItem);
});
suspiciousDiv.appendChild(suspiciousList);
resultsDiv.appendChild(suspiciousDiv);
}
// Display results for visually similar characters
if (visuallySimilar.length > 0) {
const similarDiv = document.createElement('div');
similarDiv.className = 'result-section__similar';
similarDiv.innerHTML = `
<h3>Visually Similar Character Substitutions:</h3>
<p>The following characters might be confused with others:</p>
`;
const similarList = document.createElement('ul');
similarList.className = 'result-section__list';
visuallySimilar.forEach(result => {
const listItem = document.createElement('li');
listItem.className = 'result-section__item';
listItem.innerHTML = `
Character '${result.char}' at position ${result.index}:
<ul>
<li>This character is ${result.isIn === "Number" ? "a number" : `in ${result.isIn}`}</li>
${result.alterForm ? `<li>In ${Object.keys(result.alterForm)[0]} this character looks like this: ${Object.values(result.alterForm)[0]}</li>` : ''}
<li>It is possible to confuse this character with ${result.confusedWith}</li>
</ul>
<div class="highlighted-text">${highlightChar(inputText.value, result.index, result.char)}</div>
`;
similarList.appendChild(listItem);
});
similarDiv.appendChild(similarList);
// Add the cautionary note after the character analysis
const cautionNote = document.createElement('p');
cautionNote.className = 'result-section__caution-note';
cautionNote.innerHTML = `
<strong>Note:</strong> The presence of these characters doesn't necessarily indicate malicious intent,
but caution is advised since it's possible to misread some characters. If this is a URL or email address,
type it manually rather than clicking a link.
`;
similarDiv.appendChild(cautionNote);
resultsDiv.appendChild(similarDiv);
}
}
function highlightChar(text, index, char) {
const before = text.slice(0, index);
const after = text.slice(index + 1);
let highlightedChar;
if (char.trim() === '') {
// For zero-width characters, insert a visible marker
highlightedChar = '<span class="zero-width-marker">•</span>';
} else {
highlightedChar = `<span class="highlighted-char">${char}</span>`;
}
return `${escapeHTML(before)}${highlightedChar}${escapeHTML(after)}`;
}
function escapeHTML(str) {
return str.replace(/[&<>'"]/g,
tag => ({
'&': '&',
'<': '<',
'>': '>',
"'": ''',
'"': '"'
}[tag] || tag)
);
}
});