越權漏洞修復參考

0x00 背景

 

https://www.xx.com/service/order.do?orderid=2280582085200280582

上圖,選擇orderid作爲參數, 越權遍歷爬取其他人的信息,類似選擇某個參數或者某個接口,通過拼接或者篡改數據的ID進行請求其他ID的內容,並且返回的數據存在敏感信息,簡稱爲越權漏洞。

惡意攻擊者可以利用漏洞攻擊做到:

  1. 水平越權,可以訪問同級用戶的身份證、手機號碼、納稅信息等
  2. 垂直越權,可以訪問管理員的敏感信息等。
  3. 修復思路

1、針對具有不同權限的用戶,必須在UI屏蔽未授權的功能導航,必須在服務器端對訪問的URL進行權限檢查,防止縱向越權訪問;

2、對於用戶資源的訪問,必須使用非直接對象引用、或訪問控制檢查,防止用戶訪問未授權的資源,防範橫向越權訪問。

0x01 修復思路

建議做一個過濾器,對權限進行全局校驗(每次調用某個接口時,可先對權限進行校驗)。大體流程是:第一步清洗URL地址,並提取Api接口名稱;第二步從session中提取當前登錄用戶的userid;第三步提取當前用戶的角色id;第四步判斷當前用戶對應的角色是否有權限訪問當前Api接口(檢查垂直越權);最後判斷當前登錄用戶是否對目標對象有操作權限(檢查水平越權)。

0x02 代碼修復

首先是獲取URL地址中的Api接口名稱,這裏對URL地址進行了一次URL解碼,還是存在繞過的風險,因爲當用戶%2523時,就可能繞過。

public static String GetApiName(String Url) throws Exception {

    String DecodeUrl =  URLDecoder.decode(Url,"UTF-8");URL url = new URL(DecodeUrl);String ApiUrl = url.getPath();    if(ApiUrl !=null){

        String[] ApiPath = ApiUrl.split("/");String ApiName = ApiPath[ApiPath.length-1];        return ApiName;}

    return null;}

獲取userid時,建議從session中獲取,而不是在cookie中再新建一個userid字段,用於標識用戶身份。

HttpSession session = ServletActionContext.getRequest().getSession();String userId = session.getAttribute("userId");*/

從session中提取userid後,就要查詢與之對應的角色id

//UserId爲用戶id,RoleId爲角色ID,通過UserId獲取roleidpublic static int GetRoleId(int UserId) {

    Connection conn = Connect();PreparedStatement st = null;ResultSet rs = null;    int RoleId = 0;    try {

        // 查詢接口訪問的角色id,roleid爲權限表裏的角色ID字段,apiname爲權限表裏的API接口名稱IDString sql = "select roleid from usertable where userid = ?";st = conn.prepareStatement(sql);// 這裏使用PreparedStatementst.setInt(1, UserId);rs = st.executeQuery();        if(rs.next()){

            RoleId = rs.getInt("roleid");            return RoleId;}

    } catch (Exception e) {

        e.printStackTrace();        throw new RuntimeException("查詢失敗!");}

    return RoleId;}

校驗垂直越權時,判斷當前用戶是否對指定的接口有訪問權限,U_RoleId爲用戶名對應的角色id,A_RoleId爲Api接口對應的角色id,Api_Name爲用戶嘗試訪問的API接口名稱(這裏在系統架構評審,安全設計階段,就要檢查數據庫的權限表設置時,Api接口是否有指定對應的角色id)

public static boolean CheckUpPrivilege(int UserId, String Api_Name) {

    Connection conn = Connect();PreparedStatement st = null;ResultSet rs = null;    int U_RoleId = GetRoleId(UserId);    int A_RoleId = 0;    try {

        String sql = "select roleid from user_role where apiname = ?";st = conn.prepareStatement(sql);// 這裏使用PreparedStatementst.setString(1, Api_Name);// 執行sql命令roleid和Role_Id,判斷用戶是否有權限訪問對應的接口地址rs = st.executeQuery();        if(rs.next()) {

            A_RoleId = rs.getInt("roleid");// 通過比較,當用戶角色id大於等於接口指定的角色id是,可以訪問,部分特定接口只有指定的角色才能訪問,可直接限定if (U_RoleId >= A_RoleId) {

                return true;} else {

                return false;}

        }

    } catch (Exception e) {

        e.printStackTrace();            throw new RuntimeException("查詢失敗!");}

    return false;}

最後再判斷一下是否是水平越權,S_UserId爲當前登錄用戶的userid,P_UserId爲目標對象對應的userid,比如對訂單信息進行操作時,可以先通過訂單號提取與之對應的userid,再進行判斷(當然,訂單表,在系統架構評審,安全設計階段,就要檢查訂單號是否有指定對應的用戶id)。

public static boolean CheckLevelPrivilege(int S_UserId, int P_UserId) {

    if(S_UserId == P_UserId){

        return true;}

    else{

        return false;}

}

防止越權訪問任意文件:
String permissionMask = System.getProperty("fileMask"):

Path path = testfile.toPath():

String [] validPermissions = { "r--------","r--r-----"}:

for (String validPermissions: validPermissions){

if (permissionMask.equals(validPermissions){

Set<PosixFilePermission> permission=

PosixFilePermissions.fromString(validPermissions):

File.setPosixFilePermissions(path.permission):

...

}

...

}
上述代碼可以通過允許用戶設置文件權限來防止此類文件權限操縱。如果必須確保用戶擁有能夠設置文件的權限,則可以採用一種間接方式,即創建一個允許用戶進行指定的合法文件權限列表,並且僅允許用戶從列表中進行選擇。通過這種方式,用戶提供的輸入將不會直接更改文件權限

防止越權刪除某參數的數據
@RequestMapping(value="/delete/{addrId}")
public Object remove(@PathVariable Long addrId){
Map<String, Object> respMap = new HashMap<String, Object>();
if (WebUtils.isLogged()) {
this.addressService.removeUserAddress(addrId,WebUtils.getLoggedUserId());
//關聯用戶身份
respMap.put(Constants.RESP_STATUS_CODE_KEY, Constants.RESP_STATUS_CODE_SUCCESS);
respMap.put(Constants.MESSAGE,"地址刪除成功!");
} 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章