You can write a stateless rule in WFP to rewrite packet IP addresses by using the following steps:
Create a filter condition that matches the packets you want to modify, such as IP source and destination addresses, protocol, port, etc.
Create a callout driver that will receive the matching packets and modify their IP addresses. The callout driver must be written in a low-level language such as C or C++, and must implement the necessary APIs provided by WFP.
Register the callout driver with WFP, and associate it with the filter condition you created in step 1.
Add the filter to the WFP engine, which will begin processing packets according to your rule.
This is just an example, and you will need to tailor the code to your specific requirements. Writing a WFP callout driver requires a good understanding of the Windows network stack and the WFP API, so it may be challenging for a beginner.
User
Will the above code rewrite incoming packets source IP of fe12:3456::1 to 2001:1234:abcd::1?
Assistant
No, the code provided in the previous answer will not rewrite incoming packets' source IP of fe12:3456::1 to 2001:1234:abcd::1. You would need to modify the code in the calloutFunction to make the necessary changes to the IP addresses of the incoming packets.
Here's an example of what the modification could look like:
rust
void NTAPI calloutFunction(
_In_ const FWPS_INCOMING_VALUES* inFixedValues,
_In_ const FWPS_INCOMING_METADATA_VALUES* inMetaValues,
_Inout_opt_ void* layerData,
_In_opt_ const void* classifyContext,
_In_ const FWPS_FILTER* filter,
_In_ UINT64 flowContext,
_Inout_ FWPS_CLASSIFY_OUT* classifyOut
) {
// Check if the source IP is fe12:3456::1
if (inFixedValues->incomingValue[FWPS_FIELD_INBOUND_IPPACKET_V4_IP_SRC_ADDR].value.uint32 == 0xfe123456) {
// Rewrite the source IP to 2001:1234:abcd::1
classifyOut->actionType = FWP_ACTION_PERMIT;
classifyOut->rights &= ~FWPS_RIGHT_ACTION_WRITE;
classifyOut->rights |= FWPS_RIGHT_ACTION_WRITE_IPS;
classifyOut->ipAction.type = FWP_ACTION_SET_IPSEC_SECURITY_INFO;
classifyOut->ipAction.ipsecSecurityInfo = { 0 };
classifyOut->ipAction.ipsecSecurityInfo.remoteAddress = 0x20011234abcdull;
} else {
classifyOut->actionType = FWP_ACTION_CONTINUE;
}
}
This code checks if the source IP address of an incoming packet is fe12:3456::1 and if it is, it rewrites the source IP to 2001:1234:abcd::1. Note that this code is just an example and you may need to modify it based on your specific requirements.
User
how could I get a Windows machine to run this program every time it starts up?
Assistant
To run a program every time a Windows machine starts up, you can follow these steps:
Create a shortcut to the program you want to run.
Copy the shortcut to the Windows startup folder. To do this, press Win + R and type shell:startup in the Run dialog box. This will open the startup folder.
Right-click the startup folder and select New > Shortcut. In the Create Shortcut dialog box, type the location of the program you want to run, or browse to its location by clicking on the Browse button.
Click Next, then type a name for the shortcut and click Finish.
Restart your computer and the program should start automatically when Windows starts up.
Note: The location of the startup folder may vary depending on the version of Windows you are using, but the steps to create a shortcut and copy it to the startup folder should remain the same.
User
How would I rewrite outgoing packets with destination IP 2001:1234:abcd::1 to fe12:3456::1?
Assistant
Here's an example of how you could rewrite outgoing packets with destination IP 2001:1234:abcd::1 to fe12:3456::1 using the Windows Filtering Platform (WFP) in C++:
rust
void NTAPI calloutFunction(
_In_ const FWPS_INCOMING_VALUES* inFixedValues,
_In_ const FWPS_INCOMING_METADATA_VALUES* inMetaValues,
_Inout_opt_ void* layerData,
_In_opt_ const void* classifyContext,
_In_ const FWPS_FILTER* filter,
_In_ UINT64 flowContext,
_Inout_ FWPS_CLASSIFY_OUT* classifyOut
) {
// Check if the destination IP is 2001:1234:abcd::1
if (inFixedValues->incomingValue[FWPS_FIELD_OUTBOUND_IPPACKET_V6_IP_DEST_ADDR].value.uint32 == 0x20011234abcdull) {
// Rewrite the destination IP to fe12:3456::1
classifyOut->actionType = FWP_ACTION_PERMIT;
classifyOut->rights &= ~FWPS_RIGHT_ACTION_WRITE;
classifyOut->rights |= FWPS_RIGHT_ACTION_WRITE_IPS;
classifyOut->ipAction.type = FWP_ACTION_SET_IPSEC_SECURITY_INFO;
classifyOut->ipAction.ipsecSecurityInfo = { 0 };
classifyOut->ipAction.ipsecSecurityInfo.remoteAddress = 0xfe123456ull;
} else {
classifyOut->actionType = FWP_ACTION_CONTINUE;
}
}
This code checks if the destination IP address of an outgoing packet is 2001:1234:abcd::1 and if it is, it rewrites the destination IP to fe12:3456::1. Note that this code is just an example and you may need to modify it based on your specific requirements.
User
is the above code for IPv4 or IPv6 addresses?
Assistant
The above code is for IPv6 addresses.
User
what is FWPS_FIELD_OUTBOUND_IPPACKET_V6_IP_DEST_ADDR
Assistant
FWPS_FIELD_OUTBOUND_IPPACKET_V6_IP_DEST_ADDR is a constant in the Windows Filtering Platform (WFP) that specifies the destination IP address of an outgoing IPv6 packet.
It is used as an index into the FWPS_INCOMING_VALUES structure, which provides information about the incoming network data that is being processed by the filter. The FWPS_INCOMING_VALUES structure contains an array of FWPS_VALUE structures that describe the various fields of the network data, such as the source and destination IP addresses, the source and destination ports, the protocol, etc.
The FWPS_FIELD_OUTBOUND_IPPACKET_V6_IP_DEST_ADDR constant is used to access the value of the destination IP address of an outgoing IPv6 packet in the FWPS_INCOMING_VALUES structure.
User
and what is FWPS_FIELD_INBOUND_IPPACKET_V4_IP_SRC_ADDR?
Assistant
FWPS_FIELD_INBOUND_IPPACKET_V4_IP_SRC_ADDR is a constant in the Windows Filtering Platform (WFP) that specifies the source IP address of an incoming IPv4 packet.
It is used as an index into the FWPS_INCOMING_VALUES structure, which provides information about the incoming network data that is being processed by the filter. The FWPS_INCOMING_VALUES structure contains an array of FWPS_VALUE structures that describe the various fields of the network data, such as the source and destination IP addresses, the source and destination ports, the protocol, etc.
The FWPS_FIELD_INBOUND_IPPACKET_V4_IP_SRC_ADDR constant is used to access the value of the source IP address of an incoming IPv4 packet in the FWPS_INCOMING_VALUES structure.
User
can you rewrite the first piece of code in rust?
Assistant
Here's an example of how you could rewrite the first piece of code in Rust:
rust
use winapi::um::fwpmu::{FWP_ACTION_PERMIT, FWP_ACTION_SET_IPSEC_SECURITY_INFO};
use winapi::um::winnt::{FWPS_RIGHT_ACTION_WRITE, FWPS_RIGHT_ACTION_WRITE_IPS};
pub extern "system" fn callout_function(
in_fixed_values: &FWPS_INCOMING_VALUES,
in_meta_values: &FWPS_INCOMING_METADATA_VALUES,
layer_data: Option<&mut [u8]>,
classify_context: Option<&FWPS_CLASSIFY_DATA>,
filter: &FWPS_FILTER,
flow_context: UINT64,
classify_out: &mut FWPS_CLASSIFY_OUT
) {
// Check if the source IP is fe12:3456::1
if in_fixed_values.incoming_value[FWPS_FIELD_INBOUND_IPPACKET_V6_IP_SRC_ADDR].value.uint32 == 0xfe123456_u64 {
// Rewrite the source IP to 2001:1234:abcd::1
classify_out.action_type = FWP_ACTION_PERMIT;
classify_out.rights &= !FWPS_RIGHT_ACTION_WRITE;
classify_out.rights |= FWPS_RIGHT_ACTION_WRITE_IPS;
classify_out.ip_action.type_ = FWP_ACTION_SET_IPSEC_SECURITY_INFO;
classify_out.ip_action.ipsec_security_info = unsafe { std::mem::zeroed() };
classify_out.ip_action.ipsec_security_info.remote_address = 0x2001_1234_abcd_u64;
} else {
classify_out.action_type = FWP_ACTION_CONTINUE;
}
}
This code uses the same logic as the original code, but it is written in Rust and takes advantage of the type-safety and conciseness offered by the language. Note that this code assumes that the necessary bindings for the Windows Filtering Platform (WFP) have been generated and are available in the project.