WebAPi接口安全之公鑰私鑰加密


WebAPi使用公鑰私鑰加密介紹和使用

隨着各種設備的興起,WebApi作為服務也越來越流行。而在無任何保護措施的情況下接口完全暴露在外面,將導致被惡意請求。最近項目的項目中由於提供給APP的接口未對接口進行時間防范導致短信接口被怒對造成一定的損失,臨時的措施導致PC和app的防止措施不一樣導致后來前端調用相當痛苦,選型過oauth,https,當然都被上級未通過,那就只能自己寫了,就很,,ԾㅂԾ,,。下面就此次的方式做一次記錄。最終的效果:傳輸過程中都是密文,別人拿到請求串不能更改請求參數,通過接口過期時間防止同一請求串一直被調用。

   第一步重寫MessageProcessingHandler中的ProcessRequest和ProcessResponse


 

無論是APi還是Mvc請求管道都提供了我們很好的去擴展,本次說的是api,其實mvc大概意思也是差不多的。我們現在主要寫出大致流程

從圖中可以看出我們需要在MessageProcessingHandlder上做處理。我們繼承MessageProcessingHandlder重寫ProcessRequest和ProcessResponse方法,從方法名可以看出一個是針對請求值處理,一個是針對返回值處理代碼如下:

 1  public class CustomerMessageProcesssingHandler : MessageProcessingHandler
 2     {
 3         protected override HttpRequestMessage ProcessRequest(HttpRequestMessage request, CancellationToken cancellationToken)
 4         {
 5             var contentType = request.Content.Headers.ContentType;
 6 
 7             if (!request.Headers.Contains("platformtype"))
 8             {
 9                 return request;
10             }
11             //根據平台編號獲得對應私鑰
12             string privateKey = Encoding.UTF8.GetString(Convert.FromBase64String(ConfigurationManager.AppSettings["PlatformPrivateKey_" + request.Headers.GetValues("platformtype").FirstOrDefault()]));
13             if (request.Method == HttpMethod.Post)
14             {
15                 // 讀取請求body中的數據
16                 string baseContent = request.Content.ReadAsStringAsync().Result;
17                 // 獲取加密的信息
18                 // 兼容 body: 加密數據  和 body: sign=加密數據
19                 baseContent = Regex.Match(baseContent, "(sign=)*(?<sign>[\\S]+)").Groups[2].Value;
20                 // 用加密對象解密數據
21                 baseContent = CommonHelper.RSADecrypt(privateKey, baseContent);
22                 // 將解密后的BODY數據 重置
23                 request.Content = new StringContent(baseContent);
24                 //此contentType必須最后設置 否則會變成默認值
25                 request.Content.Headers.ContentType = contentType;
26             }
27             if (request.Method == HttpMethod.Get)
28             {
29                 string baseQuery = request.RequestUri.Query;
30                 // 讀取請求 url query數據
31                 baseQuery = baseQuery.Substring(1);
32                 baseQuery = Regex.Match(baseQuery, "(sign=)*(?<sign>[\\S]+)").Groups[2].Value;
33                 baseQuery = CommonHelper.RSADecrypt(privateKey, baseQuery);
34                 // 將解密后的 URL 重置URL請求
35                 request.RequestUri = new Uri($"{request.RequestUri.AbsoluteUri.Split('?')[0]}?{baseQuery}");
36             }
37             return request;
38         }
39         protected override HttpResponseMessage ProcessResponse(HttpResponseMessage response, CancellationToken cancellationToken)
40         {
41             return response;
42         }
43     }
上面的代碼大部分已經有注釋了,但這里說明三點第一:platformtype用來針對不同的平台設置不同的公鑰和私鑰;第二:在post方法中ContentType一定要最后設置,否則會成為默認值,這個問題會導致webapi不能進行正確的參數綁定;第三:有人可能會問這里ProcessResponse是不是可以不用重寫?答案是必須重寫如果不想對結果操作直接返回就如上當然你也可以在此對返回值進行加密,但是個人認為意義不大,看具體情況因為大部分數據加密后前端還是需要解密然后展示所以此處不做任何處理。在這一步我們已經對前端請求的加密串在handler中處理成明文重新賦值給HttpRequestMessage。
 

  第二步重寫AuthorizeAttribute中OnAuthorization和HandleUnauthorizedRequest


 


