A proof-of-concept implementation of "path knocking" for CloudFlare Workers. This system automatically manages IP addresses in CloudFlare IP Lists based on path access patterns. It can be configured to whitelist or block IPs based on their access to specific paths.
Similar to "port knocking" in traditional firewalls, this system uses HTTP path access as a trigger mechanism. When a client accesses a predefined URL path, their IP address is automatically added to a configured CloudFlare IP List with a configurable expiration time. The system supports multiple lists, each with their own configuration for paths, expiration times, and actions (whitelist or block).
This can be useful for:
- Creating temporary access to protected resources
- Ensuring legitimate users bypass bot mitigation and rate limiting rules
- Implementing emergency access mechanisms
- Building dynamic IP management systems
- Creating "secret" access paths for authorized users
- Detecting and blocking potential attackers via honeypot paths
- A client accesses a configured path (e.g.,
/prefix/somethingor/auth_check) - The Worker detects this access and captures the client's IP address
- Based on the matched path configuration, the IP is added to the appropriate CloudFlare IP List with the configured expiration time
- The original request continues processing normally
- CloudFlare account with Workers enabled
- Access to create and manage CloudFlare IP Lists
- API token with appropriate permissions
Edit the configuration objects in the cloudflare_path_knock.js file:
// Cloudflare API Configuration
const CLOUDFLARE_CONFIG = {
accountId: 'YOUR_ACCOUNT_ID',
zoneId: 'YOUR_ZONE_ID',
apiToken: 'YOUR_API_TOKEN', // Token needs IP Access Rules edit permission
};
// List Configurations
const LISTS = [
{
name: 'whitelist',
listId: 'YOUR_WHITELIST_ID',
mode: 'whitelist',
notes: 'Added via path knocking',
expirationHours: 24,
triggerPaths: {
prefixes: ['/prefix/'],
exact: ['/auth_check']
}
},
{
name: 'honeypot',
listId: 'YOUR_HONEYPOT_LIST_ID',
mode: 'block',
notes: 'Added via honeypot detection',
expirationHours: 168,
triggerPaths: {
prefixes: ['/admin/debug/', '/wp-admin/'],
exact: ['/config.php', '/.env', '/phpinfo.php']
}
}
];Each list configuration supports:
name: Identifier for the listlistId: CloudFlare IP List IDmode: Action to take ('whitelist' or 'block')notes: Description added to list entriesexpirationHours: How long IPs remain in the listtriggerPaths: Paths that trigger IP additionprefixes: Array of path prefixes that trigger the listexact: Array of exact paths that trigger the list
Before deploying the worker, you'll need to create the IP lists in Cloudflare. IP lists are part of Cloudflare's custom lists feature and are defined at the account level. Here's how to set them up:
- Create the IP Lists
For each list in your configuration (whitelist, honeypot, etc.), create a new IP list using the Cloudflare API:
curl https://api.cloudflare.com/client/v4/accounts/{account_id}/rules/lists \
--header "X-Auth-Email: <EMAIL>" \
--header "X-Auth-Key: <API_KEY>" \
--header "Content-Type: application/json" \
--data '{
"name": "path_knock_whitelist",
"description": "IPs allowed via path knocking",
"kind": "ip"
}'Save the returned list_id - you'll need it for the worker configuration.
- Create Rules to Use the Lists
After creating your lists, you need to create rules that use them. For example, to use a whitelist:
curl https://api.cloudflare.com/client/v4/accounts/{account_id}/rulesets/{ruleset_id}/rules \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"action": "skip",
"action_parameters": {
"ruleset": "current"
},
"expression": "ip.src in $path_knock_whitelist",
"description": "Allow IPs from path knocking whitelist",
"enabled": true
}'For a honeypot block list:
curl https://api.cloudflare.com/client/v4/accounts/{account_id}/rulesets/{ruleset_id}/rules \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"action": "block",
"expression": "ip.src in $honeypot_list",
"description": "Block IPs that triggered honeypot paths",
"enabled": true
}'Note: Currently, IP lists only support IPv4 addresses, not IPv6.
- Create a new CloudFlare Worker
- Copy the contents of
cloudflare_path_knock.jsinto the Worker editor - Update the configuration values with your actual account information
- Create the necessary IP Lists in the CloudFlare dashboard
- Deploy the Worker to your CloudFlare zone
- Configure the appropriate routes for the Worker in the CloudFlare dashboard
This proof-of-concept should be enhanced with additional security measures before production use:
- Consider adding a secret key or token requirement to the trigger paths
- Implement rate limiting to prevent abuse
- Add logging and alerting for suspicious access patterns
- Combine with additional authentication mechanisms for critical systems
- Regularly rotate the paths used for triggering
The system logs all actions to the console in JSON format, including:
- Timestamp
- Action type (based on list name and mode)
- IP address
- Triggered path
- List name
- Expiration hours
- The system depends on the CloudFlare API being available
- There may be a slight delay in adding IPs to the Lists
- API rate limits may apply when managing many IPs
- This is a proof-of-concept and should be enhanced for production use
MIT. Do as you please.
I'll accept PRs.