Click here to Skip to main content
Licence 
First Posted 21 Jun 2004
Views 77,886
Bookmarked 46 times

A not so simple firewall.

By | 21 Jun 2004 | Article
A not so simple firewall if I can call it so. This application will ask you if you want a certain program to start.

Introduction

This new article is an update of the ex Process Monitor.

The new additions include a tray icon. In this new application, you don't have to write down what applications shouldn't start.

You'll just be asked if you want a certain application to run.

The application still uses Windows hooks but this time it is a little different. When an application is detected that wants to start, it is memorized in the Windows registry so you won't be asked again by the callback function if you want it to start or not. If you give it the approval to start, the application will be set as default to start.

This means it will start every time it wants. But if you tell the program that it should stop it, the application will never start until the hooks are stopped.

DLL_EXPORT void BagaHooku(void)
{
    if (!bHooked)
    {
        CBT = SetWindowsHookEx(WH_CBT, (HOOKPROC)CBTProc, hInst, 
                               (DWORD)NULL);
        bHooked = TRUE; 
    }
}

Just to set the hook.

Now the callback function:

LRESULT CALLBACK CBTProc(int nCode,WPARAM wParam,LPARAM lParam)
{
    if ((nCode==HCBT_ACTIVATE)||(nCode==HCBT_SYSCOMMAND)||(nCode==HCBT_QS) 
        ||(nCode==HCBT_CREATEWND))
    {
        HANDLE hProc;
        HMODULE hMods[1024];
        DWORD n;
        DWORD dwProcessId;
        DWORD lpExitCode;
        DWORD dwSize, dwType, dwDisp;
        HKEY Regentry;
        char *host1;
        char host[1024];
        char rezerva[1024];

        GetWindowThreadProcessId((HWND)wParam, &dwProcessId);
        hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)dwProcessId); 

        if (EnumProcessModules(hProc, hMods, sizeof(hMods), &n))
        {
            if (n>0)
                GetModuleFileNameEx(hProc, hMods[0], 
                         szModName, sizeof(szModName));
        }

        GetExitCodeProcess(hProc,&lpExitCode); //gets the exit code

        if (!(host1 = strrchr(szModName,'\\')))
            strcpy(host,szModName);
        else
            strcpy(host,host1+1);

        //get the program name
        RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Gapula\\PEND", 0, 
                     KEY_QUERY_VALUE, &Regentry);
        RegQueryValueEx(Regentry,host , NULL, &dwType, 
                        (unsigned char*)&rezerva, &dwSize);

        if (RegQueryValueEx(Regentry,host , NULL, &dwType, 
                        (unsigned char*)&rezerva, &dwSize)!=ERROR_SUCCESS)

        //check if the application was filtred once
        {
            RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Gapula\\OK", 0, 
                         KEY_QUERY_VALUE, &Regentry);
            RegQueryValueEx(Regentry,host , NULL, &dwType, 
                         (unsigned char*)&rezerva, &dwSize);

            if (RegQueryValueEx(Regentry,host , NULL, &dwType, 
                         (unsigned char*)&rezerva, &dwSize)!=ERROR_SUCCESS)
            //if it is not in the OK folder 

            {
                RegCloseKey(Regentry);
                RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Gapula\\RESTR", 
                            0, KEY_QUERY_VALUE|KEY_ALL_ACCESS, &Regentry);
                RegQueryValueEx(Regentry,host , NULL, &dwType, 
                            (unsigned char*)&rezerva, &dwSize);

                if (RegQueryValueEx(Regentry,host , NULL, &dwType, 
                            (unsigned char*)&rezerva, &dwSize)!=ERROR_SUCCESS)
                //if it is not in the restricted folder as well

                {
                    RegCreateKeyEx(HKEY_LOCAL_MACHINE, 
                                "SOFTWARE\\Gapula\\PEND", 0, "", 
                                REG_OPTION_NON_VOLATILE, KEY_WRITE, 
                                NULL, &Regentry, &dwDisp);
                    RegSetValueEx(Regentry, host, 0, REG_SZ,
                                (unsigned char *)szModName, 
                                strlen(szModName)+1);
                    RegCloseKey(Regentry);

                    //we put it in the pending folder so the callback 
                    //function will never ask about this again

                    strcat(szModName," is trying to start, do you allow that?
                                     \n Please recall that if you say yes 
                                     this action will be happening every time
                                     this program starts\nThis goes for NO as
                                     well so be careful what you wish for");

                    if (MessageBox(NULL,szModName,"Gabby",
                            MB_ICONQUESTION|MB_SYSTEMMODAL|MB_APPLMODAL| 
                            MB_TASKMODAL|MB_SETFOREGROUND|MB_TOPMOST|
                            MB_YESNO)==IDNO)

                    //if IDNO so if you don't want it to start we put it in
                    //the restricted folder
                    {
                        RegCreateKeyEx( HKEY_LOCAL_MACHINE, 
                                    "SOFTWARE\\Gapula\\RESTR", 0, "", 
                                    REG_OPTION_NON_VOLATILE,KEY_WRITE, 
                                    NULL, &Regentry, &dwDisp);
                        RegSetValueEx(Regentry, host, 0, REG_SZ,
                                    (unsigned char *)szModName, 
                                    strlen(szModName)+1);
                        RegCloseKey(Regentry);

                        TerminateProcess(hProc, (UINT)lpExitCode);

                    }
                    else
                    //else if you said IDYES we put it in the OK folder
                    {
                        RegCreateKeyEx(HKEY_LOCAL_MACHINE, 
                                    "SOFTWARE\\Gapula\\OK", 0, "", 
                                    REG_OPTION_NON_VOLATILE,KEY_WRITE, NULL, 
                                    &Regentry, &dwDisp);
                        RegSetValueEx(Regentry, host, 0, REG_SZ,
                                    (unsigned char *)szModName, 
                                    strlen(szModName)+1);
                        RegCloseKey(Regentry);
                        return 0;
                    }
                }
                //else if the application is in the restricted folder we 
                //terminate the application
                else
                    TerminateProcess(hProc, (UINT)lpExitCode);
            }
            else
            //else if it is in the OK folder we return 0; which means success
            {
                return 0;
            }
        }
        //else if it is in the pending folder it means it was already 
        //filtered so we have to check if it in the RESTR folder or in 
        //the OK folder 
        else
        {
            RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Gapula\\RESTR", 0, 
                         KEY_QUERY_VALUE|KEY_ALL_ACCESS, &Regentry);
            RegQueryValueEx(Regentry,host , NULL, &dwType, (unsigned 
                         char*)&rezerva, &dwSize);

            if(RegQueryValueEx(Regentry,host , NULL, &dwType, 
                         (unsigned char*)&rezerva, &dwSize)!=ERROR_SUCCESS)
            //if not in the restricted return 0; success 
                return 0;
            else
            //else terminate it
                TerminateProcess(hProc, (UINT)lpExitCode);
        }
    }

    //all we have to do now is call the next hook;
    return CallNextHookEx(CBT,nCode,wParam,lParam);
}

