概述
tdifw是windows防火墙软件(TDI层驱动过滤),负责监控网络监听与连接、以及过滤信息。
源码在src目录, 程序在Bin目录,执行根目录下的批处理文件也可以,
具体步骤如下:
1. 运行install.bat
2. 根据你机器的配置情况,编辑%SystemRoot%system32driversetctdifw.conf配置文件
3. 重新启动计算机
主程序源码是win32的,就9个目标文件,不包含驱动部分,项目如图:
程序主要源码分析:
- int main(int argc, char **argv)
- {
- static SERVICE_TABLE_ENTRY dispatch_table[] = {
- {"tdifw", service_main},
- {NULL, NULL}
- };
- _LEAK_CHECK;
- //模拟参数
- argc = 3;
- argv[0]="tdifw";
- argv[1]="install";
- argv[2]="tdifw_drv.sys";
- if (argc >= 2)
- {
- const char *param = argv[1];
- if (strcmp(param, "install") == 0)
- {
- if (argc < 3)
- {
- fprintf(stderr, "Use: tdifw install <config>n");
- return -1;
- }
- //加载驱动服务
- install_service(argv[2]);
- }
- else if (strcmp(param, "remove") == 0)
- {
- //移除驱动服务
- remove_service();
- } else if (strcmp(param, "debug") == 0)
- {
- if (argc < 3)
- {
- fprintf(stderr, "Use: tdifw debug <config>n");
- return -1;
- }
- if (start(argv[2]))
- {
- printf("press enter to exit...n");
- getchar();
- printf("exiting...n");
- //停止 释放资源
- stop();
- }
- } else if (strcmp(param, "listen") == 0)
- { // tdifw specific
- //枚举监听
- enum_listen();
- } else if (strcmp(param, "conn") == 0)
- { // tdifw specific
- //枚举连接
- enum_connect();
- } else
- {
- fprintf(stderr, "Use: tdifw install|remove|debug|listen|connn");
- }
- }
- else
- {
- g_console = FALSE;
- // 连接程序主线程到服务控制管理程序
- if (!StartServiceCtrlDispatcher(dispatch_table))
- winerr("main: StartServiceCtrlDispatcher");
- }
- return 0;
- }
int main(int argc, char **argv)
{
static SERVICE_TABLE_ENTRY dispatch_table[] = {
{"tdifw", service_main},
{NULL, NULL}
};
_LEAK_CHECK;
//模拟参数
argc = 3;
argv[0]="tdifw";
argv[1]="install";
argv[2]="tdifw_drv.sys";
if (argc >= 2)
{
const char *param = argv[1];
if (strcmp(param, "install") == 0)
{
if (argc < 3)
{
fprintf(stderr, "Use: tdifw install <config>n");
return -1;
}
//加载驱动服务
install_service(argv[2]);
}
else if (strcmp(param, "remove") == 0)
{
//移除驱动服务
remove_service();
} else if (strcmp(param, "debug") == 0)
{
if (argc < 3)
{
fprintf(stderr, "Use: tdifw debug <config>n");
return -1;
}
if (start(argv[2]))
{
printf("press enter to exit...n");
getchar();
printf("exiting...n");
//停止 释放资源
stop();
}
} else if (strcmp(param, "listen") == 0)
{ // tdifw specific
//枚举监听
enum_listen();
} else if (strcmp(param, "conn") == 0)
{ // tdifw specific
//枚举连接
enum_connect();
} else
{
fprintf(stderr, "Use: tdifw install|remove|debug|listen|connn");
}
}
else
{
g_console = FALSE;
// 连接程序主线程到服务控制管理程序
if (!StartServiceCtrlDispatcher(dispatch_table))
winerr("main: StartServiceCtrlDispatcher");
}
return 0;
}
- //获得驱动文件所在路径 则开启 否则退出
- void install_service(const char *config)
- {
- SC_HANDLE schService;
- SC_HANDLE schSCManager;
- CHAR szPath[MAX_PATH];
- //从注册表中获得信息
- AddEventSource("tdifw");
- if (GetModuleFileName(NULL, szPath, sizeof(szPath)) == 0) {
- winerr("install_service: GetModuleFileName");
- return;
- }
- //建立了一个连接到服务控制管理器,并打开指定的数据库。
- schSCManager = OpenSCManager(
- NULL, // machine (NULL == local)
- NULL, // database (NULL == default)
- SC_MANAGER_ALL_ACCESS); // access required
- if (schSCManager != NULL) {
- //创建一个服务对象并且把它加入到服务管理数据库中
- schService = CreateService(
- schSCManager, // SCManager database
- "tdifw", // name of service
- "TDI-based open source personal firewall", // name to display
- SERVICE_ALL_ACCESS, // desired access
- SERVICE_WIN32_OWN_PROCESS, // service type
- SERVICE_AUTO_START, // start type
- SERVICE_ERROR_NORMAL, // error control type
- szPath, // service's binary
- NULL, // no load ordering group
- NULL, // no tag identifier
- NULL, // dependencies
- NULL, // LocalSystem account
- NULL); // no password
- if (schService != NULL) {
- printf("tdifw service has been installedn");
- if (!add_config_info(schService, config))
- fprintf(stderr, "Can't store config info! Service will use defaults.n");
- CloseServiceHandle(schService);
- } else
- winerr("install_service: CreateService");
- CloseServiceHandle(schSCManager);
- }
- else
- winerr("install_service: OpenSCManager");
- }
//获得驱动文件所在路径 则开启 否则退出
void install_service(const char *config)
{
SC_HANDLE schService;
SC_HANDLE schSCManager;
CHAR szPath[MAX_PATH];
//从注册表中获得信息
AddEventSource("tdifw");
if (GetModuleFileName(NULL, szPath, sizeof(szPath)) == 0) {
winerr("install_service: GetModuleFileName");
return;
}
//建立了一个连接到服务控制管理器,并打开指定的数据库。
schSCManager = OpenSCManager(
NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS); // access required
if (schSCManager != NULL) {
//创建一个服务对象并且把它加入到服务管理数据库中
schService = CreateService(
schSCManager, // SCManager database
"tdifw", // name of service
"TDI-based open source personal firewall", // name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_AUTO_START, // start type
SERVICE_ERROR_NORMAL, // error control type
szPath, // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // dependencies
NULL, // LocalSystem account
NULL); // no password
if (schService != NULL) {
printf("tdifw service has been installedn");
if (!add_config_info(schService, config))
fprintf(stderr, "Can't store config info! Service will use defaults.n");
CloseServiceHandle(schService);
} else
winerr("install_service: CreateService");
CloseServiceHandle(schSCManager);
}
else
winerr("install_service: OpenSCManager");
}
- //移除服务 关闭驱动
- void remove_service(void)
- {
- SC_HANDLE schService;
- SC_HANDLE schSCManager;
- schSCManager = OpenSCManager(
- NULL, // machine (NULL == local)
- NULL, // database (NULL == default)
- SC_MANAGER_ALL_ACCESS); // access required
- if (schSCManager != NULL) {
- schService = OpenService(schSCManager, "tdifw", SERVICE_ALL_ACCESS);
- if (schService != NULL) {
- // try to stop the service
- if (ControlService(schService, SERVICE_CONTROL_STOP, &ssStatus)) {
- printf("stopping...");
- Sleep(1000);
- while(QueryServiceStatus( schService, &ssStatus)) {
- if (ssStatus.dwCurrentState == SERVICE_STOP_PENDING) {
- printf(".");
- Sleep( 1000 );
- }
- else
- break;
- }
- printf("n");
- if (ssStatus.dwCurrentState == SERVICE_STOPPED)
- printf("stoppedn");
- else
- printf("failed to stopn");
- }
- // now remove the service
- if (DeleteService(schService))
- printf("service has been removedn");
- else
- winerr("install_service: DeleteService");
- CloseServiceHandle(schService);
- }
- else
- winerr("install_service: OpenService");
- CloseServiceHandle(schSCManager);
- }
- else
- winerr("install_service: OpenSCManager");
- }
//移除服务 关闭驱动
void remove_service(void)
{
SC_HANDLE schService;
SC_HANDLE schSCManager;
schSCManager = OpenSCManager(
NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS); // access required
if (schSCManager != NULL) {
schService = OpenService(schSCManager, "tdifw", SERVICE_ALL_ACCESS);
if (schService != NULL) {
// try to stop the service
if (ControlService(schService, SERVICE_CONTROL_STOP, &ssStatus)) {
printf("stopping...");
Sleep(1000);
while(QueryServiceStatus( schService, &ssStatus)) {
if (ssStatus.dwCurrentState == SERVICE_STOP_PENDING) {
printf(".");
Sleep( 1000 );
}
else
break;
}
printf("n");
if (ssStatus.dwCurrentState == SERVICE_STOPPED)
printf("stoppedn");
else
printf("failed to stopn");
}
// now remove the service
if (DeleteService(schService))
printf("service has been removedn");
else
winerr("install_service: DeleteService");
CloseServiceHandle(schService);
}
else
winerr("install_service: OpenService");
CloseServiceHandle(schSCManager);
}
else
winerr("install_service: OpenSCManager");
}
- // 从驱动程序中获得网络监听对象
- void enum_listen(void)
- {
- ULONG size;
- struct listen_nfo *ln = NULL;
- int i, n;
- // 从 psapi.dll 中获得链接EnumProcesses、EnumProcessModules、GetModuleFileNameExW函数地址
- link_psapi();
- /* connect with driver */
- g_device = CreateFile(g_nfo_device_name, GENERIC_READ | GENERIC_WRITE,
- FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
- if (g_device == INVALID_HANDLE_VALUE) {
- winerr(g_nfo_device_name);
- goto done;
- }
- /* get list of listening objects */
- size = sizeof(*ln) * 0x10000 * 3; // this size is good enough :-)
- ln = (struct listen_nfo *)malloc(size);
- if (ln == NULL) {
- perror("malloc");
- goto done;
- }
- //与驱动交流 枚举监听操作 获取监听信息
- if (!DeviceIoControl(g_device, IOCTL_CMD_ENUM_LISTEN, NULL, 0,
- ln, size, &size, NULL)) {
- winerr("DeviceIoControl");
- goto done;
- }
- n = size / sizeof(*ln);
- // sort this list!
- qsort(ln, n, sizeof(*ln), compare_ln);
- printf("IPPrototAddress:PorttProcess (pid)n");
- printf("-------t------------t---------------------------------------------n");
- //显示
- for (i = 0; i < n ; i++) {
- char *proto, pname[MAX_PATH];
- if (ln[i].ipproto == IPPROTO_TCP)
- proto = "TCP";
- else if (ln[i].ipproto == IPPROTO_UDP)
- proto = "UDP";
- else if (ln[i].ipproto == IPPROTO_IP)
- proto = "RawIP";
- else
- proto = "?";
- // resolve pid!
- if (!get_pname_by_pid(ln[i].pid, pname, sizeof(pname)))
- pname[0] = '