Jump to content

Windows Site Redirector


IdinAK255

Recommended Posts

This project started out as a joke. I decided that I was going to make a program that blocked a certain online game which a friend of mine is absolutely addicted to. Then I took that program and modified it so that it would instead redirect the user to another website instead of just blocking it. I know it's not very hard to code such a program (took me a few minutes), but I thought I would give something back to the Hak5 forums because the show is just awesome. I just started watching nearly a week ago and I find it to be very interesting and informative.

I'm constantly working on different projects and since it's almost summer time you can bet that I will be coding a lot. When I finish some of my more interesting projects I will post them here. Anyway, the source code is below.

// Author: int main() from http://forums.hak5.org/

// Project name: Windows Site Redirector

#include <stdio.h>

#include <windows.h>

#define BUFFER 100 // BUFFER is used to define the amount of bytes that should be allocated to each site's string value.

#define NUM_SITES 1 // NUM_SITES indicates the number of sites you would like to redirect.

void redirectSite(void);

void main(void)

{

/* The two sets of if statements which follow disable the read only property of the hosts file

and hide the folder which it's located in. These could be of use if you depending on what you

are going to be using this program for... :0 */

if (GetFileAttributes("C:\\Windows\\System32\\drivers\\etc\\hosts") == FILE_ATTRIBUTE_READONLY)

SetFileAttributes("C:\\Windows\\System32\\drivers\\etc\\hosts", FILE_ATTRIBUTE_NORMAL);

if (GetFileAttributes("C:\\Windows\\System32\\drivers\\etc\\") != FILE_ATTRIBUTE_HIDDEN)

SetFileAttributes("C:\\Windows\\System32\\drivers\\etc\\", FILE_ATTRIBUTE_HIDDEN);

redirectSite();

}

void redirectSite(void)

{

int i;

/* intended_site is a two dimensional character array (string array) which is

used to indicate the sites which you would like the user to be redirected from.

For each site you must create two row elements in the array of these two forms. */

char intended_site[NUM_SITES * 2][bUFFER]

{

"gmail.com", // Form 1: (sitenamegoeshere.com)

"www.gmail.com" // Form 2: (www.sitenamegoeshere.com)

};

/* redirected_site_ip is a two dimensional character array (string array) which is

used to indicate the IP addresses of the sites which you would like the user to be redirected to.

Make sure that each IP address in redirected_site_ip corresponds to the correct site in

intended_site. */

char redirected_site_ip[NUM_SITES * 2][bUFFER]

{

"IP address to be redirected to",

"Same as above"

}

FILE *hosts;

hosts = fopen("C:\\Windows\\System32\\drivers\\etc\\hosts", "r+"); // Open the hosts file for writing.

fseek(hosts, 0, SEEK_END);

fprintf(hosts, "\n");

/* Writes the elements from intended_site and redirected_site_ip in the correct

format into the hosts file. */

for (i = 0; i < NUM_SITES; i++)

{

fprintf(hosts, "%s\t%s\n", redirected_site_ip, intended_site);

}

fclose(hosts); // Closes the hosts file.

}

Link to comment
Share on other sites

Or you could set up a transparent proxy. Would still bring the same result. But a very nice idea, thanks for posting that up...

Edited by Infiltrator
Link to comment
Share on other sites

Or you could set up a transparent proxy. Would still bring the same result. But a very nice idea, thanks for posting that up...

Thanks. Also, I'm sorry for the lack of indentation in my code. I didn't realize that my code copied over without proper indentation until I posted it the topic.

Link to comment
Share on other sites

I just realized there were some errors in my original code. Here's the working code.

// Author: int main() from http://forums.hak5.org/
// Project name: Windows Site Redirector

#include &lt;stdio.h&gt;
#include &lt;windows.h&gt;

#define BUFFER 100 // BUFFER is used to define the amount of bytes that should be allocated to each site's string value.
#define NUM_SITES 1 // NUM_SITES indicates the number of sites you would like to redirect.

void redirectSite(void);

void main(void)
{
	/* The two sets of if statements which follow disable the read only property of the hosts file
	and hide the folder which it's located in. This could be of use depending on what you
	are going to be using this program for... :0 */
	if (GetFileAttributes("C:\\Windows\\System32\\drivers\\etc\\hosts") == FILE_ATTRIBUTE_READONLY) 
		SetFileAttributes("C:\\Windows\\System32\\drivers\\etc\\hosts", FILE_ATTRIBUTE_NORMAL); 

	if (GetFileAttributes("C:\\Windows\\System32\\drivers\\etc\\") != FILE_ATTRIBUTE_HIDDEN) 
		SetFileAttributes("C:\\Windows\\System32\\drivers\\etc\\", FILE_ATTRIBUTE_HIDDEN); 

	redirectSite();
}

