TolentinoCotesta

Get Host fingerprint

Jan 14th, 2022 (edited)
1,117
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2.  
  3. #ifndef STASSID
  4. #define STASSID "xxxxxxx"
  5. #define STAPSK  "xxxxxxx"
  6. #endif
  7.  
  8.  
  9. const char* host = "api.telegram.org";
  10.  
  11. const char* ssid     = STASSID;
  12. const char* password = STAPSK;
  13.  
  14.  
  15. void getFingerprint(const char* host, uint8_t* fingerprint = nullptr) {
  16.   WiFiClientSecure client;
  17.   client.setInsecure();
  18.  
  19.   // Example "F2:AD:29:9C:34:48:DD:8D:F4:CF:52:32:F6:57:33:68:2E:81:C1:90"
  20.   char fingerPrintStr[59];
  21.   char request[128];
  22.   snprintf(request, sizeof(request), "GET /fingerprints.htm?chain=%s", host);
  23.  
  24.   if (!client.connect("www.grc.com", 443)) {
  25.     Serial.println("connection failed");
  26.     delay(1000);
  27.     return;
  28.   }
  29.  
  30.   // Make a HTTP request:
  31.   client.println(request);
  32.   client.println("Host: www.grc.com");
  33.   client.println("Connection: close");
  34.   client.println();
  35.   delay(500);
  36.  
  37.   if (client.available()) {
  38.     char searchKey[80];
  39.     snprintf(searchKey, sizeof(searchKey), "<td class=\"ledge\">%s</td>", host);
  40.     if (client.find(searchKey)) {
  41.       // Find next table cell where fingerprint string is placed
  42.       if (client.find("<td>") ) {
  43.         client.readBytes(fingerPrintStr, 59);
  44.       }
  45.     }
  46.  
  47.     // Clear RX buffer
  48.     while (client.available()) {
  49.       client.read();
  50.     }
  51.     client.stop();
  52.   }
  53.  
  54.   // Copy bytes in fingerprint array if requested
  55.   if (fingerprint != nullptr) {
  56.     uint8_t i = 0;
  57.     for (char * pch = strtok(fingerPrintStr, ":"); pch != NULL; pch = strtok(NULL, ":")) {
  58.       if (pch != NULL) {
  59.         fingerprint[i++] = (uint8_t)strtol(pch, NULL, 16);
  60.       }
  61.     }
  62.   }
  63. }
  64.  
  65.  
  66. void setup() {
  67.   Serial.begin(115200);
  68.   Serial.print("\n\nConnecting to ");
  69.   Serial.println(ssid);
  70.  
  71.   WiFi.mode(WIFI_STA);
  72.   WiFi.begin(ssid, password);
  73.  
  74.   while (WiFi.status() != WL_CONNECTED) {
  75.     delay(500);
  76.     Serial.print(".");
  77.   }
  78.  
  79.   Serial.print("\nWiFi connected. \nIP address: ");
  80.   Serial.println(WiFi.localIP());
  81.  
  82.   Serial.print("\nGet fingerprint for host ");
  83.   Serial.print(host);
  84.   Serial.println(": ");
  85.  
  86.   uint8_t fingerprint[20];
  87.   getFingerprint(host, fingerprint);
  88.  
  89.   for(int i=0; i<sizeof(fingerprint); i++) {
  90.     Serial.print(fingerprint[i], HEX);
  91.     (i < sizeof(fingerprint) -1)  ? Serial.print(":") : Serial.println();
  92.   }
  93. }
  94.  
  95. void loop() {
  96.  
  97. }
RAW Paste Data