How to block a port for specific time?

3

I want to block a port for a specific time duration, say 1 am to 3 am. I can add a rule to built in firewall but this blocks the port all the time. Is there any way to do this for only a specif period of time ?

Serious

Posted 2012-08-19T13:10:15.307

Reputation: 1 465

Answers

2

No. It is not doable unless you make your own VBS script to automate with Task Scheduler. For which you need to implement Windows Firewall API. If oyu make a script post it. It would be of immense help to others.

C2940680

Posted 2012-08-19T13:10:15.307

Reputation: 775

+1 for controlling with Task Manager suggestion. It doesn't need to be VBS script though, PowerShell scripting would work, but easiest may be to use a couple batch files and Windows' netsh command.

– Ƭᴇcʜιᴇ007 – 2012-08-19T15:48:15.760

1

Finally I found a solution. First add a rule to block desired application in the firewall. Then the rule can be enabled or disabled using the command:

netsh advfirewall firewall set rule name="MyRule" new enable=yes

This can be added to a script that periodically checks time and enables/disables MuRule accordingly. I could not find sleep command in batch script and I don't know PowerShell so I wrote a simple c++ program.

#include<ctime>
#include<windows.h>
using namespace std;

int main()
{//code for hiding console
    HWND window; 
    AllocConsole();
    window = FindWindowA("ConsoleWindowClass", NULL);
    ShowWindow(window,0);

    Sleep(60*1000);// 1 min delay
    time_t now;
    struct tm *current;
    now = time(0);
    while(1)
    {
    current = localtime(&now);
    if(current->tm_hour>=22||current->tm_hour<=6) // 10 pm to 6 am
        system ("netsh advfirewall firewall set rule name=\"MyRule\" new enable=yes");
    else
        system ("netsh advfirewall firewall set rule name=\"MyRule\" new enable=no");
    Sleep(10*60*1000);// 10 min delay
    }
    return 0;
}

Compile using gcc and run at logon using Task Scheduler with Admin Privilege.

Serious

Posted 2012-08-19T13:10:15.307

Reputation: 1 465

1I think there's some inconsistancy between 'MyRule' "MuRule" and "Murule" there. I'm not sure tho, I don't read C well enough – Journeyman Geek – 2012-10-02T11:38:26.907

Corrected. I had different name in original program. Made mistake while changing name. – Serious – 2012-10-02T11:47:02.323