void redirectSite(void)
{
	int i;

	/* intended_site is a two dimensional character array (string array) which is
	used to indicate the sites which you would like the user to be redirected from. 
	For each site you must create two row elements in the array of these two forms. */
	char intended_site[NUM_SITES * 2][BUFFER] =
	{ 
		"gmail.com", // Form 1: (sitenamegoeshere.com)
		"www.gmail.com" // Form 2: (www.sitenamegoeshere.com)
	};

	/* redirected_site_ip is a two dimensional character array (string array) which is
	used to indicate the IP addresses of the sites which you would like the user to be redirected to. 
	Make sure that each IP address in redirected_site_ip corresponds to the correct site in 
	intended_site. */
	char redirected_site_ip[NUM_SITES * 2][BUFFER] =
	{
		"IP address to be redirected to",
		"Same as above"
	};

	FILE *hosts; 
	hosts = fopen("C:\\Windows\\System32\\drivers\\etc\\hosts", "r+"); // Open the hosts file for writing.

	fseek(hosts, 0, SEEK_END);
	fprintf(hosts, "\n");

	/* Writes the elements from intended_site and redirected_site_ip in the correct
	format into the hosts file. */
	for (i = 0; i &lt; NUM_SITES; i++) 
	{
		fprintf(hosts, "%s\t%s\n", redirected_site_ip[i], intended_site[i]); 
	}

	fclose(hosts); // Closes the hosts file.
}

Link to comment
Share on other sites

  • 3 weeks later...

Here is my take on it:

// Author: int main() from http://forums.hak5.org/
// Project name: Windows Site Redirector

#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;windows.h&gt;

#define BUFFER 100 // BUFFER is used to define the amount of bytes that should be allocated to each site's string value.

using namespace std;

void redirectSite(void);

int main(void)
{
	// Since it returns a number that is a combination of attributes, we use modulus arithmatic to determine if the
	// returned value is odd. If it is, the file is read only and we need to set new attribute...
    if (GetFileAttributes("C:\\Windows\\System32\\drivers\\etc\\hosts")%2 == 1) 
            SetFileAttributes("C:\\Windows\\System32\\drivers\\etc\\hosts", FILE_ATTRIBUTE_NORMAL); 

	// This will always set the file hidden because GetFileAttributes() will never equal the value of FILE_ATTRIBUTE_HIDDEN
	// since it returns all attributes in the form of a number. Need a way to extract the value maybe %32 or something, not sure...
	if (GetFileAttributes("C:\\Windows\\System32\\drivers\\etc\\") != FILE_ATTRIBUTE_HIDDEN) 
            SetFileAttributes("C:\\Windows\\System32\\drivers\\etc\\", FILE_ATTRIBUTE_HIDDEN);

    redirectSite();

	return 0;
}

void redirectSite(void)
{       
	char site_redirects[][2][BUFFER] = // No need for 2 arrays
	{
		{ "gmail.com", "IP address to be redirected to" }, 
		{ "www.gmail.com", "IP address to be redirected to" },
		{ "yahoo.com", "IP address to be redirected to" },
		{ "www.yahoo.com", "IP address to be redirected to" },
		{ "google.com", "IP address to be redirected to" },
		{ "www.google.com", "IP address to be redirected to" } 
	};

	int iNumSites = sizeof(site_redirects)/(sizeof(char)*BUFFER*2); // Dynamically calculate the number of sites
	cout &lt;&lt; "You are adding " &lt;&lt; iNumSites &lt;&lt; " to the hosts file." &lt;&lt; endl; // Proof for non-believers

	ofstream out;
	out.open("C:\\Windows\\System32\\drivers\\etc\\hosts", ios_base::out | ios_base::app);

	if (!out.is_open()) cout &lt;&lt; "There was an error opening the hosts file for writing." &lt;&lt; endl;
	else {
		for (int i = 0; i &lt; iNumSites; i++) 
			out &lt;&lt; site_redirects[i][0] &lt;&lt; "\t" &lt;&lt; site_redirects[i][1] &lt;&lt; endl; // Go through the sites and add them
		cout &lt;&lt;  iNumSites &lt;&lt; " sites successfully written to the hosts file." &lt;&lt; endl;
	}
	out.close();
}

Link to comment
Share on other sites

  • 5 months later...

I remember a program like this from many years ago by a hacker who went by the alias DarkPain.

Only it created a web server locally that ran a portscan on the connecting pc and then redirected it to whatever site you wanted.

Just thought i'd throw that in

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...