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é
Các bài viết liên quan
Add “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 nay
10/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 – KNTT
10/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["//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["//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["//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["//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["//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["//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 đặt
Vì 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["//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["//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["//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["//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
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["//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. Lệnh này sẽ thực hiện yêu cầu GET to page webimport requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup[public_ip]: r = requests.get["//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!"]
6. Lệnh này sẽ thực hiện yêu cầu POST đến URL và dữ liệu của nội dung bài viết có thể được chuyển sang định dạng từ điển
Lưu ý. Các phương thức yêu cầu khác nhau sẽ được thực hiện theo cách tương tự như yêu cầu. đầu, yêu cầu. đặt,…
Để 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["//google.com"]
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["//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. Lệnh này sẽ trả về mã phản hồi đã nhận được từ yêu cầu như 200, 404, 301,…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["//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!"]
8. Lệnh này sẽ trả về dữ liệu bạn đã nhận được từ một trang webimport requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup[public_ip]: r = requests.get["//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!"]
9. Lệnh này sẽ lấy dữ liệu đã phản hồi từ trang web dưới dạng từ điển
Argument for the method request
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["//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!"]
10. Lệnh này được sử dụng để đặt thời gian chờ cho một yêu cầuimport requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup[public_ip]: r = requests.get["//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!"]
11. Lệnh này được sử dụng để chỉ định hướng chuyển hướng có thể được phép hoặc không 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["//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!"]
12 sẽ cho phép các yêu cầu chuyển hướngimport requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup[public_ip]: r = requests.get["//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!"]
13. Lệnh này sẽ hiển thị dạng mã hóa của dữ liệu nhận đượcimport requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup[public_ip]: r = requests.get["//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!"]
14. Lệnh này sẽ chuyển cookie đến yêu cầu phiênimport requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup[public_ip]: r = requests.get["//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!"]
15. Lệnh này sẽ được sử dụng để cung cấp tiêu đề cho yêu cầu phiên
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
Lưu ý. Đây sẽ là một yêu cầu rất cơ bản, nhưng dù sao nó cũng sẽ giúp bạn hiểu cách thu thập dữ liệu bằng Python. Mình đang sử dụng API này để nhận thông tin chung về địa chỉ IP
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["//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 động
Trê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["//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["//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["//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["//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["//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["//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ục
Bâ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 write
Chú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["//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["//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["//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["//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["//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["//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["//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. read modeimport requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup[public_ip]: r = requests.get["//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. ghi modeimport requests import json # Thư viện dùng để định dạng dữ liệu nhận được def iplookup[public_ip]: r = requests.get["//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!"]
39. Chế độ kết nối [Khi mở tệp, vị trí con trỏ sẽ luôn ở cuối 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["//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!"]
30. Chế độ đọc và ghi
Lưu ý. Việc bổ sung
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["//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 vào một chế độ sẽ mở tệp ở chế độ hoạt động nhị phân, tức là tất cả nội dung của tệp sẽ được coi là byte đối tượng 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["//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 sẽ đọc tệp ở định dạng nhị phâ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["//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 động
Chú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["//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["//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["//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["//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["//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["//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["//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ết
Bà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