יום שבת, 26 בינואר 2013

Arduino Ethernet HoneyPot



ראינו איך ניתן לתקשר עם ה Arduino בצורה סיראלית וכמו שהבטחתי הגיע הזמן לשלב תקשורות נוספות, בסדרת המאמרים הקרובה נתמקד בתקשורות שונות בסביבת Arduino, התקשורת הראשונה שפותחת את הסדרה היא איך לא Ethernet , בעזרת ה Ethernet Shield ניתן לשלב את ה Arduino בכל רשת מבוססת LAN, ולבצע מגוון משימות כמו WebServer , DHCP וכדומה.

דרישות:
  • Arduino Uno
  • Ethernet Shield


Ethernet Shield


את שלי מצאתי באינטרנט במחיר של 20 דולר עם כניסת Micro SD וצ'יפ של Wizent, כל IDE של Arduino מגיע עם ספריית Ethernet עם המון דוגמאות שמתאימות ל Shield שרכשתי, אני לא אתמקד בדוגמאות הבסיסיות (יש מספיק) אלא במשהו קצת יותר מורכב שאפשר לנו להפוך את ה Arduino לכלי אבטחת מידע.

HoneyPot

אחד מהכלים המעניינים באבטחת מידע, נקודה ברשת שבעצם "מלכודת" שנועדה להתריע לנו כאשר מתבצעת תנועה לא לגיטימית ברשת, זו נקודה שאף אחד לא צריך להגיע אליה אבל במקרה שמגיעים נדע זאת מיד ,פה העסק נהפך למסובך, ספריית ה Ethernet שמגיעה עם ה IDE לא חושפת את כתובת ה Client כלומר נדע שנכנסו ל HoneyPot אבל לא נדע מי (כתובת IP) ולכן עלינו לחשוף אותה בעצמנו, כל הספריות החיצוניות של Arduino כתובות ב C++ ובעצם עושות את כל הקסם מאחורי הקלעים, מרבית ה Shields נעזרים בספריות שמגיעות עם ה IDE אבל לפעמים יש להוריד ספריות ייעדיות.

חשוב מאוד! - לגבות את תיקיית ה Ethernet בתיקיית Libraries בתיקיית השורש ב IDE שלכם.

יש לערוך את הקובץ Client.cpp בתיקיית ה Ethernet ולהוסיף את הפונקציה בסוף הקובץ:

uint8_t * Client::getip(uint8_t dstip[])
{
W5100.readSnDIPR(_sock, dstip);
return dstip;
}

יש לערוך את הקובץ Client.h ולהוסיף את החתימה של הפונקציה:

  uint8_t * getip(uint8_t dstip[]);


אחרי שעדכנו הספרייה נעבור לקוד ב Arduino, התוכנית מבוססת על הדוגמה של ה Web Server שמגיעה עם ה IDE , הרעיון מאוד פשוט, נאזין לפורט 80 וכאשר שמשהו יגיע נעביר הודעה בעזרת החיבור הסיראלי למנהל המערכת ובמקביל נציג דף אזהרה למי שנפל למלכודת.

קוד:


/*
  Web  Server

 A simple web server that shows the value of the analog input pins.
 using an Arduino Wiznet Ethernet shield. 

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)

 created 18 Dec 2009
 by David A. Mellis
 modified 4 Sep 2010
 by Tom Igoe

 */


#include <Client.h>
#include <Ethernet.h>
#include <Server.h>
#include <Udp.h>

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1, 177 };

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
Server server(80);

void setup()
{
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.begin(9600);
}

void loop()
{
  // listen for incoming clients
  Client client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
         
         //create byte array for dstip
         byte dstip[] = {0,0,0,0 };
         //getting the ip from the upgraded library
         client.getip(dstip);
          
          client.print("<body style='background-color:black'>");
          client.print("<div style='color:red; font-family:arial;font-size:24px; font-weghit:bold'>Warning</div>");
         client.print("<div style='color:white'><b>");
         client.print(dstip[0],DEC);
         client.print(".");
         client.print(dstip[1],DEC);
         client.print(".");
         client.print(dstip[2],DEC);
         client.print(".");
         client.print(dstip[3],DEC);
         client.print("</b>");
         client.print(" You are not supposed to be in this parts of the network,  message send to the administrator");
         client.print("</div>");
         client.print("<br />");
         client.print("<div style='font-size:24px;color:#007eff'>DuinoPot v.1</div>");
         client.print("<div style='font-size:12px;color:white'>proxytype.blogspot.com</div>");
         client.print("</ body>");
         
         //send serial message to administrator
        Serial.print("CONNECTION FROM --> ");
        Serial.print(dstip[0],DEC);
        Serial.print(".");
        Serial.print(dstip[1],DEC);
        Serial.print(".");
        Serial.print(dstip[2],DEC);
        Serial.print(".");
        Serial.print(dstip[3],DEC);
        Serial.print("\n");
        
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}




אז כמו שאמרתי כאשר משהו נופל לפח מוצג לו הדף הבא:


במקביל נשלחת הודעה סיראלית למנהל המערכת, ניתן לשמור את המידע על הגבי ה Micro SD אבל נשאיר את זה לפעם אחרת.




סיכום:

עבודה עם תקשורת Ethernet מאפשרת להרחיב את הפרוייקטים לתחומים רחבים יותר כמו האינטרנט, ובעצם להוציא את ה Arduino לעולם הגדול.