上圖是Api中Filter的請求順序,我們在第一個Filter上處理,代碼如下:
 1    public class CustomRequestAuthorizeAttribute : AuthorizeAttribute
 2     {
 3 
 4         public override void OnAuthorization(HttpActionContext actionContext)
 5         {
 6             //action具有[AllowAnonymous]特性不參與驗證
 7             if (actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().OfType<AllowAnonymousAttribute>().Any(x => x is AllowAnonymousAttribute))
 8             {
 9                 base.OnAuthorization(actionContext);
10                 return;
11             }
12             var request = actionContext.Request;
13             string method = request.Method.Method, timeStamp = string.Empty, expireyTime = ConfigurationManager.AppSettings["UrlExpireTime"], timeSign = string.Empty, platformType = string.Empty;
14             if (!request.Headers.Contains("timesign") || !request.Headers.Contains("platformtype") || !request.Headers.Contains("timestamp") || !request.Headers.Contains("expiretime"))
15             {
16                 HandleUnauthorizedRequest(actionContext);
17                 return;
18             }
19             platformType = request.Headers.GetValues("platformtype").FirstOrDefault();
20             timeSign = request.Headers.GetValues("timesign").FirstOrDefault();
21             timeStamp = request.Headers.GetValues("timestamp").FirstOrDefault();
22             var tempExpireyTime = request.Headers.GetValues("expiretime").FirstOrDefault();
23             string privateKey = Encoding.UTF8.GetString(Convert.FromBase64String(ConfigurationManager.AppSettings[$"PlatformPrivateKey_{platformType}"]));
24             if (!SignValidate(tempExpireyTime, privateKey, timeStamp, timeSign))
25             {
26                 HandleUnauthorizedRequest(actionContext);
27                 return;
28             }
29             if (tempExpireyTime != "0")
30             {
31                 expireyTime = tempExpireyTime;
32             }
33             //判斷timespan是否有效
34             double ts2 = ConvertHelper.ToDouble((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds, 2), ts = ts2 - ConvertHelper.ToDouble(timeStamp);
35             bool falg = ts > int.Parse(expireyTime) * 1000;
36             if (falg)
37             {
38                 HandleUnauthorizedRequest(actionContext);
39                 return;
40             }
41             base.IsAuthorized(actionContext);
42         }
43         protected override void HandleUnauthorizedRequest(HttpActionContext filterContext)
44         {
45             base.HandleUnauthorizedRequest(filterContext);
46 
47             var response = filterContext.Response = filterContext.Response ?? new HttpResponseMessage();
48             response.StatusCode = HttpStatusCode.Forbidden;
49             var content = new
50             {
51                 BusinessStatus = -10403,
52                 StatusMessage = "服務端拒絕訪問"
53             };
54             response.Content = new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json");
55         }
56         private bool SignValidate(string expiryTime, string privateKey, string timestamp, string sign)
57         {
58             bool isValidate = false;
59             var tempSign = CommonHelper.RSADecrypt(privateKey, sign);
60             if (CommonHelper.EncryptSHA256($"expiretime{expiryTime}" + $"timestamp{timestamp}") == tempSign)
61             {
62                 isValidate = true;
63             }
64             return isValidate;
65         }
66     }

請求頭部增加參數expiretime使用此參數作為本次接口的過期時間如果沒有則表示使用平台默認的接口時間,是我們可以針對不同的接口設置不同的過期時間;timestamp請求時間戳來防止別人拿到接口后一直調用timesign是過期時間和時間戳通過hash然后在通過公鑰加密的串來防止別人修改前兩個參數。重寫HandleUnauthorizedRequest來設置返回內容。


 

至此整個驗證過程就結束了,我們在使用過程中可以建立BaseApi將特性標記上讓其他APi繼承,當然我們的接口中可能有的action不需要驗證看OnAuthorization第一行代碼 增加相應的特性跳過此驗證。在整個過程中其實我們已經使用了兩種加密方式。一是本文中的CustomerMessageProcesssingHandler;另外一種就是timestamp+QueryString然后hash 在公鑰加密 這樣就不需要CustomerMessageProcesssingHandler其實就是本文中的頭部加密方式。

補充:園友建議增加請求端實例,確實是昨天有所遺漏。趁不忙補充上:

本次以HttpClient調用方式為例,展示Get,Post請求加密到執行的相應的action的過程;首先看一下Get請求如下:

可以看到我們的請求串url已經是密文,頭部時間sign也是密文,除非別人拿到我們的私鑰不然是不能修改其參數的。然后請求到達我們的CustomerMessageProcesssingHandler中我們看下Get中得到的參數是:

這是我們得到的前端傳過來的querystring的參數他的值就是我們前端加密后傳過來的下一步我們解密應該要得到未加密之前的參數也就是客戶端中id=1同時重新給requesturi賦值;

結果中我們可以看到id=1已被正確解密得到。接下來進入我們的CustomRequestAuthorizeAttribute

在這一步我們進行對timeSign的解密對請求只進行hash對比然后驗證時間戳是否在過期時間內最終我們到達相應的action:

這樣整個請求也就完成了Post跟Get區別不大重要的在於拿到傳遞參數的地方不一樣這里我只貼一下調用的代碼過程同上:

 1   public static void PostTestByModel() {
 2 
 3             HttpClient http = new HttpClient();
 4             var timestamp = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds;
 5             var expiretime = "600";
 6             var timesign = RSAEncrypt(publicKey, EncryptSHA256($"expiretime{expiretime}timestamp{timestamp}"));
 7             var codeValue = RSAEncrypt(publicKey, JsonConvert.SerializeObject(new Tenmp { Id = 1, Name = "cl" }));
 8             http.DefaultRequestHeaders.Add("platformtype", "Web");
 9             http.DefaultRequestHeaders.Add("timesign", $"{timesign}");
10             http.DefaultRequestHeaders.Add("timestamp", $"{string.Format("{0:N2}", timestamp.ToString()) }");
11             http.DefaultRequestHeaders.Add("expiretime", expiretime);
12             var url1 = string.Format($"{host}api/Values/PostTestByModel");
13             HttpContent content = new StringContent(codeValue);
14             MediaTypeHeaderValue typeHeader = new MediaTypeHeaderValue("application/json");
15             typeHeader.CharSet = "UTF-8";
16             content.Headers.ContentType = typeHeader;
17             var response1 = http.PostAsync(url1, content).Result;
18         }

最后當驗證不通過得到的返回值:

這也就是重寫HandleUnauthorizedRequest的目的 當然你也可以不重寫此方法那么返回的就是401 英文的未通過驗證。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM