EvilZone

Programming and Scripting => C - C++ => : xC September 03, 2013, 01:28:28 AM

: Windows Firewall Enable/Disable
: xC September 03, 2013, 01:28:28 AM
Old code that I ported from Delphi in like 2009... not much use for it anymore.

Edit: Could be used for any service really with simple modifications.

:
#include <windows.h>
#include <stdio.h>


int WFDisable( bool bEnable );


int main()
{
   WFDisable( false ); //disable
   Sleep( 5000 );
   WFDisable( true ); //enable
   
   return( getchar( ) );
}


int WFDisable( bool bEnable )
{
   OSVERSIONINFO osvi;
   SERVICE_STATUS sStatus;
   BOOL bControl;
   LPCTSTR cService;
   
   osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
   if( GetVersionExA( &osvi ) != 0 ) {
      if( osvi.dwMajorVersion > 5 ) {
         cService = "MpsSvc";
      } else {
         cService = "SharedAccess";
      }
   } else {
      return( 1 );
   }
   SC_HANDLE hManager = OpenSCManager( NULL, NULL, 0xF003F );
   if( hManager == NULL ) {
      return( 2 );
   }
   SC_HANDLE hService = OpenService( hManager, cService, 0xF01FF );
   if( hService == NULL ) {
      return( 3 );
   }
   if( bEnable == true ) {
      bControl = StartService( hService, 0, NULL ); //start
   } else {
      bControl = ControlService( hService, 0x00000001, &sStatus ); //stop
   }
   if( bControl == 0 ) {
      return( 4 );
   }
   CloseServiceHandle( hManager );
   CloseServiceHandle( hService );
   
   return( 0 );
}
: Re: Windows Firewall Enable/Disable
: Kulverstukas September 03, 2013, 06:26:40 AM
I suppose this only works for winxp as the code is old and win vista and above have protections about it.
+cookie though.
: Re: Windows Firewall Enable/Disable
: kenjoe41 September 03, 2013, 11:55:31 AM
Yup, this would only work on XP but also win7/vista ain't that far off.
Alittle #include <netfw,h> header that ships with the win SDK, also #include <comdef.h> for error handling and using the COM to access the interfaces maybe using (ole32.lib) then i think you will be able to manipulate the Advanced security API, then alittle more code and win7 firewall is down or up or whatever.