Một dự án Tiến trình thương mại nội bộ gần đây đã cho phép chúng tôi có cơ hội thiết lập trang web Drupal 8 và để REST hoạt động trên đó. Trong quá trình này, và nhờ ngăn xếp mới cùng với tài liệu bắt kịp, tôi đã học được một số thủ thuật có thể hữu ích cho các nhà phát triển dám nghĩ dám làm ngoài kia
1. Các thực thể POST là một đường dẫn khác với giao diện người dùng REST nói
Nếu bạn cài đặt mô-đun REST UI và bật điểm cuối, bạn sẽ thấy màn hình như
Từ màn hình này, bạn có thể tin rằng URL mà bạn sử dụng để HTTP POST một thực thể mới [ví dụ: nếu gói được đặt tên là "cập nhật"] sẽ là
http. //tên của bạn. tld/admin/cấu trúc/eck/thực thể/thành phần/cập nhật
nhưng bạn có thể đã sai. Đường dẫn bài đăng thực tế là
http. //tên của bạn. tld/thực thể/thành phần
trong đó "thành phần" là loại thực thể. Đây dường như là trường hợp của hầu hết mọi thực thể. Đừng quên
?format=haljson
trên tất cả những thứ của bạn, cho biện pháp tốt
2. Có một thứ gọi là mã thông báo CSRF và bạn có thể nhận một mã thông báo bằng cURL
Tài liệu này cho bạn biết rằng các hoạt động rủi ro cần có mã thông báo CSRF và cách nhận mã thông báo này. Yuen Ying Kit thậm chí còn có mã PHP dựa trên Guzzle cần thiết [đăng vào năm 2013. ] để lấy mã thông báo CSRF một cách nhanh chóng
Giả sử bạn muốn viết nó cho một yêu cầu cURL trong PHP mà không có bất kỳ phụ thuộc bên ngoài nào. Điều đó sẽ trông giống như
Biến $csrf_token hiện chứa mã thông báo đang hoạt động để POST-ing và thực hiện các hoạt động rủi ro khác
Đối với một yêu cầu cURL để thực hiện một thao tác như vậy, tiêu đề HTTP của bạn phải bao gồm những điều sau đây
Miễn là bạn có tải trọng chấp nhận được trong CURLOPT_POSTFIELDS của mình, bạn nên tham gia cuộc đua
3. Các trường tham chiếu thực thể hoạt động theo cách bạn mong đợi, ngoại trừ không
Nếu bạn NHẬN một thực thể có trường tham chiếu thực thể [Tham chiếu? Tham chiếu thực thể?] dưới dạng HAL JSON, kết quả sẽ bao gồm một số mẩu tin thú vị cho biết tham chiếu
Đây là tất cả các tải trọng hữu ích để tương tác với các thực thể được tham chiếu khi nhập, nhưng
Nếu bạn định dạng một thực thể HAL JSON cho POST bằng cấu trúc này, thì trường tham chiếu sẽ không được điền [ít nhất là nó không có đối với tôi]. Thủ thuật cũng bao gồm một giá trị trực tiếp cho trường mong muốn, như
Điều này sẽ thêm JSON trông giống như
mà phần phụ trợ sẽ xử lý thành tài liệu tham khảo. Tôi giữ những thứ HAL khác để có biện pháp tốt
kết thúc
Vì vậy, đó là những thứ tôi tìm thấy trong lần đầu tiên làm việc với RESTful trong Drupal 8. Chắc chắn sẽ có nhiều cách khác đến từ đâu và chúng tôi chắc chắn sẽ giữ cho bạn được ĐĂNG với tất cả các vấn đề và điều chỉnh thú vị mà chúng tôi tìm thấy trên đường đi
×
Chạy lệnh cURL đầu tiên của chúng tôi
- Dán lệnh cURL vào bash Shell
- Thêm công tắc -v để lấy tiêu đề HTTP bị hủy
- chuyển hướng stderr bằng cách thêm. 2>&1
$ curl '//52.58.164.53:8443/index.action' -H 'Pragma: no-cache' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.8' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 [Windows NT 6.1; Win64; x64] AppleWebKit/537.36 [KHTML, like Gecko] Chrome/61.0.3163.100 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'Cache-Control: no-cache' -H 'Cookie: JSESSIONID=1jzw19tuvg42wl93fxp1exzkf' -H 'Connection: keep-alive' --compressed --insecure -v 2>&1 % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 ... { [5 bytes data] < HTTP/1.1 200 OK < Expires: Thu, 01-Jan-1970 00:00:00 GMT < Set-Cookie: JSESSIONID=ioyf0enlkduy1geo82r2tryk8;Path=/;Secure;HttpOnly < Cache-Control: no-store,no-cache < X-Frame-Options: SAMEORIGIN < X-Download-Options: noopen < X-XSS-Protection: 1;mode=block < Strict-Transport-Security: max-age=31536000;includeSubdomains < X-Content-Type-Options: nosniff < Content-Language: de-DE < Content-Type: text/html; charset=utf-8 < Content-Length: 8471 ...
Trạng thái HTTP của yêu cầu cURL đầu tiên của chúng tôi
- Website được tải thành công -> HTTP/1. 1 200 được
- Độ Dài Nội Dung Trang Web bây giờ là. 8471
- Trang web sử dụng cookie. JSESSIONID
- Website sử dụng giao thức https
Sử dụng Google Dev để hiểu Logic đăng nhập
JavaScript và các tệp JavaScript tải theo chỉ mục Yêu cầu ban đầu. hành động tải 2 tệp javascript
- đăng nhập. js
- xác minh mã. js
Xử lý và xác thực biểu mẫu đăng nhập
var verifyResult; $["#login"].click[function[] { if [!allowLogin] { jAlert[browserMessage, Message.alarm_info, '{"' + Message.sur + '":"OK"}']; clearPassword[]; return; } var userName = $.trim[$["#userName"].val[]]; var password = $["#password"].val[]; if [!loginValidate[userName, password]] { clearPassword[]; return; } var showVerifyCode = document.getElementById["verifyCode_tr"].style.display; comitLogin[userName, password]; }];
- Đối với hướng dẫn cURL của chúng tôi, chúng tôi có thể bỏ qua Bước này vì chúng tôi đã biết tên người dùng/mật khẩu
- Hãy xem lại hàm JS comitLogin[]
Mã đăng nhập được triển khai thông qua đồng bộ hóa. Yêu cầu BÀI ĐĂNG AJAX
function comitLogin[userName, password] { if [$["#veryCode"].is[":visible"]] { if [!validateVCodeLength[]] { clearPassword[]; return; } } var code = $["#veryCode"].attr["value"]; $.ajax[{ url : basePath + "security!login.action", type : "POST", async : false, dataType : "json", data : { "userName" : userName, "password" : password, dateTime : new Date[].getTime[], "veryCode" : code }, cache : false, beforeSend:function [XMLHttpRequest] { //let go }, success : function[res] { clearPassword[]; $["#pass1"].val[""]; $["#pass2"].val[""]; $["#loginErrorMsg"].html[""] $["#loginErrorMsg2"].html[""]; if [res.retMsg == "op.successfully"] { window.location.href = newPath + "securitys!tologin.action"; return; } else if [res.retMsg == "op.update"] { $["#loginView"].hide[]; ....
- Để gửi tên người dùng, mật khẩu, ngày tháng và Mã xác minh, yêu cầu POST được sử dụng
- Chức năng từ xa được gọi là bảo mật. đăng nhập. hoạt động
- Đăng nhập thành công sẽ trở lại. “op. thành công”
- Sau khi đăng nhập thành công, chuyển hướng trang được kích hoạt tới. an ninh. để đăng nhập. hoạt động
Bước 1. Triển khai Đăng nhập JavaScript trang web bằng bash/cURL
Đặt điểm dừng trong AJAX POTS khi quay lại từ yêu cầu AJAX POST- For further processing we need to use the JSESSIONID returned by the Response header
Dán ở trên Đầu ra vào Bash Shell
$ curl '//52.58.164.53:8443/security!login.action' -H 'Pragma: no-cache' -H 'Origin: //52.58.164.53:8443' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 [Windows NT 6.1; Win64; x64] AppleWebKit/537.36 [KHTML, like Gecko] Chrome/61.0.3163.100 Safari/537.36' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Cache-Control: no-cache' -H 'X-Requested-With: XMLHttpRequest' -H 'Cookie: JSESSIONID=jhqdzlufdh95hevd878z5nig' -H 'Connection: keep-alive' -H 'Referer: //52.58.164.53:8443/index.action' --data 'userName=pvlocal&password=PPPP&dateTime=1510764929706&veryCode=' --compressed % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 curl: [60] SSL certificate problem: self signed certificate in certificate chain More details here: //curl.haxx.se/docs/sslcerts.html curl failed to verify the legitimacy of the server and therefore could not establish a secure connection to it. To learn more about this situation and how to fix it, please visit the web page mentioned above. -> Disable SSL verification by using -k switch $ curl '//52.58.164.53:8443/security!login.action' -H 'Pragma: no-cache' -H 'Origin: //52.58.164.53:8443' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 [Windows NT 6.1; Win64; x64] AppleWebKit/537.36 [KHTML, like Gecko] Chrome/61.0.3163.100 Safari/537.36' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Cache-Control: no-cache' -H 'X-Requested-With: XMLHttpRequest' -H 'Cookie: JSESSIONID=jhqdzlufdh95hevd878z5nig' -H 'Connection: keep-alive' -H 'Referer: //52.58.164.53:8443/index.action' --data 'userName=pvlocal&password=PPPP&dateTime=1510764929706&veryCode=' --compressed -k -i % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 95 100 28 100 67 28 67 0:00:01 --:--:-- 0:00:01 196HTTP/1.1 200 OK Expires: Thu, 01-Jan-1970 00:00:00 GMT Set-Cookie: JSESSIONID=1pt4jamjah1vbl44uep5ecvzm;Path=/;Secure;HttpOnly Cache-Control: no-store,no-cache X-Frame-Options: SAMEORIGIN X-Download-Options: noopen X-XSS-Protection: 1;mode=block Strict-Transport-Security: max-age=31536000;includeSubdomains X-Content-Type-Options: nosniff Content-Language: de-DE Set-Cookie: JSESSIONID=a5sjmaztpwa81acg06hymnxgb;Path=/;Secure;HttpOnly Content-Type: application/json;charset=UTF-8 Content-Length: 28 {"retMsg":"op.successfully"} -> cURL Request works Now
Kiểm tra đăng nhập không hợp lệ bằng cách thay đổi mật khẩu thành xxxx
$ curl '//52.58.164.53:8443/security!login.action' -H 'Pragma: no-cache' -H 'Origin: //52.58.164.53:8443' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 [Windows NT 6.1; Win64; x64] AppleWebKit/537.36 [KHTML, like Gecko] Chrome/61.0.3163.100 Safari/537.36' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Cache-Control: no-cache' -H 'X-Requested-With: XMLHttpRequest' -H 'Cookie: JSESSIONID=jhqdzlufdh95hevd878z5nig' -H 'Connection: keep-alive' -H 'Referer: //52.58.164.53:8443/index.action' --data 'userName=pvlocal&password=xxxx&dateTime=1510764929706&veryCode=' --compressed -k
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 125 100 58 100 67 58 67 0:00:01 --:--:-- 0:00:01 348 {"retMsg":"Falscher Benutzername oder falsches Passwort."}
Tạo Tập lệnh Bash để Tự động hóa – WRlogin. sh
#!/bin/bash # # Use Google Dev dev tools and select Copy as cURL [bash] to learn quickly about the Curl Syntax for a HTTP request # # After a successfull login a NEW JSESSIONID returned from the response cookie should be used for subsequent requests # loginCredentials="userName=pvlocal&password=Feli2010&dateTime=1510483708371&veryCode" url="//52.58.164.53:8443/security!login.action" echo "--------------------------------------------------------------------------------------------------------" echo "-> URL :" $url echo "-> INPUT Cookie :" $cookie echo "-> INPUT Login Credentials :" $loginCredentials echo "-> OUTPUT :" NEW JSESSIONID cookie after successfull Login echo "--------------------------------------------------------------------------------------------------------" # # DebugON set, possibly to the empty string will printout add. trace info # To enable Debugging run: $ export DebugON # if [ $DebugON ]; then echo "DebugON is set, possibly to the empty string" set -x fi # # Only use the needed HTTP header Fields - Most Http Header Fields copied from our Google DEV Tool cURL copy we do not need curl_header1='Accept-Language: en-US,en;q=0.8' curl_header2='Accept-Encoding: gzip, deflate, br' curl_header3='Accept: application/json, text/javascript, */*; q=0.01' # # Arrays makes this much easier. Don't use Quotes here as $cookie and $csrf_token content uses spaces ! # args=["-k" "-v" "$url" -H "$cookie" "--data" "$loginCredentials" -H "$csrf_token" -H "$curl_header1" -H "${curl_header2}" -H "${curl_header3}"] # echo "---------------------------------- cURL command to be executed -----------------------------------------" echo curl "${args[@]}" echo "--------------------------------------------------------------------------------------------------------" output=$[ curl "${args[@]}" 2>&1 ] # # Sucessfull Login returns following string # '"retMsg":"op.successfully"} # login_status="" login_status=$[echo "$output" | grep retMsg | awk -v FS="[retMsg\":\"|\"}]" '{print $2 }'] # # Note; login.sh return two occurances of string: # Set-Cookie: JSESSIONID=h0eaof37hnta1wtjlk1j1nig2;Path=/;Secure;HttpOnly # As we are only intrested on the 2.nd one use: sed -n 2p # expcmd="" expcmd=$[echo "$output" | grep Set-Cookie | sed -n 2p | awk -v FS="[Set-Cookie: |;]" '{print "export cookie=\"Cookie: " $2 "\""}'] http_return="" http_return=$[echo "$output" | grep '< HTTP'] #echo "Login Status : " "$login_status" # echo "New JSESSIONID EXPORT command: " "$expcmd" if [ "$login_status" == "op.successfully" ]; then echo "--------------------------------------------------------------------------------------------------------" echo "HTTP Return Code: " "$http_return" echo "Login OK : Javascript Return Status:" "$login_status" echo "Run New JSESSIONID EXPORT command: \$" "$expcmd" echo "--------------------------------------------------------------------------------------------------------" elif [ "$login_status" == "op.verifyCode.fail" ]; then echo "--------------------------------------------------------------------------------------------------------" echo "HTTP Return Code: " "$http_return" echo "Login Failded - Wrong verification Code : Javascript Return Status:" "$login_status" echo "Login to WebSite and enter verification Code - after successfull login rerun this script !" echo "--------------------------------------------------------------------------------------------------------" else echo "--------------------------------------------------------------------------------------------------------" echo "HTTP Return Code: " "$http_return" echo "Login failed check Login credentials - Javascript Return Status: " "$login_status" echo "--------------------------------------------------------------------------------------------------------" fi
Kiểm tra bash Script WRlogin. sh
$ ./WRlogin.sh -------------------------------------------------------------------------------------------------------- -> URL : //52.58.164.53:8443/security!login.action -> INPUT Cookie : Cookie: JSESSIONID=17grbvbtwu2ga9blndj7rfrsk -> INPUT Login Credentials : userName=pvlocal&password=Feli2010&dateTime=1510483708371&veryCode -> OUTPUT : NEW JSESSIONID cookie after successfull Login -------------------------------------------------------------------------------------------------------- ---------------------------------- cURL command to be executed ----------------------------------------- curl -k -v //52.58.164.53:8443/security!login.action -H Cookie: JSESSIONID=17grbvbtwu2ga9blndj7rfrsk --data userName=pvlocal&password=Feli2010&dateTime=1510483708371&veryCode -H X-CSRF-TOKEN: c3d70f5d-4388-4d85-a1db-9ac797b92418 -H Accept-Language: en-US,en;q=0.8 -H Accept-Encoding: gzip, deflate, br -H Accept: application/json, text/javascript, */*; q=0.01 -------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------- HTTP Return Code: < HTTP/1.1 200 OK Login OK : Javascript Return Status: op.successfully Run New JSESSIONID EXPORT command: $ export cookie="Cookie: JSESSIONID=rudqwfhaqx0m1mchkau9ycqzs" --------------------------------------------------------------------------------------------------------
Những gì chúng tôi đã học được trong Bước 1
- Thông báo đăng nhập thành công sẽ trả về {"retMsg". "op. thành công"} và một JSESSIONID mới
- JSESSIONID này cần được sử dụng cho yêu cầu HTTP/AJAX tiếp theo
- Ghi chú. Ở giai đoạn này, chúng tôi có thể tấn công trang này bằng phương pháp brute FORCE
Bước 2. Triển khai Chuyển hướng đăng nhập bằng bash/cURL
- Sau khi đăng nhập thành công, trang được chuyển hướng đến bảo mật. để đăng nhập. hoạt động
- Để xem lại Mã JS hoàn chỉnh Bước 1
if [res.retMsg == "op.successfully"] { window.location.href = newPath + "securitys!tologin.action"; return;
Sử dụng các công cụ Google Dev để trích xuất Mã bash cURL
- Đăng nhập vào trang web
- Điều hướng đến tab Mạng
- Xác định vị trí bảo mật. để đăng nhập. yêu cầu hành động
- Chọn “Sao chép” -> “Sao chép dưới dạng cURL [bash]”
Dán mã vào bash Shell
$ curl '//52.58.164.53:8443/securitys!tologin.action' -H 'Pragma: no-cache' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.9' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 [Windows NT 6.1; Win64; x64] AppleWebKit/537.36 [KHTML, like Gecko] Chrome/62.0.3202.94 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'Referer: //52.58.164.53:8443/index.action' -H 'Cookie: JSESSIONID=1b0miyons0sfl1uqxsbhdv0zud' -H 'Connection: keep-alive' -H 'Cache-Control: no-cache' --compressed --insecure % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
NetEco 1000S
Hãy đặc biệt chú ý đến _crsf thẻ meta HTML được trả về từ yêu cầu cURL ở trên
- Lưu ý CRSF Tojne này sẽ được sử dụng để truy cập Trang web
meta name="_csrf" content="508a5c53-6d3d-4ed1-b56e-e292fcf0a2dc"
meta name="_csrf_header" content="X-CSRF-TOKEN"
Xây dựng tập lệnh WRLogin2. sh để mô phỏng chuyển hướng Trang
var verifyResult; $["#login"].click[function[] { if [!allowLogin] { jAlert[browserMessage, Message.alarm_info, '{"' + Message.sur + '":"OK"}']; clearPassword[]; return; } var userName = $.trim[$["#userName"].val[]]; var password = $["#password"].val[]; if [!loginValidate[userName, password]] { clearPassword[]; return; } var showVerifyCode = document.getElementById["verifyCode_tr"].style.display; comitLogin[userName, password]; }];0
Tập lệnh kiểm tra WRLogin2. sh để mô phỏng chuyển hướng Trang
- WRlogin2. sh trích xuất X-CSRF-TOKEN từ thẻ Meta HTML
- Cùng với JSESSIONID trở lại từ WRLogin. sh Thông tin này được sử dụng để truy cập WEBSite mục tiêu của chúng tôi
var verifyResult; $["#login"].click[function[] { if [!allowLogin] { jAlert[browserMessage, Message.alarm_info, '{"' + Message.sur + '":"OK"}']; clearPassword[]; return; } var userName = $.trim[$["#userName"].val[]]; var password = $["#password"].val[]; if [!loginValidate[userName, password]] { clearPassword[]; return; } var showVerifyCode = document.getElementById["verifyCode_tr"].style.display; comitLogin[userName, password]; }];1
Bước 3. Cuối cùng Triển khai Truy cập Trang bằng bash/cURL
- Đối với kịch bản này, chúng tôi cần. JSESSIONID được trả về từ WRlogin. sh
- X-CSRF-TOKEN được trả về từ WAR login2,sh
Làm theo các bước ở trên để giải nén lệnh bash cURL
var verifyResult; $["#login"].click[function[] { if [!allowLogin] { jAlert[browserMessage, Message.alarm_info, '{"' + Message.sur + '":"OK"}']; clearPassword[]; return; } var userName = $.trim[$["#userName"].val[]]; var password = $["#password"].val[]; if [!loginValidate[userName, password]] { clearPassword[]; return; } var showVerifyCode = document.getElementById["verifyCode_tr"].style.display; comitLogin[userName, password]; }];2
Xây dựng tập lệnh bash shell WRData. sh để mô phỏng Truy cập trang web
var verifyResult; $["#login"].click[function[] { if [!allowLogin] { jAlert[browserMessage, Message.alarm_info, '{"' + Message.sur + '":"OK"}']; clearPassword[]; return; } var userName = $.trim[$["#userName"].val[]]; var password = $["#password"].val[]; if [!loginValidate[userName, password]] { clearPassword[]; return; } var showVerifyCode = document.getElementById["verifyCode_tr"].style.display; comitLogin[userName, password]; }];3
Kiểm tra tập lệnh bash shell WRData. sh để truy cập trang web mục tiêu của chúng tôi
var verifyResult; $["#login"].click[function[] { if [!allowLogin] { jAlert[browserMessage, Message.alarm_info, '{"' + Message.sur + '":"OK"}']; clearPassword[]; return; } var userName = $.trim[$["#userName"].val[]]; var password = $["#password"].val[]; if [!loginValidate[userName, password]] { clearPassword[]; return; } var showVerifyCode = document.getElementById["verifyCode_tr"].style.display; comitLogin[userName, password]; }];4
Thử nghiệm cuối cùng của Tập lệnh Shell đã phát triển của chúng tôi
var verifyResult; $["#login"].click[function[] { if [!allowLogin] { jAlert[browserMessage, Message.alarm_info, '{"' + Message.sur + '":"OK"}']; clearPassword[]; return; } var userName = $.trim[$["#userName"].val[]]; var password = $["#password"].val[]; if [!loginValidate[userName, password]] { clearPassword[]; return; } var showVerifyCode = document.getElementById["verifyCode_tr"].style.display; comitLogin[userName, password]; }];5