-
Notifications
You must be signed in to change notification settings - Fork 13
/
drmine.js
executable file
·176 lines (159 loc) · 4.83 KB
/
drmine.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
#!/usr/bin/env node
const fs = require('fs');
const { URL } = require('url');
const Promise = require('bluebird');
const puppeteer = require('puppeteer-core');
let browser;
let online_miners;
const scanned_hosts = [];
const culprit_hosts = [];
const path = process.argv[2];
const config = require('./config.js');
async function init(){
browser = await puppeteer.launch({
//headless: false,
ignoreHTTPSErrors: true,
executablePath: config.executablePath,
args: [
'--no-sandbox',
'--disable-gpu',
'--disable-extensions',
'--dns-prefetch-disable',
'--disable-dev-shm-usage',
'--ignore-certificate-errors',
'--allow-running-insecure-content',
'--enable-features=NetworkService',
],
}).catch(e => console.error('Error!', e.message));
}
async function responseInfo(url){
// remove hosts already found mining
if(culprit_hosts.includes(new URL(url).host))
return;
let page;
try {
page = await browser.newPage();
page.setRequestInterception(true);
page.on('request', req => intercept(req, url));
await page.goto(url, {timeout: config.timeout});
const host = new URL(page.url()).host;
await page._client.send('Page.setDownloadBehavior', {behavior: 'deny'});
if(!scanned_hosts.includes(host)){ // scan only if not already scanned
scanned_hosts.push(host);
const hrefs = await page.$$eval('a[href]',
anchors => anchors.map(a=>a.href));
checkLinks(host, hrefs);
}
}
catch(e) {
if(!/ERR_NAME_NOT_RESOLVED|Target closed|not opened/.test(e.message)){
console.info('Trying to reload', url);
await page.reload({timeout: config.timeout});
}
else
log(url, e.message);
}
finally {
await page.close();
}
}
function intercept(request, url){
// abort request to unneccessary resources
if(config.resources.includes(request.resourceType())){
return request.abort();
}
else if('script' === request.resourceType()){
if(detectMiner(request, url))
return request.abort();
}
request.continue();
}
async function getMiners(){
let page;
try{
page = await browser.newPage();
await page.goto(config.list_browser, {timeout: config.timeout});
online_miners = await page.evaluate(()=>document.body.innerText);
if(!online_miners){
throw "Something went wrong, online_miners seems emtpy :(";
}
online_miners = online_miners.trim().split('\n').map(i => i.trim());
}
catch(e){
console.error(e.message);
}
finally {
await page.close();
}
}
function checkLinks(host, hrefs){
if(!hrefs) return;
const links = hrefs.filter((value, index, self) => {
// filter unique and same-origin hosts
return self.indexOf(value) == index && host === new URL(value).host
});
proceed(links);
}
function detectMiner(request, url){
let flag = false;
// ignore results of redirect - TODO
const reqURL = new URL(request.url());
if(online_miners.includes(reqURL.host)){
flag = true;
culprit_hosts.push(new URL(url).host);
console.info(`${url} => ${reqURL.href}`);
writeFile(config.output_file, {url: url, msg: ` => Found using ${reqURL}`});
}
return flag;
}
function writeFile(fileName, data, flag='a'){ // data expected to be an object with attrs url and msg
fs.writeFile(fileName, `${data.url} ${data.msg}\n`, {flag: flag}, (err) => {
if(err) console.error(err)
});
}
function log(url, msg){
writeFile(config.log_file, {url: url, msg: msg});
}
async function proceed(urls){
if(!urls.length) return;
try{
await Promise.map(
urls,
async (url, index, length) => {
try{
await responseInfo(url);
}
catch(e){
console.info('[!] Number of URLs processed:', index);
}
},
{concurrency: config.tabs}
);
}
catch(e){
console.error('Error!', e.message);
}
finally {
//await browser.close();
console.info('[!] Time:', Date());
console.info("[-] Terminate (CTRL+C) if idle for over a minute");
}
}
(async () => {
await init();
await getMiners();
if(/^https?:\/\//i.test(path)){
return proceed([path]);
}
try{
fs.readFile(path, 'utf8', (error, data) => {
proceed(data.trim().split('\n'));
});
}
catch(e){
console.error(e.message);
}
})();
/***********************************************************
* Usage: node automation.js lists.txt *
***********************************************************/