The firewall is very powerful because it filters every application. The program that loads it is very simple because all it has to do is to load it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

gamitech

Software Developer

Romania Romania

Member

I love VC++

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow can we tell what application/program is trying to access network? Pinmemberwwa20216:22 6 Jan '10  
Generalgood Pinmembercute_friend70776:47 19 May '09  
GeneralContact you Pinmembermamad1234568:20 23 Jul '07  
GeneralRe: Contact you Pinmembereuacela11:38 31 Jul '07  
QuestionWhat Happend exactly when ? PinmemberMandorle0:02 16 Apr '07  
QuestionProblem in compiling the code PinmemberAsshish0:08 21 Mar '07  
AnswerRe: Problem in compiling the code Pinmembereuacela6:28 21 Mar '07  
Generalprevious article ! Pinmemberfarshad.f2:50 10 Jul '06  
QuestionWhat Is This? HIPS? Pinmembereaster_200716:08 20 Feb '06  
GeneralVery good PinmemberTorres O.10:09 25 Jan '06  
GeneralRe: Very good Pinmembereuacela12:12 25 Jan '06  
GeneralRe: Very good PinmemberTorres O.16:45 25 Jan '06  
GeneralRe: Very good Pinmembereuacela17:02 25 Jan '06  
GeneralRe: Very good PinmemberTorres O.15:47 2 Feb '06  
Generalplz help Pinmember3loka8:10 22 Jun '05  
GeneralRe: plz help PinmemberThatsAlok23:19 17 Nov '05  
GeneralNice Article, but don't trust this App ... PinsussAnonymous6:34 11 Mar '05  
GeneralNice application... PinmemberVlad Stanciu7:15 11 Dec '04  
GeneralRe: Nice application... Pinmembereuacela10:00 12 Dec '04  
GeneralJust a few problems, other than that, it's pretty cool. PinmemberDeath0:27 22 Aug '04  
GeneralExcellent Pinsusswjvii3:34 28 Jun '04  
GeneralRe: Excellent Pinmembereuacela7:09 28 Jun '04  
GeneralExcellent Pinmemberwjvii3:25 28 Jun '04  
Generalgreat job Pinmemberlalalalal11:05 23 Jun '04  
GeneralInteresting Article. PinmemberShail_Srivastav8:07 23 Jun '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 22 Jun 2004
Article Copyright 2004 by gamitech
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid