Author Topic: Windows Firewall Enable/Disable  (Read 1038 times)

0 Members and 1 Guest are viewing this topic.

xC

  • Guest
Windows Firewall Enable/Disable
« on: 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.

Code: [Select]
#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 );
}
« Last Edit: September 03, 2013, 01:48:37 am by xC »

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Windows Firewall Enable/Disable
« Reply #1 on: 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.

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
Re: Windows Firewall Enable/Disable
« Reply #2 on: 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.
« Last Edit: September 03, 2013, 12:02:26 pm by kenjoe41 »
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]