-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
153 lines (140 loc) · 4.71 KB
/
background.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
var dns = {};
var cancelRequestsTo = {};
var ignorePingCheck = {};
var checkDomain = {};
function SendPingCheck(url){
var xmlhttp;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4){
//console.log("[SendPingCheck] unset ignore ping check for url: " + url);
ignorePingCheck[url] = false;
}
}
xmlhttp.open("GET", url, true);
xmlhttp.setRequestHeader('Cache-Control', 'no-cache');
xmlhttp.send();
}
setInterval(function(){
var keyNames = Object.keys(checkDomain);
var l = keyNames.length;
for(var i = 0; i<l; i++){
var url = keyNames[i];
ignorePingCheck[url] = true;
//console.log("[ignorePingCheck] set for ping url: " + url);
//console.log("[BLOCKED DOMAIN - "+keyNames[i]+"] - Checking to see if rebind attack is over with url: " + url);
delete checkDomain[url];
SendPingCheck(url);
}
}.bind(this), 10000);
function CheckDNS(fqdn, ip){
if(!ip) return true; //cached maybe?
if(dns[fqdn]){
//new ip is rfc1918
if(/(^127\.)|(^192\.168\.)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^::1$)|(^[fF][cCdD])/.test(ip)){
if(!/(^127\.)|(^192\.168\.)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^::1$)|(^[fF][cCdD])/.test(dns[fqdn])){
console.log("[DNS REBIND ATTACK DETECTED] - " + fqdn + " - " + ip);
cancelRequestsTo[fqdn] = ip;
return false;
} else {
//or else what?
}
} else {
// set dns to new ip
if(dns[fqdn] != ip){
dns[fqdn] = ip;
//console.log("[IP CHANGE] - " + fqdn + " - " + ip);
} else {
dns[fqdn] = ip;
}
}
} else {
// set dns initially
//console.log("[IP SET] - " + fqdn + " - " + ip );
dns[fqdn] = ip;
}
if(cancelRequestsTo[fqdn]){
console.log("[REBIND ATTACK OVER] for domain " + fqdn + " removing block.");
delete cancelRequestsTo[fqdn];
}
return true;
}
function getHostData(url) {
var m = /^(?:(\w+):)?\/\/([^\/\?#]+)/.exec(url);
if (!m || !m[2]) {
return null;
}
var result = {
scheme:m[1],
initialHost:m[2],
host:m[2],
forceReplace:false
};
return result;
}
chrome.webRequest.onResponseStarted.addListener(function (details){
//console.log("Response started " + JSON.stringify(details));
var hostData = getHostData(details.url);
if (!hostData) return;
/*
if(ignorePingCheck[details.url]){
console.log("IgnorePingCheck - " + details.url + " : " + ignorePingCheck[details.url]);
console.log("IP: " + details.ip);
}*/
var isDnsSafe = CheckDNS(hostData.host, details.ip);
//console.log("onResponseStarted - " + details.url + " : " + details.ip);
if (!isDnsSafe && !ignorePingCheck[details.url]) {
//console.log("[onResponseStarted] Cancel DNS Rebind Attack for: " + details.url)
return {
cancel:true
};
}
},
{
urls: ["<all_urls>"]
},
["responseHeaders"]);
chrome.webRequest.onBeforeRequest.addListener(function (details) {
var hostData = getHostData(details.url);
if (!hostData) return;
if(ignorePingCheck[details.url]){
//console.log("IgnorePingCheck - " + details.url + " : " + ignorePingCheck[details.url]);
//console.log("IP: " + details.ip);
CheckDNS(hostData.host, details.ip);
}
//console.log("IgnorePingCheck - " + details.url + " : " + ignorePingCheck[details.url])
if (cancelRequestsTo[hostData.host] && !ignorePingCheck[details.url]) {
//console.log("[onBeforeRequest] Cancel DNS Rebind Attack for: " + details.url + " - " + cancelRequestsTo[hostData.host]);
var url = "http://" + hostData.host +"/";
ignorePingCheck[url] = true;
//console.log("[ignorePinchCheck] set for ping url: " + url);
console.log("[BLOCKED DOMAIN - "+hostData.host+"] - Checking to see if rebind attack is over with url: " + url);
checkDomain[url] = true;
return {
//cancel:true
redirectUrl : chrome.extension.getURL('blocked.html?fqdn=' + encodeURI(hostData.host) + "&url=" + encodeURI(hostData.host) +"&ip="+ dns[hostData.host] + "&attempt=" + cancelRequestsTo[hostData.host])
};
}
}, {
urls : ["<all_urls>"]
},["blocking"]);
chrome.webRequest.onErrorOccurred.addListener(function (details){
//console.log("[onErrorOccurred] " + JSON.stringify(details));
var hostData = getHostData(details.url);
if (!hostData) return;
var isDnsSafe = CheckDNS(hostData.host, details.ip);
},
{
urls: ["<all_urls>"]
});
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
//log.info("URL Sender: " + sender.tab.url);
//////console.log(request);
if(request.unblock){
console.log("[UNBLOCKED DOMAIN] - User initiated unblock of : " + request.unblock);
delete dns[request.unblock];
delete cancelRequestsTo[request.unblock];
}
}
);