关于 implementing-api-threat-protection-with-apigee
用于配置 Google Apigee 策略以保护 API 的实践技能,可应对格式异常内容、注入模式、流量激增和无效凭证。适用于通过 Apigee 代理部署 API 安全控制的团队。
主要功能
- 01JSON 与 XML 验证策略
- 02基于正则表达式的注入防护
- 03SpikeArrest 流量限制
- 04OAuth 2.0 与 API 密钥验证
- 05Advanced API Security 配置
适用场景
- 01为 API 代理应用请求验证
- 02限制发往后端服务的流量激增
- 03执行令牌和 API 密钥检查
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mukul975/ anthropic-cybersecurity-skills --skill implementing-api-threat-protection-with-apigee安装后在 Claude Code 中输入/implementing-api-threat-protection-with-apigee确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
LICENSEreferences/api-reference.mdscripts/agent.py详细介绍
使用 Apigee 实现 API 威胁防护
概述
Google Apigee 是一个企业级 API 管理平台,提供用于威胁防护的原生安全策略,包括 JSON 和 XML 内容验证、OAuth 2.0 强制实施、SpikeArrest 速率限制、正则表达式威胁防护,以及用于检测恶意客户端和 API 滥用模式的 Advanced API Security。Apigee 作为反向代理运行,拦截所有 API 流量,在请求到达后端服务之前应用安全策略,从而有效保护 API 免受 OWASP API Security Top 10 威胁的影响。
何时使用
- 在您的环境中部署或配置使用 Apigee 实现 API 威胁防护功能时
- 在建立符合合规要求的安全控制措施时
- 在为该领域构建或改进安全架构时
- 在开展需要此实现的安全评估时
前提条件
- 已配置 Apigee 组织的 Google Cloud Platform 账号
- 已配置 Apigee X 或 Apigee hybrid 环境
- 已部署后端 API 服务,并可从 Apigee 访问
- 已安装并完成身份验证的 Google Cloud CLI(gcloud)
- 目标 API 的 OpenAPI 规范
- 了解 Apigee 代理包结构
核心安全策略
1. JSON 威胁防护
通过限制结构深度、条目数量和字符串长度,防范基于 JSON 的拒绝服务攻击:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<JSONThreatProtection name="JSON-Threat-Protection-1">
<DisplayName>JSON Threat Protection</DisplayName>
<Source>request</Source>
<ObjectEntryNameLength>50</ObjectEntryNameLength>
<ObjectEntryCount>25</ObjectEntryCount>
<ArrayElementCount>100</ArrayElementCount>
<ContainerDepth>5</ContainerDepth>
<StringValueLength>500</StringValueLength>
</JSONThreatProtection>
2. XML 威胁防护
防范 XML 炸弹、XXE 攻击和超大 XML 负载:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XMLThreatProtection name="XML-Threat-Protection-1">
<DisplayName>XML Threat Protection</DisplayName>
<Source>request</Source>
<NameLimits>
<Element>50</Element>
<Attribute>50</Attribute>
<NamespacePrefix>20</NamespacePrefix>
<ProcessingInstructionTarget>50</ProcessingInstructionTarget>
</NameLimits>
<ValueLimits>
<Text>1000</Text>
<Attribute>500</Attribute>
<NamespaceURI>256</NamespaceURI>
<Comment>256</Comment>
<ProcessingInstructionData>256</ProcessingInstructionData>
</ValueLimits>
<StructureLimits>
<NodeDepth>5</NodeDepth>
<AttributeCountPerElement>5</AttributeCountPerElement>
<NamespaceCountPerElement>3</NamespaceCountPerElement>
<ChildCount>25</ChildCount>
</StructureLimits>
</XMLThreatProtection>
3. 正则表达式威胁防护
检测请求参数中的 SQL 注入、XSS 及其他注入模式:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RegularExpressionProtection name="RegEx-Threat-Protection-1">
<DisplayName>Regex Injection Protection</DisplayName>
<Source>request</Source>
<IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
<QueryParam name="*">
<Pattern>[\s]*((delete)|(exec)|(drop\s*table)|(insert)|(shutdown)|(update)|(\bor\b))</Pattern>
</QueryParam>
<QueryParam name="*">
<Pattern>[\s]*<\s*script\b[^>]*>[^<]+<\s*/\s*script\s*></Pattern>
</QueryParam>
<Header name="*">
<Pattern>[\r\n]</Pattern>
</Header>
<URIPath>
<Pattern>(/\.\.)|(\.\./)</Pattern>
</URIPath>
<JSONPayload>
<JSONPath>$.*</JSONPath>
<Pattern>[\s]*((delete)|(exec)|(drop\s*table)|(insert)|(shutdown)|(update))</Pattern>
</JSONPayload>
</RegularExpressionProtection>
4. SpikeArrest 策略
防止流量峰值压垮后端服务:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SpikeArrest name="Spike-Arrest-1">
<DisplayName>API Spike Arrest</DisplayName>
<Rate>30ps</Rate>
<Identifier ref="request.header.x-api-key"/>
<MessageWeight ref="request.header.x-request-weight"/>
<UseEffectiveCount>true</UseEffectiveCount>
</SpikeArrest>
5. OAuth 2.0 令牌验证
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<OAuthV2 name="Verify-OAuth-Token">
<DisplayName>Verify OAuth 2.0 Access Token</DisplayName>
<Operation>VerifyAccessToken</Operation>
<ExternalAuthorization>false</ExternalAuthorization>
<ExternalAccessToken>request.header.Authorization</ExternalAccessToken>
<SupportedGrantTypes>
<GrantType>authorization_code</GrantType>
<GrantType>client_credentials</GrantType>
</SupportedGrantTypes>
<Scope>read write</Scope>
<GenerateResponse enabled="true"/>
</OAuthV2>
6. API 密钥验证
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<VerifyAPIKey name="Verify-API-Key-1">
<DisplayName>Verify API Key</DisplayName>
<APIKey ref="request.header.x-api-key"/>
</VerifyAPIKey>
代理包配置
完整的安全代理流程
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ProxyEndpoint name="default">
<PreFlow name="PreFlow">
<Request>
<Step>
<Name>Verify-OAuth-Token</Name>
</Step>
<Step>
<Name>Spike-Arrest-1</Name>
</Step>
<Step>
<Name>JSON-Threat-Protection-1</Name>
<Condition>request.header.Content-Type = "application/json"</Condition>
</Step>
<Step>
<Name>XML-Threat-Protection-1</Name>
<Condition>request.header.Content-Type = "text/xml"</Condition>
</Step>
<Step>
<Name>RegEx-Threat-Protection-1</Name>
</Step>
<Step>
<Name>CORS-Policy</Name>
</Step>
</Request>
<Response>
<Step>
<Name>Remove-Internal-Headers</Name>
</Step>
<Step>
<Name>Add-Security-Headers</Name>
</Step>
</Response>
</PreFlow>
<Flows>
<Flow name="sensitive-operations">
<Description>Additional protection for sensitive endpoints</Description>
<Request>
<Step>
<Name>Quota-Strict</Name>
</Step>
</Request>
<Condition>(proxy.pathsuffix MatchesPath "/admin/**") or
(proxy.pathsuffix MatchesPath "/users/*/sensitive")</Condition>
</Flow>
</Flows>
<HTTPProxyConnection>
<BasePath>/v1</BasePath>
<VirtualHost>secure</VirtualHost>
</HTTPProxyConnection>
<RouteRule name="default">
<TargetEndpoint>default</TargetEndpoint>
</RouteRule>
</ProxyEndpoint>
安全标头策略
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage name="Add-Security-Headers">
<DisplayName>Add Security Response Headers</DisplayName>
<Set>
<Headers>
<Header name="X-Content-Type-Options">nosniff</Header>
<Header name="X-Frame-Options">DENY</Header>
<Header name="Strict-Transport-Security">max-age=31536000; includeSubDomains</Header>
<Header name="Cache-Control">no-store, no-cache, must-revalidate</Header>
<Header name="Content-Security-Policy">default-src 'none'</Header>
<Header name="X-Request-ID">{messageid}</Header>
</Headers>
</Set>
<Remove>
<Headers>
<Header name="X-Powered-By"/>
<Header name="Server"/>
</Headers>
</Remove>
<IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
<AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>
Advanced API Security
启用 Apigee 的 Advanced API Security 附加组件,以实现基于机器学习的威胁检测:
# Enable Advanced API Security on Apigee X instance
gcloud apigee organizations update $ORG_NAME \
--advanced-api-security-config=enabled
# View detected abuse alerts
gcloud apigee apis security-reports list \
--organization=$ORG_NAME \
--environment=$ENV_NAME
# Create security action to block suspicious traffic
gcloud apigee security-actions create \
--organization=$ORG_NAME \
--environment=$ENV_NAME \
--action-type=DENY \
--condition-type=IP_ADDRESS \
--condition-values="192.168.1.100,10.0.0.50" \
--description="Block identified malicious IPs"
部署
# Deploy proxy bundle with security policies
gcloud apigee apis deploy \
--api=$API_NAME \
--environment=$ENV_NAME \
--revision=$REVISION \
--organization=$ORG_NAME
# Validate deployment
gcloud apigee apis list-deployments \
--api=$API_NAME \
--organization=$ORG_NAME
参考资料
- Apigee JSON 威胁防护:https://cloud.google.com/apigee/docs/api-platform/reference/policies/json-threat-protection-policy
- Google Cloud Apigee 安全最佳实践:https://cloud.google.com/architecture/best-practices-securing-applications-and-apis-using-apigee
- Apigee Advanced API Security:https://docs.cloud.google.com/apigee/docs/api-security
- Apigee OWASP API Top 10:https://docs.apigee.com/api-platform/faq/owasp-top-api-threats
- Wallarm Apigee 安全策略指南:https://lab.wallarm.com/what/apigee-api-security-policies-howto/
评论
支持 Markdown