AWS API Gateway ni huduma kamili inayotolewa na Amazon Web Services (AWS) iliyoundwa kwa ajili ya waendelezaji kuunda, kuchapisha, na kusimamia APIs kwa kiwango kikubwa. Inafanya kazi kama lango la kuingia kwenye programu, ikiruhusu waendelezaji kuanzisha mfumo wa sheria na taratibu. Mfumo huu unadhibiti ufikiaji wa watumiaji wa nje kwa data au kazi fulani ndani ya programu.
API Gateway inakuwezesha kufafanua jinsi maombi kwa APIs zako yanapaswa kushughulikiwa, na inaweza kuunda mwisho wa API maalum na mbinu maalum (mfano, GET, POST, PUT, DELETE) na rasilimali. Pia inaweza kuunda SDKs za mteja (Software Development Kits) ili kurahisisha waendelezaji kuita APIs zako kutoka kwa programu zao.
Aina za API Gateways
HTTP API: Jenga REST APIs zenye latency ya chini na gharama nafuu zikiwa na vipengele vilivyojumuishwa kama OIDC na OAuth2, na msaada wa asili wa CORS. Inafanya kazi na yafuatayo: Lambda, HTTP backends.
WebSocket API: Jenga API ya WebSocket kwa kutumia muunganisho endelevu kwa matumizi ya wakati halisi kama vile programu za mazungumzo au dashibodi. Inafanya kazi na yafuatayo: Lambda, HTTP, AWS Services.
REST API: Tengeneza REST API ambapo unapata udhibiti kamili juu ya ombi na jibu pamoja na uwezo wa usimamizi wa API. Inafanya kazi na yafuatayo: Lambda, HTTP, AWS Services.
REST API Binafsi: Unda REST API ambayo inapatikana tu kutoka ndani ya VPC.
Vipengele Vikuu vya API Gateway
Rasilimali: Katika API Gateway, rasilimali ni vipengele vinavyounda muundo wa API yako. Zinawakilisha njia au mwisho tofauti wa API yako na zinahusiana na vitendo mbalimbali ambavyo API yako inasaidia. Rasilimali ni kila mbinu (mfano, GET, POST, PUT, DELETE) ndani ya kila njia (/, au /users, au /user/{id}).
Hatua: Hatua katika API Gateway zinawakilisha matoleo au mazingira tofauti ya API yako, kama vile maendeleo, uhuishaji, au uzalishaji. Unaweza kutumia hatua kusimamia na kupeleka matoleo mengi ya API yako kwa wakati mmoja, ikiruhusu kujaribu vipengele vipya au marekebisho ya makosa bila kuathiri mazingira ya uzalishaji. Hatua pia zinasaidia mabadiliko ya hatua, ambayo ni jozi za funguo-thamani zinazoweza kutumika kubadilisha tabia ya API yako kulingana na hatua ya sasa. Kwa mfano, unaweza kutumia mabadiliko ya hatua kuelekeza maombi ya API kwa kazi tofauti za Lambda au huduma nyingine za nyuma kulingana na hatua.
Hatua inaonyeshwa mwanzoni mwa URL ya mwisho wa API Gateway.
Waandishi wa Idhini: Waandishi wa idhini katika API Gateway wana jukumu la kudhibiti ufikiaji wa API yako kwa kuthibitisha utambulisho wa mpiga simu kabla ya kuruhusu ombi kuendelea. Unaweza kutumia AWS Lambda functions kama waandishi wa idhini maalum, ambayo inakuruhusu kutekeleza mantiki yako mwenyewe ya uthibitishaji na idhini. Wakati ombi linapokuja, API Gateway inapeleka tokeni ya idhini ya ombi kwa waandishi wa idhini wa Lambda, ambaye anashughulikia tokeni na kurudisha sera ya IAM inayotathmini ni vitendo gani ambavyo mpiga simu anaruhusiwa kufanya. API Gateway pia inasaidia waandishi wa idhini wa ndani, kama vile AWS Identity and Access Management (IAM) na Amazon Cognito.
Sera ya Rasilimali: Sera ya rasilimali katika API Gateway ni hati ya JSON ambayo inafafanua ruhusa za kufikia API yako. Ni sawa na sera ya IAM lakini imeandaliwa mahsusi kwa API Gateway. Unaweza kutumia sera ya rasilimali kudhibiti nani anaweza kufikia API yako, ni mbinu zipi wanaweza kuita, na kutoka IP gani au VPC gani wanaweza kuungana. Sera za rasilimali zinaweza kutumika kwa pamoja na waandishi wa idhini ili kutoa udhibiti wa ufikiaji wa kina kwa API yako.
Ili kufanya kazi, API inahitaji kupelekwa tena baada ya sera ya rasilimali kubadilishwa.
Kumbukumbu
Kwa kawaida, CloudWatch Logs zime zimwa, Kumbukumbu ya Ufikiaji ime zimwa, na X-Ray tracing pia ime zimwa.
Uhesabu
Kumbuka kwamba katika APIs zote za AWS kuhesabu rasilimali (apigateway na apigatewayv2) ruhusa pekee unayohitaji na ruhusa pekee ya kusoma inayoweza kutolewa ni apigateway:GET, kwa hiyo unaweza kuhesabu kila kitu.
# Generic infoawsapigatewayget-accountawsapigatewayget-domain-namesawsapigatewayget-usage-plansawsapigatewayget-vpc-linksawsapigatewayget-client-certificates# Enumerate APIsawsapigatewayget-rest-apis# This will also show the resource policy (if any)## Get stagesawsapigatewayget-stages--rest-api-id<id>## Get resourcesawsapigatewayget-resources--rest-api-id<id>## Get API resource action per HTTP verb (check authorizers and api key required)awsapigatewayget-method--http-methodGET--rest-api-id<api-id>--resource-id<resource-id>## Call APIhttps://<api-id>.execute-api.<region>.amazonaws.com/<stage>/<resource>## API authorizersawsapigatewayget-authorizers--rest-api-id<id>## Modelsawsapigatewayget-models--rest-api-id<id>## More infoawsapigatewayget-gateway-responses--rest-api-id<id>awsapigatewayget-request-validators--rest-api-id<id>awsapigatewayget-deployments--rest-api-id<id># Get api keys generatedawsapigatewayget-api-keys--include-valueawsapigatewayget-api-key--api-key<id>--include-value# Get just 1## Example use API keycurl-XGET-H"x-api-key: AJE&Ygenu4[..]"https://e83uuftdi8.execute-api.us-east-1.amazonaws.com/dev/test## Usage plansawsapigatewayget-usage-plans#Get limit use infoawsapigatewayget-usage-plan-keys--usage-plan-id<plan_id>#Get clear text values of api keysawsapigatewayget-usage-plan-key--usage-plan-id<plan_id>--key-id<key_id>###Already consumedawsapigatewayget-usage--usage-plan-id<plan_id>--start-date2023-07-01--end-date2023-07-12
# Generic infoawsapigatewayv2get-domain-namesawsapigatewayv2get-domain-name--domain-name<name>awsapigatewayv2get-vpc-links# Enumerate APIsawsapigatewayv2get-apis# This will also show the resource policy (if any)awsapigatewayv2get-api--api-id<id>## Get all the info from an api at onceawsapigatewayv2export-api--api-id<id>--output-typeYAML--specificationOAS30/tmp/api.yaml## Get stagesawsapigatewayv2get-stages--api-id<id>## Get routesawsapigatewayv2get-routes--api-id<id>awsapigatewayv2get-route--api-id<id>--route-id<route-id>## Get deploymentsawsapigatewayv2get-deployments--api-id<id>awsapigatewayv2get-deployment--api-id<id>--deployment-id<dep-id>## Get integrationsawsapigatewayv2get-integrations--api-id<id>## Get authorizersawsapigatewayv2get-authorizers--api-id<id>awsapigatewayv2get-authorizer--api-id<id>--authorizer-id<uth-id>## Get domain mappingsawsapigatewayv2get-api-mappings--api-id<id>--domain-name<dom-name>awsapigatewayv2get-api-mapping--api-id<id>--api-mapping-id<map-id>--domain-name<dom-name>## Get modelsawsapigatewayv2get-models--api-id<id>## Call APIhttps://<api-id>.execute-api.<region>.amazonaws.com/<stage>/<resource>
Mamlaka tofauti za kufikia mwisho wa API Gateway
Sera ya Rasilimali
Inawezekana kutumia sera za rasilimali kufafanua nani anaweza kuita mwisho wa API.
Katika mfano ufuatao unaweza kuona kwamba IP iliyoonyeshwa haiwezi kuita mwisho wa /resource_policy kupitia GET.
IAM Authorizer
Inawezekana kuweka kwamba mbinu ndani ya njia (rasilimali) inahitaji uthibitisho wa IAM ili kuitwa.
Wakati hii imewekwa utapokea kosa {"message":"Missing Authentication Token"} unapojaribu kufikia mwisho bila mamlaka yoyote.
Njia rahisi ya kuzalisha token inayotarajiwa na programu ni kutumia curl.
Kumbuka kwamba katika hali nyingine Authorizer inaweza kuwa imeandikwa vibaya na kutuma chochote ndani ya Authorization header kutaruhusu kuona maudhui yaliyofichwa.
Inawezekana kutumia lambda ambayo kwa msingi wa token fulani it arejeshe sera ya IAM ikionyesha kama mtumiaji ameidhinishwa kuita kiunganishi cha API.
Unaweza kuweka kila njia ya rasilimali ambayo itakuwa ikitumia mthibitishaji.
</details>
Call it with something like:
<pre class="language-bash" data-overflow="wrap"><code class="lang-bash"><strong>curl "https://jhhqafgh6f.execute-api.eu-west-1.amazonaws.com/prod/custom_auth" -H 'Authorization: your-secret-token'
</strong></code></pre>
<div data-gb-custom-block data-tag="hint" data-style='warning'>
Kulingana na msimbo wa Lambda, ruhusa hii inaweza kuwa na hatari
</div>
Note that if a **deny policy is generated and returned** the error returned by API Gateway is: `{"Message":"User is not authorized to access this resource with an explicit deny"}`
This way you could **identify this authorization** being in place.
### Required API Key
It's possible to set API endpoints that **require a valid API key** to contact it.
<figure><img src="../../../.gitbook/assets/image (88).png" alt=""><figcaption></figcaption></figure>
It's possible to generate API keys in the API Gateway portal and even set how much it can be used (in terms of requests per second and in terms of requests per month).
To make an API key work, you need to add it to a **Usage Plan**, this usage plan must be added to the **API Stage** and the associated API stage needs to have a configured **method throttling** to the **endpoint** requiring the API key:
<figure><img src="../../../.gitbook/assets/image (198).png" alt=""><figcaption></figcaption></figure>
## Unauthenticated Access
<div data-gb-custom-block data-tag="content-ref" data-url='../aws-unauthenticated-enum-access/aws-api-gateway-unauthenticated-enum.md'>
[aws-api-gateway-unauthenticated-enum.md](../aws-unauthenticated-enum-access/aws-api-gateway-unauthenticated-enum.md)
</div>
## Privesc
<div data-gb-custom-block data-tag="content-ref" data-url='../aws-privilege-escalation/aws-apigateway-privesc.md'>
[aws-apigateway-privesc.md](../aws-privilege-escalation/aws-apigateway-privesc.md)
</div>
## Post Exploitation
<div data-gb-custom-block data-tag="content-ref" data-url='../aws-post-exploitation/aws-api-gateway-post-exploitation.md'>
[aws-api-gateway-post-exploitation.md](../aws-post-exploitation/aws-api-gateway-post-exploitation.md)
</div>
## Persistence
<div data-gb-custom-block data-tag="content-ref" data-url='../aws-persistence/aws-api-gateway-persistence.md'>
[aws-api-gateway-persistence.md](../aws-persistence/aws-api-gateway-persistence.md)
</div>
<div data-gb-custom-block data-tag="hint" data-style='success'>
Learn & practice AWS Hacking:<img src="../../../.gitbook/assets/image (1) (1) (1) (1).png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="../../../.gitbook/assets/image (1) (1) (1) (1).png" alt="" data-size="line">\
Learn & practice GCP Hacking: <img src="../../../.gitbook/assets/image (2) (1).png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="../../../.gitbook/assets/image (2) (1).png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
<details>
<summary>Support HackTricks</summary>
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks_live)**.**
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
</details>
</div>