I'v been trying to block all ips of a specific website using netsh advfirewall for an Electron JavaScript application. Here is my code.
const { app, BrowserWindow } = require('electron')
const { exec } = require('child_process')
const dns = require('dns')
const url = require('url')
const createWindow = () => {
const win = new BrowserWindow({
width: 800,
height: 600
})
win.loadFile('Screens/index.html')
blockWebsite('littlebigsnake.com')
win.setMenu(null)
}
app.whenReady().then(() => {
createWindow()
})
function blockWebsite(urlString) {
// grab host name from input
const { hostname } = url.parse(`http://${urlString}`)
// turn hostname into all ips
dns.resolve(hostname, 'A', (err, addresses) => {
if (err) {
console.error(`Error: ${err}`)
return
}
// block each ip address through the firewall
addresses.forEach(address => {
exec(`netsh advfirewall firewall add rule name="Block ${urlString}" dir=out action=block remoteip=${address}`, (error, stdout, stderr) => {
if (error) {
console.error(`${error}`)
return
}
console.log(`stdout: ${stdout}`)
console.log(`stderr: ${stderr}`)
})
})
})
}
Here is the error I am getting Error: Command failed: netsh advfirewall firewall add rule name="Block littlebigsnake.com" dir=out action=block remoteip=104.26.9.138
Error: Command failed: netsh advfirewall firewall add rule name="Block littlebigsnake.com" dir=out action=block remoteip=172.67.71.90
Error: Command failed: netsh advfirewall firewall add rule name="Block littlebigsnake.com" dir=out action=block remoteip=104.26.8.138
Figured it out.
const blockWebsite = (website) => {
const command = `echo 127.0.0.1 ${website} >> C:\\Windows\\System32\\drivers\\etc\\hosts`
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`${error}`)
return
}
console.log(`Success: ${stdout}`)
})
}
Just write the website url to hosts file. The hosts file will then redirect all outgoing attempts to this website into a crash(not quite, but close enough). I can't get it working without admin privileges however. So you need to run as admin.