EvilZone
Programming and Scripting => C - C++ => : The Alchemist April 12, 2014, 03:37:04 PM
-
I'm trying to make a website blocker that runs with administrator privileges.
Here is my C code : http://www.scriptings.tk/paste/5348fab7a1ee5
Or view it here :
#include<stdio.h>
#include<conio.h>
void main()
{
FILE* handler;
int i;
char* str = "127.0.0.1 ";
char site[25];
handler = fopen("C:\\\\Windows\\System32\\drivers\\etc\\hosts","a");
do
{
printf("Enter site : ");
fflush(stdin);
gets(site);
fputs(str, handler);
fputs(site, handler);
fputs("\n", handler);
printf("Enter 1 to continue : ");
scanf("%d",&i);
} while(i == 1);
fclose(handler);
getch();
}
Here is my manifest file : http://www.scriptings.tk/paste/5348fb172bbd8
OR view it here :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<ms_asmv2:trustInfo xmlns:ms_asmv2="urn:schemas-microsoft-
com:asm.v2">
<ms_asmv2:security>
<ms_asmv2:requestedPrivileges>
<ms_asmv2:requestedExecutionLevel level="requiredAdministrator">
</ms_asmv2:requestedExecutionLevel>
</ms_asmv2:requestedPrivileges>
</ms_asmv2:security>
</ms_asmv2:trustInfo>
</assembly>
Im using Microsoft Visual Studio 2010. Its getting Built completely and asking for administrator privileges while executing. But when I run the exe, an error comes up :
(http://i7.minus.com/iGinoXBJhEXok.png)
How do I solve this problem? Is this something thats happening due to Windows 7 firewall or something?
Please guys, help me out with this.
-
Don't use that shitty MS visual studio crap. I recommend CodeBlocks or C++Builder.
-
Don't use that shitty MS visual studio crap. I recommend CodeBlocks or C++Builder.
Ok, I'll use Codeblocks if I can add manifest using that. What will be the solution to the problem?
The stream you try to manipulate is obviously null, check if the fopen succeeded (line 9)
Yeah, fopen() isn't succeeding I think.
-
I'm trying to make a website blocker that runs with administrator privileges.
Here is my C code : http://www.scriptings.tk/paste/5348fab7a1ee5
Or view it here :
#include<stdio.h>
#include<conio.h>
void main()
{
char* str = "127.0.0.1 ";
fputs(str, handler);
}
The memory for str is allocated statically off the stack. It's read-only. You can't re-assign a value to it. Instead, declare it as char str[size] = "whatever";
and it should run.
Also, always use fgets instead of gets. gets does no boundary check.
-
The memory for str is allocated statically off the stack. It's read-only. You can't re-assign a value to it. Instead, declare it as char str[size] = "whatever";
and it should run.
Also, always use fgets instead of gets. gets does no boundary check.
The value for str is always "127.0.0.1 " I'm not changing it in the code, so I'm using char *str="something" so, whether I do char *str="something" or char str[100] = "something" it wont make a difference.
As far as I know, fgets() is for input from file right(I may be wrong)? I'm using gets() as I'm taking input from keyboard.
-
The value for str is always "127.0.0.1 " I'm not changing it in the code, so I'm using char *str="something" so, whether I do char *str="something" or char str[100] = "something" it wont make a difference.
As far as I know, fgets() is for input from file right(I may be wrong)? I'm using gets() as I'm taking input from keyboard.
If you're not going to change *str then why 'd you do this:
fputs(str, handler);
When you do
*str = "something";
"something" is put into the string table of the executable which is read-only. Trying to write something to a r/o region is going to fail. Allocating it the other way around reserves memory from the stack which is r/w.
Use fgets with the default input stream i.e. stdin.
fgets(site, size, stdin);
Hope this helps.
-
Alright, instead of fputs(str, handler), I'll use fputs("127.0.0.1 ", handler)
That should be even better.
And I'll use fgets too now.
-
Batch FTW with things like this
@echo off
title=EditHosts
setlocal EnableExtensions
set "ExitCode=0"
set "VBS=%Temp%\getadmin.vbs"
:: Check for permissions
>nul 2>&1 "%SystemRoot%\System32\cacls.exe" "%SystemRoot%\System32\config\system"
if "%ErrorLevel%"=="0" goto gotAdmin
if /i "%~1"=="Self" goto ElevateFail
goto UACPrompt
:ElevateFail
set "ExitCode=1"
echo Error: Administrator privileges are required.
pause>nul
goto End
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%VBS%"
echo UAC.ShellExecute "%~s0", "Self %*", "", "runas", 1 >> "%VBS%"
echo Requesting administrative privileges...
"%VBS%"
goto End
:gotAdmin
shift
if exist "%VBS%" del "%VBS%"
pushd "%CD%"
cd /d "%~dp0"
attrib -r -s "%SystemRoot%\System32\drivers\etc\hosts"
cls
echo Please enter the site you would like to block
set /p site2block=
cls
echo 127.0.0.1 %site2block% >> "%SystemRoot%\System32\drivers\etc\hosts"
attrib +r +s "%SystemRoot%\System32\drivers\etc\hosts"
cls
echo %site2block% has now been blocked, Windows now needs to clear your
echo DNS cache
pause
popd
goto flushdns
:flushdns
ipconfig /flushdns
:End
endlocal & exit /b %ExitCode%
NOTE: I haven't tested the batch file, should work in theory, I had written a batch file that does edit the hosts file before, but I can't find it. This batch file also ensures that you have admin privileges before trying to edit the hosts file
:)
As for your program, you might be having issues with administrator rights, as well as the read only attribute on the hosts file.
-
Suit yourself with what you think is necessary or even better. :)
I sense a hint of troll.
-
Don't use that shitty MS visual studio crap. I recommend CodeBlocks or C++Builder.
Nothing wrong with MS Visual Studio, especially for Windows development. It's the best IDE out there if developing Windows programs.
Btw... Surprised *nobody* has mentioned that fflush(stdin) has undefined behavior? :S Never use fflush(stdin)...
Read: http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1052863818&id=1043284351 (http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1052863818&id=1043284351)
The problem is that I'll bet you that you didn't even know you are probably compiling C code with a C++ compiler. Because with VS it takes a bit of re-work to actually trigger the C compiler to compile your project, rather than the C++ compiler. Try declaring a type (non-void) pointer, and assigning it to the return of a call to malloc() without casting and see what happens.
-
Nothing wrong with MS Visual Studio, especially for Windows development. It's the best IDE out there if developing Windows programs.
Btw... Surprised *nobody* has mentioned that fflush(stdin) has undefined behavior? :S Never use fflush(stdin)...
Read: http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1052863818&id=1043284351 (http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1052863818&id=1043284351)
The problem is that I'll bet you that you didn't even know you are probably compiling C code with a C++ compiler. Because with VS it takes a bit of re-work to actually trigger the C compiler to compile your project, rather than the C++ compiler. Try declaring a type (non-void) pointer, and assigning it to the return of a call to malloc() without casting and see what happens.
I'm already past this project... Still, I'll try doing it.