Click here to Skip to main content
11,640,626 members (58,766 online)
Click here to Skip to main content

Ad & Spyware Prevention Using Your Hosts File

, 3 Apr 2006 39.9K 263 18
Rate this:
Please Sign up or sign in to vote.
This article explains how you can prevent ads and spyware, using your hosts file.

Introduction

I created this script to automate the process of updating your hosts file from the list contained on http://www.someonewhocares.org/hosts/. Using this hosts file is the best way I have found to "make the internet not suck (as much)" just like it claims to.

The VBScript downloads the web page to a local temp file, parses it so only comments and wanted lines are contained, then puts this file in the proper directory. It also creates a backup copy of the hosts file, and sets the new one to read-only. Just double-click to execute it or if the "all was successful" echo is removed from the end, then it may be run as a Windows scheduled task. I have tested it on Windows 2K, 2003, and XP.

Prevention is better than cleaning, and being able to use hotmail without the "Win Free DVD Player" ad flashing in your face is priceless. Every IT guy should use this!

The Code

    'Create objFSO and objIE objects
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    set objIE=WScript.createobject("internetexplorer.application")


    'Determine path to hosts and set strHostsPath to correct path
    dim bFolder, strHostsPath
    bFolder = objFSO.folderExists("C:\WINDOWS\")
    
    if bFolder = False then

        strHostsPath = "C:\WINNT\system32\drivers\etc\"
    else
        strHostsPath = "C:\WINDOWS\system32\drivers\etc\"
    
    End if    


    'Create Temp Web file here since 
    'it needs a few milliseconds to complete
    Set TempWebFile = objFSO.CreateTextFile("C:\Temp.html", 2)

    'Navigate and wait for page to load
    objIE.Navigate ("http://someonewhocares.org/hosts/")

    Wscript.Sleep 1000
    'If sleep here omitted, an endless loop 
    'happens on windows 2003

    'wait for IE to Download Page before continuing execution
    While objIE.Busy: Wend 

    'Write contents of navigated webpage to local temp Web file
    TempWebFile.Write objIE.Document.body.outerHTML 
    TempWebFile.close 'Close File

    'open the local Webpage and read in all lines
    set PageText = objFSO.GetFile("C:\temp.html")
    set ts = pagetext.OpenAsTextStream(1,-2)


    dim ResultLine,ResultComment,ReadLine
'40

    'Create output file for parsed data from webpage
    Set OutFile = objFSO.OpenTextFile("C:\temp.txt",2,True)
    ' FileMode = 1=read 2=Write 8=Append


    Do while not ts.AtEndOfStream


        ReadLine = ts.readline   
        ResultLine = InStr(Readline, "127.0.0.1")
        ResultComment = InStr(Readline, "#")

        'Weed out anything but Good lines, 
        '  comments, and blanklines, and write temp file.
        if (ResultLine = 1) or (ResultComment = 1) _
                            or (ReadLine = "") then
            OutFile.WriteLine(ReadLine)
        end if 
    Loop
    
    ts.close
    OutFile.close 'Close File


    'Set host file to normal attributes from whatever it may be 
    Set objHostsFile = objFSO.GetFile(strHostsPath + "hosts")
    objHostsFile.Attributes = 0

    'Backup existing hosts file
    set BackFile=objFSO.GetFile(strHostsPath + "hosts")
    BackFile.Copy strHostsPath + "hosts.bak"

    'Copy new hosts file
    set NewFile=objFSO.GetFile("C:\temp.txt")
    NewFile.Copy strHostsPath + "hosts"

    'Delete Temp Files
    set DeleteFile=objFSO.GetFile("C:\Temp.html")
    DeleteFile.delete
    NewFile.delete

    'Set new host file attribute to read only
    objHostsFile.Attributes = 1

    WScript.Echo "Your Hosts File Has Been Updated Successfully!"

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

nieljake
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralAntivirus Conflicts Pin
Babs Iwarere27-Dec-07 21:56
memberBabs Iwarere27-Dec-07 21:56 
QuestionHuh? Pin
Tremors18-Jan-06 18:24
memberTremors18-Jan-06 18:24 
GeneralWhy not use text version Pin
blader26518-Jan-06 10:53
memberblader26518-Jan-06 10:53 
GeneralInteresting Pin
flupkeDe18-Jan-06 1:37
memberflupkeDe18-Jan-06 1:37 
QuestionEnvironment Variable for System Root? Pin
chmod75512-Jan-06 2:15
memberchmod75512-Jan-06 2:15 
AnswerRe: Environment Variable for System Root? Pin
nieljake28-Mar-06 1:29
membernieljake28-Mar-06 1:29 
GeneralRe: Environment Variable for System Root? Pin
chmod75528-Mar-06 4:04
memberchmod75528-Mar-06 4:04 
GeneralRe: Environment Variable for System Root? Pin
donjuane4-May-06 18:45
memberdonjuane4-May-06 18:45 
GeneralSeems to be working Pin
jairajsun11-Jan-06 12:01
memberjairajsun11-Jan-06 12:01 
Generalsee Pin
zxcv123412341234123411-Jan-06 10:35
memberzxcv123412341234123411-Jan-06 10:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150731.1 | Last Updated 3 Apr 2006
Article Copyright 2006 by nieljake
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid