-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity.js
More file actions
333 lines (288 loc) · 10.8 KB
/
security.js
File metadata and controls
333 lines (288 loc) · 10.8 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// ========================================
// 클라이언트 사이드 보안 강화 모듈
// ========================================
class SecurityManager {
constructor() {
this.devToolsOpen = false;
this.gameStartTime = null;
this.lastLevelTime = null;
this.susiciousActivity = 0;
this.maxSuspiciousThreshold = 3;
this.initSecurity();
}
initSecurity() {
// this.detectDevTools(); // 개발자 도구 감지 비활성화
this.preventCommonHacks();
this.setupIntegrityChecks();
this.monitorGameplay();
}
// 개발자 도구 탐지
detectDevTools() {
let devtools = { open: false, orientation: null };
const threshold = 160;
const detectDevTools = () => {
if (window.outerWidth - window.innerWidth > threshold ||
window.outerHeight - window.innerHeight > threshold) {
if (!devtools.open) {
devtools.open = true;
this.onDevToolsDetected();
}
} else {
devtools.open = false;
}
};
// 여러 방법으로 개발자 도구 탐지
setInterval(detectDevTools, 500);
// Console 접근 탐지
let consoleOpened = false;
Object.defineProperty(window, 'console', {
get: function() {
if (!consoleOpened) {
consoleOpened = true;
setTimeout(() => {
security.onDevToolsDetected();
}, 100);
}
return console;
}
});
// F12, Ctrl+Shift+I 차단
document.addEventListener('keydown', (e) => {
// F12
if (e.key === 'F12') {
e.preventDefault();
this.onDevToolsDetected();
return false;
}
// Ctrl+Shift+I (개발자 도구)
if (e.ctrlKey && e.shiftKey && e.keyCode === 73) {
e.preventDefault();
this.onDevToolsDetected();
return false;
}
// Ctrl+U (소스 보기)
if (e.ctrlKey && e.keyCode === 85) {
e.preventDefault();
return false;
}
// Ctrl+S (저장)
if (e.ctrlKey && e.keyCode === 83) {
e.preventDefault();
return false;
}
});
// 우클릭 방지
document.addEventListener('contextmenu', (e) => {
e.preventDefault();
return false;
});
// 드래그 방지
document.addEventListener('dragstart', (e) => {
e.preventDefault();
return false;
});
// 선택 방지
document.addEventListener('selectstart', (e) => {
e.preventDefault();
return false;
});
}
onDevToolsDetected() {
this.devToolsOpen = true;
this.susiciousActivity++;
// 조용히 로그만 남기고 게임은 계속 진행
console.warn('Developer tools detected, but allowing game to continue');
// 게임 중단하지 않음 - 주석 처리
// if (window.game) {
// window.game.gameRunning = false;
// clearInterval(window.game.timerInterval);
// }
}
// 일반적인 해킹 시도 방지
preventCommonHacks() {
// Script injection 방지
const originalEval = window.eval;
window.eval = function() {
throw new Error('eval() is disabled for security reasons');
};
// Function constructor 차단
const originalFunction = window.Function;
window.Function = function() {
throw new Error('Function constructor is disabled for security reasons');
};
// setTimeout/setInterval 문자열 실행 차단
const originalSetTimeout = window.setTimeout;
const originalSetInterval = window.setInterval;
window.setTimeout = function(callback, delay) {
if (typeof callback === 'string') {
throw new Error('String-based setTimeout is disabled for security reasons');
}
return originalSetTimeout.apply(this, arguments);
};
window.setInterval = function(callback, delay) {
if (typeof callback === 'string') {
throw new Error('String-based setInterval is disabled for security reasons');
}
return originalSetInterval.apply(this, arguments);
};
// innerHTML 모니터링
const originalInnerHTML = Object.getOwnPropertyDescriptor(Element.prototype, 'innerHTML');
Object.defineProperty(Element.prototype, 'innerHTML', {
set: function(value) {
if (typeof value === 'string' && (value.includes('<script') || value.includes('javascript:'))) {
console.warn('Suspicious innerHTML detected:', value);
return;
}
originalInnerHTML.set.call(this, value);
},
get: originalInnerHTML.get
});
}
// 게임 무결성 검사
setupIntegrityChecks() {
// 게임 변수 보호
if (window.game) {
this.protectGameObject();
}
// DOM 조작 감지
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
// 의심스러운 DOM 변경 감지
if (mutation.addedNodes.length > 0) {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === 1) { // Element node
const element = node;
if (element.tagName === 'SCRIPT' ||
element.innerHTML?.includes('script') ||
element.innerHTML?.includes('eval')) {
console.warn('Suspicious script injection detected');
element.remove();
this.susiciousActivity++;
}
}
});
}
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});
}
protectGameObject() {
if (!window.game) return;
// 게임 레벨 조작 방지
let actualLevel = window.game.level;
Object.defineProperty(window.game, 'level', {
get: function() {
return actualLevel;
},
set: function(value) {
// 비정상적인 레벨 증가 감지
if (value > actualLevel + 1 || value > 50) {
console.warn('Abnormal level change detected:', value);
security.susiciousActivity++;
return;
}
actualLevel = value;
}
});
// 시간 조작 방지
let actualTimeLeft = window.game.timeLeft;
Object.defineProperty(window.game, 'timeLeft', {
get: function() {
return actualTimeLeft;
},
set: function(value) {
// 시간 증가 시도 감지
if (value > actualTimeLeft + 1) {
console.warn('Time manipulation detected:', value);
security.susiciousActivity++;
return;
}
actualTimeLeft = value;
}
});
}
// 게임플레이 모니터링
monitorGameplay() {
// 게임 시작 시간 기록
this.gameStartTime = Date.now();
// 비정상적으로 빠른 클릭 감지
let clickCount = 0;
let lastClickTime = 0;
document.addEventListener('click', (e) => {
const now = Date.now();
if (now - lastClickTime < 50) { // 50ms 이내 연속 클릭
clickCount++;
if (clickCount > 5) {
console.warn('Automated clicking detected');
this.susiciousActivity++;
}
} else {
clickCount = 0;
}
lastClickTime = now;
});
}
// 게임 종료 시 무결성 검증
validateGameCompletion(level, playerName) {
// 정상적인 게임 종료는 항상 허용 (보안 경고 방지)
if (level <= 50 && level >= 1) {
return true;
}
const gameEndTime = Date.now();
const gameDuration = gameEndTime - (this.gameStartTime || gameEndTime);
// 의심스러운 활동이 너무 많은 경우 (임계값 높임)
if (this.susiciousActivity >= (this.maxSuspiciousThreshold + 2)) {
console.warn('Too many suspicious activities detected');
return false;
}
// 너무 짧은 게임 시간 (임계값 낮춤)
if (gameDuration < 2000) {
console.warn('Game completed too quickly');
return false;
}
// 개발자 도구 체크 완전 제거
// if (this.devToolsOpen) {
// console.warn('Developer tools were detected during game');
// }
// 레벨당 최소 시간 체크 (평균 3초)
const minTimePerLevel = level * 3000;
if (gameDuration < minTimePerLevel && level > 5) {
console.warn('Game progression too fast for level:', level);
return false;
}
return true;
}
// API 요청에 무결성 토큰 추가
generateIntegrityToken(data) {
const timestamp = Date.now();
const gameData = JSON.stringify(data);
const token = btoa(gameData + timestamp + navigator.userAgent.slice(0, 10));
return { token, timestamp };
}
}
// 보안 매니저 인스턴스 생성
let security;
// 페이지 로드 시 보안 초기화
document.addEventListener('DOMContentLoaded', () => {
security = new SecurityManager();
// 게임 객체가 생성된 후 보호 설정
const checkGameObject = setInterval(() => {
if (window.game) {
security.protectGameObject();
clearInterval(checkGameObject);
}
}, 100);
});
// 전역 오류 처리
window.addEventListener('error', (e) => {
console.warn('Script error detected:', e.message);
});
// 페이지 언로드 시 정리
window.addEventListener('beforeunload', () => {
if (security) {
security.susiciousActivity = 0;
}
});