This firewall is the simplest of it's kind. It is a packet filtering firewall that monitors SYN packets. When a SYN packet is sent to an unauthorized TCP port a RST packet is sent to same port, immediately tearing down the connection.
The rules apply to both local and remote connections. }
function GetInAddr: TInAddr; var Host: array[0..128] of char; HostEnt: PHostEnt; begin GetHostName(@Host, 128); HostEnt := GetHostByName(@Host); Result := PInAddr(HostEnt^.h_addr_list^)^ end;
begin if WSAStartup(WINSOCK_VERSION, WSAData) = 0 then begin rSocket := Socket(PF_INET, SOCK_RAW, 0); sSocket := Socket(PF_INET, SOCK_RAW, 0); if ((rSocket <> INVALID_SOCKET) and (sSocket <> INVALID_SOCKET)) then begin SetSockOpt(sSocket, 0, 2, @Option, SizeOf(Option)); SockAddrIn.sin_family := AF_INET; SockAddrIn.sin_addr := GetInAddr; SockAddrIn.sin_port := htons(0); bind(rSocket, @SockAddrIn, SizeOf(SockAddrIn)); WSAIoctl(rSocket, IOC_RCVALL, @Control, SizeOf(Control), nil, 0, @BytesReceived, nil, nil); while rSocket <> INVALID_SOCKET do begin BytesReceived := recv(rSocket, Data, SizeOf(Data), 0); if BytesReceived > 0 then begin if Data.d_ip.ip_protocol = 6 then begin if (Data.d_tcp.th_flags and 2) <> 0 then begin for ArgLoop := 0 to 31 do begin if Ports[ArgLoop] = htons(Data.d_tcp.th_dport) then begin RSTHeader(Data.d_ip.ip_saddr, Data.d_tcp.th_sport, Data.d_ip.ip_daddr, Data.d_tcp.th_dport, TPACKETARRAY(RST), SockAddrIn, BytesSent, ntohl(Data.d_tcp.th_seq) + 1); SendTo(sSocket, RST, BytesSent, 0, SockAddrIn, sizeof(SockAddrIn)); Break; end; end; end; end; end else begin Break; end; end; end; end; WSACleanup; end;
begin WriteLn('AFX Lite TCP Firewall by Aphex'); WriteLn('http://www.iamaphex.cjb.net'); WriteLn('unremote@knology.net'); if ParamStr(1) = '' then begin WriteLn(''); WriteLn('Usage: afxfw.exe <port> <port> <port>...'); Halt(0); end; for ArgLoop := 1 to 32 do begin if ParamStr(ArgLoop) <> '' then begin Ports[ArgLoop - 1] := StrToInt(ParamStr(ArgLoop)); end; end; Main; end.