-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_block.js
745 lines (608 loc) · 20.1 KB
/
sample_block.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
//Strips http or https from URL
function strip_http(url){
/* Remove http from URL */
if (url.indexOf("http://") != -1){
url = url.substr("http://".length);
}
/* Remove https from URL */
if (url.indexOf("https://") != -1){
url = url.substr("https://".length);
}
return url;
}
//checks for crossdomain or crossdomain- in the blacklist
function check_wildchar(bl){
var index = bl.indexOf("crossdomain");
var index2 = bl.indexOf("crossdomain-");
if(index>-1){
bl.splice(index, 1);
}
if(index2>-1){
bl.splice(index2, 1);
}
if((index>-1) || (index2>-1)){
return true;
}
return false;
}
//checks whether given request is to same domain or different domain
function check_xdomain(details,wl){
var s_source = details.url.split("://");
var s_source1=s_source[1].split("/")
var s_source2=s_source1[0].split(".");
script_source=s_source2[1]+"."+s_source2[2];
//alert(script_source);
var flag_wl=false;
//check whether whitelist is empty
if (typeof wl !== 'undefined' && wl.length > 0) {
var wl_len=wl.length;
for (i = 0; i < wl_len; i++) {
flag_wl=wl[i].search(script_source);
if(flag_wl)
{
return flag_wl;
}
}
}
var referer_value="dummy";
for (var i = 0; i < details.requestHeaders.length; ++i) {
if (details.requestHeaders[i].name.toLowerCase() === 'referer')
{
var r_value=details.requestHeaders[i].value.split("://");
var r_domain=r_value[1].split("/");
var r_value2=r_domain[0].split(".");
referer_value=r_value2[1]+"."+r_value2[2];
}
}
var result= referer_value.search(script_source);
//alert(referer_value+script_source+result);
return result;
}
//checks and whitelists the requets made by our extension
function check_extn(url){
url=strip_http(url);
var s=url.split("/");
url = "*://"+s[0]+"/*";
if (url=="*://chrome-extension:/*"){
return true;
}
return false;
}
//function to strip additonal RegEx charcters from whitelist
function strip_whitelist(wl){
var wl_len=wl.length;
var strip_wl=[]
for (var i = 0; i < wl_len; i++) {
if(wl[i]=="<all_urls>"){
strip_wl.push(wl[i]) ;
}
else{
var temp2=wl[i].split('*://*.');
if(typeof temp2[1] ==='undefined'){
temp2=wl[i].split('*://.');
}
if(typeof temp2[1] ==='undefined'){
temp2=wl[i].split('www.');
}
if(typeof temp2[1] !=='undefined'){
var temp3=temp2[1].split("/*");
if(typeof temp3[1] ==='undefined'){
temp3=temp2[1].split("/");
}
if(typeof temp3[0] !=='undefined'){
strip_wl.push(temp3[0]) ;
}
}
}
}
return strip_wl;
}
//Disables inline scripts by adding CSP headers
function CSP_filterJS(details) {
//strips additional characters
var strip_wl=strip_whitelist(wl);
// check for whitelist
var wl_flag=check_white_list(details.url,strip_wl);
if(!wl_flag){
var headers = details.responseHeaders;
var jLen=headers.length;
var flag=false;
for (var j = 0; j !== jLen; ++j) {
var header = headers[j];
var name = header.name.toLowerCase();
if (name !== "content-security-policy" &&
name !== "x-webkit-csp") {
continue;
}
else
{
flag=true;
alert(header.value);
var n = header.value.search("script-src");
if(n<0)
{
header.value =header.value+ "script-src 'none'";
}
header.value=header.value.replace("'unsafe-inline'","");
header.value=header.value.replace("'unsafe-eval'","");
}
}
if(!flag){
var element = {};
var new_csp_header="content-security-policy";
var new_csp_value="script-src 'none'";
element.name=new_csp_header;
element.value=new_csp_value;
headers.push(element);
}
}
return {responseHeaders: headers};
}
//Disables cross origin scripts by adding CSP headers
function CSP_crossoriginJS(details) {
var headers = details.responseHeaders;
var jLen=headers.length;
var flag=false;
for (var j = 0; j !== jLen; ++j) {
var header = headers[j];
var name = header.name.toLowerCase();
if (name !== "content-security-policy" &&
name !== "x-webkit-csp") {
continue;
}
else
{
flag=true;
//alert(header.value);
var n = header.value.search("script-src");
if(n<0)
{
header.value =header.value+ "script-src 'self'";
}
//header.value=header.value.replace("'unsafe-inline'","");
// header.value=header.value.replace("'unsafe-eval'","");
}
}
if(!flag){
var element = {};
var new_csp_header="content-security-policy";
var new_csp_value="script-src 'self'";
element.name=new_csp_header;
element.value=new_csp_value;
headers.push(element);
}
return {responseHeaders: headers};
}
//Disables iframes by adding x-frame option headers
function add_xframe(details) {
var headers = details.responseHeaders;
var jLen=headers.length;
var flag=false;
for (var j = 0; j !== jLen; ++j) {
var header = headers[j];
var name = header.name.toLowerCase();
if (name !== "x-frame-options") {
continue;
}
else
{
flag=true;
//alert(header.value);
header.value ="DENY";
}
}
if(!flag){
var element = {};
var new_csp_header="x-frame-options";
var new_csp_value="DENY";
element.name=new_csp_header;
element.value=new_csp_value;
headers.push(element);
}
return {responseHeaders: headers};
}
//Set Set-Cookie resposne header to HttpOnly
function set_cookieHttpOnly(details) {
var headers = details.responseHeaders;
var jLen=headers.length;
var flag=false;
for (var j = 0; j !== jLen; ++j) {
var header = headers[j];
var name = header.name.toLowerCase();
if (name !== "set-cookie") {
continue;
}
else{
flag=true;
//alert(header.value);
header.value =header.value+"; HttpOnly";
//alert(header.value);
}
}
return {responseHeaders: headers};
}
//function to calculate match for sensitive URLs to protect against phishing
function match_sensitive(details,wl){
url=strip_http(details);
//alert("url"+url);
var s=url.split("/");
//alert("s[0]"+s[0]);
//url = "*://"+s[0]+"/*";
var sensitive=false;
var wl_len=wl.length;
var match=[];
for (var i = 0; i < wl_len; i++) {
var wl_url=wl[i];
var str_len=wl_url.length;
if(str_len>(s[0].length))
{
len=str_len;
}
else{
len=(s[0].length)
}
var count=0;
for (j=0;j<len;j++)
{
if (wl_url[j]===s[0][j])
{
count=count+1;
}
}
match.push((count/len)*100);
}
for(var i = 0; i < wl_len; i++){
if (match[i]>50 && match[i]!=100){
//sensitive=true;
//alert("Given domain" + s[0] +" matches your sensitive domain " + wl[i] + ",match%" + match[i])
var r = confirm("Given domain " + s[0] +" matches your sensitive domain " + wl[i] + " ,match " + match[i] + "%. Do you want to continue?");
sensitive=!r;
}
}
return sensitive;
}
//returns true if domain in whitelist
function check_white_list(url,wl){
url=strip_http(url);
var s=url.split("/");
url = "*://"+s[0]+"/*";
if (url=="*://chrome-extension:/*"){
return false;
}
//check whether whitelist is empty
if (typeof wl !== 'undefined' && wl.length > 0) {
var wl_len=wl.length;
var flag_wl=false;
for (var i = 0; i < wl_len; i++) {
var wl_url=wl[i];
var p= new RegExp(wl_url, 'i');
flag_wl=p.test(s[0]);
if(flag_wl)
{
return flag_wl;
}
}
}
return flag_wl;
}
//function to monitor requests
function monitor_request(type,wl,bl){
var strip_wl=strip_whitelist(wl);
//check blacklist for crossdomain or crossdomain-
var wildcard_flag= check_wildchar(bl);
//cancel requests to blacklisted URLs
if (bl.length > 0) {
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
var denyrequest=true;
// check for whitelist
var wl_flag=check_white_list(details.url,strip_wl);
if(check_extn(details.url)){
denyrequest=false;
}
else if(wl_flag === true)
{
denyrequest=false;
alert("Permitted request type "+ type +" : " + details.url);
}
else{
alert("blocking request type "+ type +" : " + details.url);
}
return {cancel: denyrequest}; },
// filters
{
urls: bl,
types: [type]
},
// extraInfoSpec
["blocking"]);
if(type==="script"){
chrome.webRequest.onHeadersReceived.addListener(CSP_filterJS, {
urls: js_bl,
types: ["main_frame", "sub_frame"]
}, ["blocking", "responseHeaders"]);
}
if(type==="sub_frame"){
chrome.webRequest.onHeadersReceived.addListener(add_xframe, {
urls: iframe_bl,
types: ["main_frame", "sub_frame"]},
["blocking", "responseHeaders"]);
}
}
//cancel image requests to cross-origin domains
if(wildcard_flag)
{
chrome.webRequest.onBeforeSendHeaders.addListener(
function(details) {
//check if given domain is cross-domain
var flag= check_xdomain(details);
//check whether this crossdomain is in whitelist
var flag2=check_white_list(details.url,strip_wl);
if(flag>-1 || flag2 === true)
{
alert("Permitted request type "+ type +" : " + details.url);
}
else
{
alert("crossdomain request type "+ type +" : " + details.url);
return {cancel: true};
}
//alert("script source:"+details.url);
},
{urls: ["<all_urls>"], types: [type]},
["blocking", "requestHeaders"]);
}
}
function monitor_responses(type,wl,bl){
var strip_wl=strip_whitelist(wl);
//check blacklist for crossdomain or crossdomain-
var wildcard_flag= check_wildchar(bl);
//cancel requests to blacklisted URLs
if (bl.length > 0) {
chrome.webRequest.onHeadersReceived.addListener(
function(details) {
for (var i = 0; i < details.responseHeaders.length; ++i) {
if(details.responseHeaders[i].name.toLowerCase() == 'content-type') {
var headervalue=(details.responseHeaders[i].value.toLowerCase());
//alert(headervalue);
var app= headervalue.search(type)
if(app >-1){
//if (details.responseHeaders[i].value.toLowerCase() == 'application/octect-stream'){
alert("blocking "+details.responseHeaders[i].value);
//details.responseHeaders.splice(i, 1);
return {cancel: true};
break;
}
}
}
return {responseHeaders: details.responseHeaders};
},
// filters
{
urls: bl,
},
// extraInfoSpec
["blocking","responseHeaders"]);
}
//cancel responses to cross-origin domains
if(wildcard_flag)
{
chrome.webRequest.onHeadersReceived.addListener(
function(details) {
var flag= check_xdomain(details);
//check whether this crossdomain is in whitelist
var flag2=check_white_list(details.url,strip_wl);
if(flag>-1 || flag2 === true)
{
alert("Permitted response type "+ type +" : " + details.url);
}
else{
for (var i = 0; i < details.responseHeaders.length; ++i) {
if(details.responseHeaders[i].name.toLowerCase() == 'content-type') {
var headervalue=(details.responseHeaders[i].value.toLowerCase());
//alert(headervalue);
var app= headervalue.search(type)
if(app >-1){
//if (details.responseHeaders[i].value.toLowerCase() == 'application/octect-stream'){
alert("blocking "+details.responseHeaders[i].value);
//details.responseHeaders.splice(i, 1);
return {cancel: true};
break;
}
}
}
}
return {responseHeaders: details.responseHeaders};
},
// filters
{
urls: ["<all_urls>"], },
// extraInfoSpec
["blocking","responseHeaders"]);
}
}
//function to modify request headers
function modify_request(header_value,wl,bl){
var strip_wl=strip_whitelist(wl);
//check blacklist for crossdomain or crossdomain-
var wildcard_flag= check_wildchar(bl);
//cancel requests to blacklisted URLs
if (bl.length > 0) {
chrome.webRequest.onBeforeSendHeaders.addListener(
function(details) {
// check for whitelist
var wl_flag=check_white_list(details.url,strip_wl);
if(wl_flag === true){
alert("Permitted request header "+ header_value + " for "+ details.url);
}
else{
for (var i = 0; i < details.requestHeaders.length; ++i) {
if (details.requestHeaders[i].name === header_value) {
alert("blocking header "+ header_value +" : "+ details.requestHeaders[i].value+ " for "+details.url );
details.requestHeaders.splice(i, 1);
break;
}
}
}
return {requestHeaders: details.requestHeaders};
},
// filters
{ urls: bl },
// extraInfoSpec
["blocking", "requestHeaders"]);
}
//modify headers requests to cross-origin domains
if(wildcard_flag)
{
chrome.webRequest.onBeforeSendHeaders.addListener(
function(details) {
var flag= check_xdomain(details);
//check whether this crossdomain is in whitelist
var flag2=check_white_list(details.url,strip_wl);
if(flag>-1 || flag2 === true)
{
alert("Permitted request header "+ header_value + " for "+ details.url);
}
else{
for (var i = 0; i < details.requestHeaders.length; ++i) {
if (details.requestHeaders[i].name === header_value) {
alert("blocking header "+ header_value +" : "+ details.requestHeaders[i].value+ " for "+details.url );
details.requestHeaders.splice(i, 1);
break;
}
}
}
return {requestHeaders: details.requestHeaders};
},
// filters
{
urls: ["<all_urls>"], },
// extraInfoSpec
["blocking","requestHeaders"]);
}
}
//function to remove all cookies
function deletecookie(cookie) {
var url = "http" + (cookie.secure ? "s" : "") + "://" + cookie.domain + cookie.path;
chrome.cookies.remove({"url": url, "name": cookie.name});
};
//Monitors image requests when flag is set
if (imageflag) {
monitor_request("image",image_wl,image_bl);
}
//Monitors script requests when flag is set
if (jsflag) {
monitor_request("script",js_wl,js_bl);
}
//Monitors font requests when flag is set
if (fontflag) {
monitor_request("font",font_wl,font_bl);
}
//Monitors object requests when flag is set
if (objectflag) {
monitor_request("object",object_wl,object_bl);
}
//Monitors XMLHttpRequests when flag is set
if (XHRflag) {
monitor_request("xmlhttprequest",XHR_wl,XHR_bl);
}
//Monitors stylesheets requests when flag is set
if (stylesheetflag) {
monitor_request("stylesheet",stylesheet_wl,stylesheet_bl);
}
//Monitors media requests when flag is set
if (mediaflag) {
monitor_request("media",media_wl,media_bl);
}
//Monitors iframe requests when flag is set
if (iframeflag) {
monitor_request("sub_frame",iframe_wl,iframe_bl);
}
//create blacklist
if(blockflag)
{
var strip_wl=strip_whitelist(white_list);
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
var denyrequest=true;
if(check_extn(details.url)){
denyrequest=false;
}
else{
denyrequest=!(check_white_list(details.url, strip_wl));
}
return {cancel: denyrequest}; },
{urls: blocked_bl},
["blocking"]);
}
//Monitors responses for application payload when flag is set
if (appflag) {
monitor_responses("application",app_wl,app_bl);
}
//Monitors responses for executable application payload when flag is set
if (exeflag) {
monitor_responses("application/octet-stream",exe_wl,exe_bl);
}
//blocking non https connections
if (httpflag) {
var strip_wl=strip_whitelist(non_https_wl);
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
var denyrequest=true;
if(check_extn(details.url)){
denyrequest=false;
}
else{
denyrequest=!(check_white_list(details.url, strip_wl));
}
return {cancel: denyrequest}; },
{urls: non_https_bl},
["blocking"]);
}
//modify header to remove user-agent value
if(UAflag){
modify_request("User-Agent",UA_wl,UA_bl);
}
//modify header to remove referer value
if(refererflag){
modify_request("referer",referer_wl,referer_bl);
}
//modify header to remove cookie values
if (cookiesflag) {
modify_request("cookie",cookies_wl,cookies_bl);
}
//set HttpOnly Cookies for all domains given in whitelist
if(HttpOnlycookiesflag){
//first it will delete all cookies
chrome.cookies.getAll({},function (all_cookies) {
var count = all_cookies.length;
for (var i = 0; i < count; i++) {
deletecookie(all_cookies[i]);
}
});
//all cookies will be set with HttpOnly
chrome.webRequest.onHeadersReceived.addListener(set_cookieHttpOnly, {
urls: HttpOnlycookies_wl,
types: ["main_frame", "sub_frame"]
}, ["blocking", "responseHeaders"]);
}
//prevent phishing attacks by URL matching
if (sensitiveflag) {
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
var denyrequest=true;
var sensitivity=match_sensitive(details.url,sensitive_wl);
if(!sensitivity){
denyrequest=false;
}
else{
//alert("Phishing possible. Blocking");
}
return {cancel: denyrequest}; },
// filters
{
urls: ["<all_urls>"]
},
// extraInfoSpec
["blocking"]);
}