Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

[Tutorial]Editing hosts file in C# + VB.NET (blocking them from sites)

C#

Code
using System.IO;

Code
string system = Environment.GetFolderPath(Environment.SpecialFolder.System);
            //^ Above creates the string system, and stores the system folder path in it

            string hosts = system + "\\" + "drivers" + "\\" + "etc" + "\\" + "hosts";
            //^ In VB.NET you would only need one slash, but in C# you need two
            // ^ basically this takes the string system and adds the path to it

            StreamWriter writer = new StreamWriter(hosts);
            // ^ Creates a new StreamWriter with the path of our hosts string

            writer.Write("##Hosts file edit");
            //^ Writes ##Hosts file edit in our hosts file

            writer.Write(Environment.NewLine);
            //^ Creates a new line, important so text is not all in 1 line

            writer.Write("127.0.0.1 www.google.ca");
            //^ Writes 127.0.0.1 www.google.ca in our hosts file, redirecting anybody who
            //^ goes to google to 127.0.0.1 which is localhost

            writer.Write(Environment.NewLine);
            //^ See above                

            writer.Write("127.0.0.1 google.ca");
            //^ This is important, because the user cannot go to www.google.ca, but he will
            //^ be able to go to google.ca!!

            writer.Write(Environment.NewLine);
            //^ See above

            writer.Write("127.0.0.1 209.85.135.99");
            //^ Okay, 209.85.135.99 is the IP address of google.  
            //^ if you redirect google.ca and www.google.ca then the user will still
            //^ be able to go to 209.85.135.99, which is google so we want to block that
            //^ Now, if the user tries to go to www.google.ca he will be redirected to
            //^ localhost, if he goes to google.ca he will be redirected to localhost
            //^ and finally, if he is smart and pings google then he will
            //^ get the address 209.85.135.99, but when he tries to go to that he will be
            //^ redirected to localhost bwahahahahah

            writer.Dispose();
            //^ This is important, it closes our streamwriter and saves the data, please
            //^ remember writer.Write will clear everything in the hosts file if the style
            //^ is not append, but I will not get into that now.

            //^ if we did not have our newline this is what our hosts file would look like
            //^ ##Hosts file edit127.0.0.1 www.google.ca127.0.0.1 google.ca127.0.0.1 209.85.135.99
            //^ but our hosts file has to look like the following in order to be read
            
            //^ ##Hosts file edit
            //^ 127.0.0.1 www.google.ca
            //^ 127.0.0.1 google.ca
            //^ 127.0.0.1 209.85.135.99

            //^ I hope you enjoyed my tutorial, feel free to ask for more in C# or VB.NET!

VB.NET

Code
Imports System.IO

Code
Dim systempath As String = Environment.GetFolderPath(Environment.SpecialFolder.System)
        'this creates a new string with the value of the persons system32/62 folder

        Dim hosts As String = systempath & "\" & "drivers" & "\" & "etc" & "\\" & "hosts"
        'this creates a new string with the value of systempath and the above text
        'in VB.NET we do not need two \\'s only one.

        Dim writer As New StreamWriter(hosts)
        'this creates the streamwriter "writer" and sets its path to our string hosts, done above.

        writer.Write("##Hosts file edit")
        'writes ##Hosts file edit in our hosts file
        'same as in C#
        writer.Write(Environment.NewLine)

        ' Creates a new line, important so text is not all in 1 line
        ' same as in C#

        writer.Write("127.0.0.1 www.google.ca")
        ' Writes 127.0.0.1 www.google.ca in our hosts file, redirecting anybody who
        ' goes to google to 127.0.0.1 which is localhost

        writer.Write(Environment.NewLine)
        ' See above                

        writer.Write("127.0.0.1 google.ca")
        ' This is important, because the user cannot go to www.google.ca, but he will
        ' be able to go to google.ca!!

        writer.Write(Environment.NewLine)
        ' See above

        writer.Write("127.0.0.1 209.85.135.99")
        ' Okay, 209.85.135.99 is the IP address of google.  
        ' if you redirect google.ca and www.google.ca then the user will still
        ' be able to go to 209.85.135.99, which is google so we want to block that
        ' Now, if the user tries to go to www.google.ca he will be redirected to
        ' localhost, if he goes to google.ca he will be redirected to localhost
        ' and finally, if he is smart and pings google then he will
        ' get the address 209.85.135.99, but when he tries to go to that he will be
        ' redirected to localhost bwahahahahah

        'SAME AS IN C#

        writer.Dispose()
        ' This is important, it closes our streamwriter and saves the data, please
        ' remember writer.Write will clear everything in the hosts file if the style
        ' is not append, but I will not get into that now.

        ' if we did not have our newline this is what our hosts file would look like
        ' ##Hosts file edit127.0.0.1 www.google.ca127.0.0.1 google.ca127.0.0.1 209.85.135.99
        ' but our hosts file has to look like the following in order to be read

        ' ##Hosts file edit
        ' 127.0.0.1 www.google.ca
        ' 127.0.0.1 google.ca
        ' 127.0.0.1 209.85.135.99

        ' I hope you enjoyed my tutorial, feel free to ask for more in C# or VB.NET!

        'SAME AS IN C#


I did not use a converter, I wrote it in C# first however, then just used my knowledge of VB.NET to translate it.
You didn't hear it from me.

http://support.microsoft.com/kb/927847

Have fun.
(Sep 23, 2009 03:14 PM)6e756c6c Wrote:  You didn't hear it from me.

http://support.microsoft.com/kb/927847

Have fun.

On the road to black-hattary. Welcome, son.
Never! I see some better potential for that - like blocking the ad server or something..
Cool, that would be good to do if you want to block WLM too.
(Sep 23, 2009 03:25 PM)Feminist Wrote:  Cool, that would be good to do if you want to block WLM too.
ya but how would that work? Do you just put:

TCP/UDP 30000 - 65535 that in?
No, you would add something like this to the hosts file:

127.0.0.1 login.live.com

I think it worthy of being mentioned that most AV programs will pick up modified host files - some even go as far as registering another host file altogether rendering this technique useless. : (
(Sep 23, 2009 04:26 PM)6e756c6c Wrote:  No, you would add something like this to the hosts file:

127.0.0.1 login.live.com

I think it worthy of being mentioned that most AV programs will pick up modified host files - some even go as far as registering another host file altogether rendering this technique useless. : (

I have never heard of that
There must be a better way to do this. Editing HOSTS trips every AV, and WinDefender screams like a little girl.
Excellent..thnx alot man...really usefull...
great tut i + repped
haha block https://Login.live.com & http://Login.live.com & both ips :d then no logging into msn :p
I'll use this is my new phisher. [RuneScape]
Hack they're accs and block RuneScape.

THANK YOU FOR THIS!
Not sure why you're using 'writer.Write(Environment.NewLine)' when 'writer.Writeline(w/e)' does the job with less code...
How is this a tutorial though? lol.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by going clearJan 27, 2021 06:19 AM 2 234 Jan 27, 2021 06:19 AM
Last Post: gigajew
[Shell] by Emre_Oct 27, 2020 10:28 AM 0 127 Oct 27, 2020 10:28 AM
Last Post: Emre_
[.NET] by SIM-4581439Apr 7, 2020 12:23 AM 2 196 Apr 7, 2020 12:23 AM
Last Post: SIM-4581439