View previous topic :: View next topic |
Author |
Message |
UnLmtD Grandmaster Cheater Reputation: 0
Joined: 13 Mar 2007 Posts: 894 Location: Canada
|
Posted: Sun Apr 15, 2007 4:01 pm Post subject: Help with some functions |
|
|
Hey guys,
Im not sure if its the right way or if I made a mistake somewhere but Im using this code Code: | char system[MAX_PATH];
GetSystemDirectory(system,sizeof(system));
strcat(system, "\\drivers\\etc");
printf( "[!] Checking for hosts file in %s\n", system );
strcat(system, "\\drivers\\etc\\hosts");
SetFileAttributes(system,FILE_ATTRIBUTE_NORMAL); |
As you see Im trying to get the "hosts" location. And SetFileAttribute is not working =( Can someone explain to me whats wrong.
And is there a way to check is "hosts" exist in the directory?
_________________
|
|
Back to top |
|
|
appalsap Moderator Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Sun Apr 15, 2007 5:15 pm Post subject: |
|
|
you've concatenated the string "c:\windows\system32\drivers\etc\drivers\etc\hosts"
Here is the corrected code.
Code: |
#include <windows.h>
#include <stdio.h>
BOOL SetHOSTS()
{
char HOSTpath[MAX_PATH];
memset(&HOSTpath, 0, sizeof(HOSTpath));
GetSystemDirectory(HOSTpath, sizeof(HOSTpath));
lstrcat(HOSTpath, "\\drivers\\etc\\HOSTS");
return SetFileAttributes(HOSTpath, FILE_ATTRIBUTE_NORMAL);
}
int main(int argc, char * argv[])
{
if (SetHOSTS())
puts("Success!");
else
puts("Failure!");
getch();
return 0;
}
|
|
|
Back to top |
|
|
UnLmtD Grandmaster Cheater Reputation: 0
Joined: 13 Mar 2007 Posts: 894 Location: Canada
|
Posted: Sun Apr 15, 2007 5:20 pm Post subject: |
|
|
Thanks again appalsap, and I believe that Code: | int main(int argc, char * argv[])
{
if (SetHOSTS())
puts("Success!");
else
puts("Failure!");
getch();
return 0;
} |
Checks is the file exists?
_________________
|
|
Back to top |
|
|
appalsap Moderator Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Sun Apr 15, 2007 5:22 pm Post subject: |
|
|
SetFileAttributes returns 0 (FALSE) on error so it tells you whether the function worked or not
|
|
Back to top |
|
|
UnLmtD Grandmaster Cheater Reputation: 0
Joined: 13 Mar 2007 Posts: 894 Location: Canada
|
Posted: Sun Apr 15, 2007 5:25 pm Post subject: |
|
|
zomgiownyou wrote: | Thanks again appalsap, and I believe that Code: | int main(int argc, char * argv[])
{
if (SetHOSTS())
puts("Success!");
else
puts("Failure!");
getch();
return 0;
} |
Checks is the file exists? |
*EDIT* O yeah 1 more thing so now if I want to add a line to the file will this work? Code: | ofstream tohost ( HOSTpath , ios::app );
tohost<<"127.0.0.1 testhere"; |
O SHT sorry I messed up the quoting+editing
_________________
|
|
Back to top |
|
|
appalsap Moderator Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Sun Apr 15, 2007 5:29 pm Post subject: |
|
|
Code: |
HANDLE hFile; char * whatever = "127.0.0.1 testhere";
DWORD lol;
hFile = CreateFile(HOSTpath, FILE_WRITE_DATA, 0, NULL,\
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
WriteFile(hFile, whatever, lstrlen(whatever), &lol, NULL);
CloseHandle(hFile);
|
|
|
Back to top |
|
|
UnLmtD Grandmaster Cheater Reputation: 0
Joined: 13 Mar 2007 Posts: 894 Location: Canada
|
Posted: Sun Apr 15, 2007 5:56 pm Post subject: |
|
|
Well as expected everything worked expect this Code: | HANDLE hFile; char * whatever = "127.0.0.1 testhere";
DWORD lol;
hFile = CreateFile(HOSTpath, FILE_WRITE_DATA, 0, NULL,\
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
WriteFile(hFile, whatever, lstrlen(whatever), &lol, NULL);
CloseHandle(hFile); |
For some reason it won't write to the file
_________________
|
|
Back to top |
|
|
appalsap Moderator Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Sun Apr 15, 2007 6:04 pm Post subject: |
|
|
Actually,
It works fine
You just didn't take the time
To put it in properly
Code: |
#include <windows.h>
#include <stdio.h>
void Write(char * who);
BOOL SetHOSTS()
{
char HOSTpath[MAX_PATH]; BOOL set;
memset(&HOSTpath, 0, sizeof(HOSTpath));
GetSystemDirectory(HOSTpath, sizeof(HOSTpath));
lstrcat(HOSTpath, "\\drivers\\etc\\HOSTS");
set = SetFileAttributes(HOSTpath, FILE_ATTRIBUTE_NORMAL);
Write(HOSTpath);
return set;
}
int main(int argc, char * argv[])
{
if (SetHOSTS())
puts("Success!");
else
puts("Failure!");
getch();
return 0;
}
void Write(char * who)
{
HANDLE hFile; char * whatever = "127.0.0.1 testhere";
DWORD lol;
hFile = CreateFile(who, FILE_WRITE_DATA, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
WriteFile(hFile, whatever, lstrlen(whatever), &lol, NULL);
CloseHandle(hFile);
}
|
|
|
Back to top |
|
|
stealthy17 Expert Cheater Reputation: 0
Joined: 10 Apr 2007 Posts: 144 Location: The Netherlands
|
Posted: Sun Apr 15, 2007 6:47 pm Post subject: |
|
|
|
|
Back to top |
|
|
UnLmtD Grandmaster Cheater Reputation: 0
Joined: 13 Mar 2007 Posts: 894 Location: Canada
|
Posted: Sun Apr 15, 2007 7:32 pm Post subject: |
|
|
Success! I successfully recreated dHTTPd till the part that acts like a web server
_________________
|
|
Back to top |
|
|
|