Python yêu cầu cookie
Tiếp nối bài viết trước đó là tạo cổng quét (scan port) bằng python. Trong bài viết này, mình sẽ hướng dẫn các bạn làm quen với các yêu cầu thư viện trong python và cách ứng dụng thư viện đó trong an ninh mạng nhé Show
Các bài viết liên quanAdd “tin nhắn bí mật” vào hình ảnh bằng Python. mật thư17/11/2022 - Cập nhật ngày 18/11/2022 Tải xuống Khóa học Nhà phát triển Python hoàn chỉnh vào năm 2023. Không để làm chủ02/11/2022 Các trình biên dịch Python trực tuyến tốt nhất hiện nay10/08/2022 Phần mềm Dạy và Học môn Tin Học theo bộ Kết nối tri thức – KNTT10/07/2022 Yêu cầu thư việnĐối phó với các yêu cầu HTTP không phải là một nhiệm vụ dễ dàng trong bất kỳ ngôn ngữ lập trình nào. Nếu nói về Python, nó đi kèm với hai thư viện tích hợp sẵn là import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")4 và import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")5, để xử lý các hoạt động liên quan đến HTTP. Cả hai thư viện đi kèm này đều có một tập hợp các hàm (chức năng) khác nhau và nhiều khi cần sử dụng giống nhau. Chế độ chính của việc sử dụng import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")4 rất dễ gây nhầm lẫn (một số phương thức có sẵn trong cả import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")4, import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")5), tài liệu không rõ ràng và chúng ta cần viết rất nhiều mã để thực hiện một yêu cầu HTTP đơn giản Để làm cho công việc đơn giản hơn, một thư viện bên thứ ba dễ sử dụng đã ra đời, được gọi là import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")0, có sẵn và hầu hết các nhà phát triển đều thích sử dụng nó để thay thế urllib/urllib2. Nó là một thư viện HTTP của Apache2 và được cung cấp bởi urllib3 và httplib Cài đặtVì trong bài này sử dụng python3 nên mình sẽ sử dụng pip3 để cài đặt các thư viện của bên thứ ba Nhập lệnh sau vào Terminal import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")1 Lệnh import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")2 sẽ cài đặt thư viện import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")3 trên hệ thống của bạn Các khái niệm cơ bản về yêu cầuĐể bắt đầu sử dụng thư viện import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")3 của python, trước hết bạn cần phải nhập nó. Sau đây là các lệnh sẽ được sử dụng để tạo một chương trình thu thập dữ liệu từ một trang web hoặc API. Nhưng lần đầu tiên, bạn cần đọc bài viết này xác định cụm về cơ chế hoạt động của HTTP đã tồn tại
Để dễ hiểu, chúng ta hãy lấy một ví dụ về yêu cầu đối tượng >>>import requests >>>r = requests.get("https://google.com")
Argument for the method request
Bài tập 1. Tra cứu IPÁp dụng những kiến thức trên, chúng tôi sẽ thực hiện một yêu cầu đơn giản tới API tra cứu IP để thu thập thông tin cho mục tiêu của chúng tôi
import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!") Cách hoạt độngTrên dòng đầu tiên và thứ hai, chúng ta nhập 2 thư viện là import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")0 và import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")17. Sau đó, chúng ta tạo một hàm có tên import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")18, chứa tham số import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")19. Tiếp theo, chúng ta gửi yêu cầu tới API và xem nó đã thành công hay không thông qua import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")7. Chuyển dữ liệu nhận được sang định dạng json và từ điển rồi trong Terminal. Mình đã thêm import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")31 để xử lý bất kỳ lỗi nào có thể xảy ra Tập tin 2. Chặn thư mụcBây giờ, chúng ta hãy sử dụng một công cụ phức tạp hơn, nhưng rất hữu ích, một công cụ chặn thư mục. Nhưng trước khi tiếp tục, mình sẽ giải thích cho các bạn cách đọc và ghi tệp bằng Python File xử lý. Read and writeChúng ta cần đọc các tệp để thực hiện chặn thư mục bằng cách sử dụng tấn công từ điển. Trong python, chúng ta sử dụng hàm import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")32 là một hàm tổng hợp trả về một tệp đối tượng và có thể được sử dụng để mở tệp theo nhiều cách khác nhau như đọc, ghi và nối thêm Ví dụ import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")1 Chúng ta sử dụng hàm import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")32 có 2 tham số là đường dẫn của tệp và chế độ mở tệp. Trong ví dụ trên, import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")34 là đường dẫn tệp, còn import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")35 là chế độ đọc và sau đó mở tệp import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")36 để ghi dữ liệu Các file mode khác nhau
import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")3 Cách hoạt độngChúng ta đã tạo một hàm có tên là import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")33 với các tham số là import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")34 và import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")35, đây sẽ là tệp chứa danh sách thư mục để brute force trên trang web. Tiếp theo, mình cũng không quên sử dụng import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")31. Sau đó, mình mở tệp import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")35 bằng cách sử dụng import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")38 và đọc danh sách các từ có trong tệp, rồi tách chuỗi và giải mã, kết nối dữ liệu vào các url được định sẵn của mình, sau đó thêm các url và đường dẫn giống nhau . Cuối cùng, thực hiện yêu cầu GET tới url đã tạo và đầu ra của yêu cầu miễn phí là mã phản hồi không bằng 404 (có nghĩa là “Không tìm thấy”) Mình sẽ sử dụng tập lệnh này để hack một máy chủ trên TryHackMe MrRobotCTF và đây là kết quả import requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup(public_ip): r = requests.get("http://ip-api.com/json/"+public_ip) if r.status_code == 200: # Nếu thành công data = json.loads(r.text) # Chuyển dữ liệu nhận được vào json for key, value in (data).items(): # Định dạng dữ liệu json sang dictionary print("{}:{}".format(key, value)) else: # If error occurs print("Error Occured while making request") if __name__ == "__main__": try: ip = input("Enter IP: ") iplookup(ip) except: print("Error Occured!")3 Bạn cũng có thể phát triển máy chủ MrRobot và sử dụng tập lệnh này để lấy các thư mục của trang web Tổng kếtBài viết này sẽ cung cấp cho bạn đủ ý tưởng và kiến thức để tạo ra các công cụ của riêng bạn bằng Python3. Nhưng trong dự án thứ hai, bạn có thể thấy rằng chương trình chạy hơi lâu. Để giải quyết vấn đề đó, chúng ta có thể sử dụng đa luồng. Mình chưa bổ sung tính năng đó vì nó sẽ yêu cầu thêm một số thư viện và kiến thức khác nhau. Ngoài ra, bạn cũng có thể xem thêm nhiều bài viết về python tại đây |