Click here to Skip to main content
15,885,757 members
Articles / Mobile Apps
Article

A not so simple firewall.

Rate me:
Please Sign up or sign in to vote.
3.48/5 (34 votes)
21 Jun 2004 119.7K   1.8K   48   43
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:

C++
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


Written By
Software Developer
Romania Romania
I love VC++

Comments and Discussions

 
QuestionHow can we tell what application/program is trying to access network? Pin
wwa2026-Jan-10 16:22
wwa2026-Jan-10 16:22 
Generalgood Pin
cute_friend707719-May-09 6:47
cute_friend707719-May-09 6:47 
GeneralContact you Pin
mamad12345623-Jul-07 8:20
mamad12345623-Jul-07 8:20 
GeneralRe: Contact you Pin
gamitech31-Jul-07 11:38
gamitech31-Jul-07 11:38 
QuestionWhat Happend exactly when ? Pin
Mandorle16-Apr-07 0:02
Mandorle16-Apr-07 0:02 
QuestionProblem in compiling the code Pin
Asshish21-Mar-07 0:08
Asshish21-Mar-07 0:08 
AnswerRe: Problem in compiling the code Pin
gamitech21-Mar-07 6:28
gamitech21-Mar-07 6:28 
Generalprevious article ! Pin
farshad.f10-Jul-06 2:50
farshad.f10-Jul-06 2:50 
QuestionWhat Is This? HIPS? Pin
easter_200720-Feb-06 16:08
easter_200720-Feb-06 16:08 
GeneralVery good Pin
Torres O.25-Jan-06 10:09
Torres O.25-Jan-06 10:09 
GeneralRe: Very good Pin
gamitech25-Jan-06 12:12
gamitech25-Jan-06 12:12 
GeneralRe: Very good Pin
Torres O.25-Jan-06 16:45
Torres O.25-Jan-06 16:45 
GeneralRe: Very good Pin
gamitech25-Jan-06 17:02
gamitech25-Jan-06 17:02 
GeneralRe: Very good Pin
Torres O.2-Feb-06 15:47
Torres O.2-Feb-06 15:47 
Generalplz help Pin
3loka22-Jun-05 8:10
3loka22-Jun-05 8:10 
GeneralRe: plz help Pin
ThatsAlok17-Nov-05 23:19
ThatsAlok17-Nov-05 23:19 
GeneralNice Article, but don't trust this App ... Pin
Anonymous11-Mar-05 6:34
Anonymous11-Mar-05 6:34 
GeneralNice application... Pin
Stanciu Vlad11-Dec-04 7:15
Stanciu Vlad11-Dec-04 7:15 
GeneralRe: Nice application... Pin
gamitech12-Dec-04 10:00
gamitech12-Dec-04 10:00 
GeneralJust a few problems, other than that, it's pretty cool. Pin
Death22-Aug-04 0:27
Death22-Aug-04 0:27 
GeneralExcellent Pin
wjvii28-Jun-04 3:34
wjvii28-Jun-04 3:34 
GeneralRe: Excellent Pin
gamitech28-Jun-04 7:09
gamitech28-Jun-04 7:09 
GeneralExcellent Pin
wjvii28-Jun-04 3:25
wjvii28-Jun-04 3:25 
Generalgreat job Pin
lalalalal23-Jun-04 11:05
lalalalal23-Jun-04 11:05 
GeneralInteresting Article. Pin
Shail_Srivastav23-Jun-04 8:07
Shail_Srivastav23-Jun-04 8:07 

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

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