// 获取当前时间和6小时前的UTC时间
const now = new Date();
const sixHoursAgo = new Date(now.getTime() - 6 * 60 * 60 * 1000); // 当前时间减去6小时
//const sixHoursAgo = new Date(now.getTime() - 12 * 60 * 60 * 1000); // 当前时间减去6小时
// 格式化函数
const formatUTCTime = (date) => {
const year = date.getUTCFullYear();
const month = String(date.getUTCMonth() + 1).padStart(2, '0');
const day = String(date.getUTCDate()).padStart(2, '0');
const hours = String(date.getUTCHours()).padStart(2, '0');
const minutes = String(date.getUTCMinutes()).padStart(2, '0');
const seconds = String(date.getUTCSeconds()).padStart(2, '0');
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}Z`;
};
// 生成时间范围
const datetime_leq = formatUTCTime(now); // 当前时间(作为结束时间)
const datetime_geq = formatUTCTime(sixHoursAgo); // 6小时前(作为开始时间)
// 遍历所有条目并添加字段
for (const item of $input.all()) {
item.json.datetime_geq = datetime_geq;
item.json.datetime_leq = datetime_leq;
}
return $input.all();
https://api.cloudflare.com/client/v4/graphql
{"operationName":"GetSecuritySampledLogs","variables":{"zoneTag":"xxxx9e4c7b4d06780fa34c744956xxxx","accountTag":"xxxx516d1d6a7456546b8ce85368xxxx","filter":{"AND":[{"datetime_geq":"2025-03-16T14:14:38Z","datetime_leq":"2025-03-17T02:14:38Z","requestSource":"eyeball"},{"wafAttackScoreClass":"attack"}]}},"query":"query GetSecuritySampledLogs {n viewer {n scope: zones(filter: {zoneTag: $zoneTag}) {n logs: httpRequestsAdaptive(filter: $filter, limit: 100, orderBy: ["datetime_DESC"]) {n leakedCredentialCheckResultn cacheStatusn clientASNDescriptionn clientAsnn clientCountryNamen clientIPn clientRequestHTTPHostn clientRequestHTTPMethodNamen clientRequestHTTPProtocoln clientRequestPathn clientRequestSchemen userAgentn securityActionn securitySourcen datetimen rayNamen clientRequestReferern clientRequestQueryn contentScanNumMaliciousObjn contentScanNumObjn edgeResponseContentTypeNamen edgeResponseStatusn xRequestedWithn originResponseStatusn wafAttackScoreClassn __typenamen }n __typenamen }n __typenamen }n}n"}
// 提取所有 clientIP
const logs = $input.all()[0].json.data.viewer.scope[0].logs;
const ipCounts = {};
// 统计 IP 出现次数
logs.forEach(log => {
const ip = log.clientIP;
ipCounts[ip] = (ipCounts[ip] || 0) + 1;
});
// 过滤次数 >10 的 IP
const frequentIPs = Object.entries(ipCounts)
.filter(([ip, count]) => count > 5)
.map(([ip, count]) => ({ ip, count }));
// 返回结果(符合 n8n 输出格式)
return frequentIPs.map(ipInfo => ({
json: ipInfo
}));
https://api.cloudflare.com/client/v4/accounts/xxxx516d1d6axxxx546b8ce85368xxxx/rules/lists/xxxx2d4f1fd148xxxxae72c6ccd1xxxx/items
[
{
"ip": "1.2.3.4",
"comment": ""
}
]
原文始发于微信公众号(Ice ThirdSpace):利用n8n构造安全自动化流程SOAR联动cloudflare自动封禁IP
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论