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.
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?
(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...
haha block https://Login.live.com & http://Login.live.com & both ips :d then no logging into msn :p
How is this a tutorial though? lol.
Thanks for sharing the source code mate, Keep It Up.
this a very nice...

thx to tutorial...
This is very nice any chance of the source for how to do in vb6?
(Feb 19, 2010 05:42 PM)King Flawless Wrote:  This is very nice any chance of the source for how to do in vb6?

char HostsFile[256]; or HostsFile BYTE 260 dup(?) This will basically turn into a pointer to the bytes (array) that is viewable in the hex dump.

Honestly no clue in VB6, just use a file stream and stuff... Here's how it's done in assembler and C++... I am sure you can easily import the functions I am using and just use a simple file io.

Assembler:
Code
.386
.model flat, stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\msvcrt.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\msvcrt.lib

fopen PROTO C :DWORD, :DWORD
fprintf PROTO C :DWORD, :DWORD, :VARARG
fflush PROTO C :DWORD
fclose PROTO C :DWORD

.DATA
extPath db "\System32\drivers\etc\hosts", 00h
HostsWrite db "127.0.0.1 www.google.ca", 13, 10 ;CR LF (carrage return linefeed)
                  db "127.0.0.1 google.ca", 00h ;end of string null terminated
strWINDIR db "WINDIR", 00h
xAB db "ab", 00h
xfprintf db "%s", 00h

.DATA?
HostsPath BYTE 260 dup(?) ;buffer for hosts file
hFile DWORD ?

.CODE
start: ;entry point

push 260
lea edx, HostsPath
push edx
push offset strWINDIR
call GetEnvironmentVariable

lea edx, HostsPath
push edx
push offset extPath
call lstrcat

lea edx, HostsPath
push edx
call GetFileAttributes

cmp eax, -1     ;attributes returned in eax, if its -1 that means file does not exist

push eax
call ExitProcess

jnz _writeFile

_writeFile:
push offset xAB
lea edx, HostsPath
push edx
call fopen

add esp, 2*4
mov hFile, eax ;handle returned into eax

push offset HostsWrite
push offset xfprintf
push hFile
call fprintf

push hFile
call fflush

push hFile
call fclose


push eax
call ExitProcess

end start

C++:
Code
#include <windows.h>
#include <stdio.h>

HANDLE hFile;

int main()
{

char fileWrite[] = "\n127.0.0.1 www.google.ca\n127.0.0.1 google.ca";

char HostsPath[256]; //Yeah, I know I did 260 in assembler, made a typo and i dont feel like fixing it...
GetEnvironmentVariable("WINDIR", HostsPath, 256);

lstrcat(HostsPath, "\\System32\\drivers\\etc\\hosts");

if( GetFileAttributes(HostsPath) != -1 ) //checking if exists
{
hFile = fopen(HostsPath, "ab");
if( hFile != NULL ) // didn't check for this in assembler, didnt feel like typing too much (null = 0)
}
fprintf(hFile, "%s", fileWrite);
fflush(hFile);
fclose(hFile);
}
return 0;

PS: This was done in notepad... so their might be errors

for VB6, google file streams and just import the functions that I am using.
I have better way...
1st create a copy of host on notepad and add the host u wanna block...then make a sfx archive using winrar...put the host file in the archive and make it extract direct to the file that contain the original host...
Its Works great for me...
No that way is not better because it takes multiple files.
This will not work on Windows 7 or any Windows Vista with the UAC in effect. The program will throw a System Access Exception. I have not found a way in C# to get around this yet, but when I do, I will let you know. Reading from the host file can be done, haven't found how to alter it.


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