Advertisement
Guest User

test

a guest
May 24th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.49 KB | None | 0 0
  1. // SHA
  2. //#include <sha256.h>
  3. //#include <sha256_config.h>
  4. #include <sha1.h>
  5. #include <printf.h>
  6. #include <sha1_config.h>
  7.  
  8. #include <TimeLib.h>
  9. #include <Ethernet.h>
  10. #include <EthernetUdp.h>
  11. #include <Wire.h>
  12. #include <SPI.h>
  13. #include <Adafruit_PN532.h>
  14.  
  15. // Connection
  16. byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0X02 };
  17. IPAddress ip(192, 168, 216, 50);
  18. //IPAddress timeServer(64, 99, 80, 121);
  19. IPAddress timeServer(202, 90, 132, 242);
  20. const int timeZone = 8;
  21. EthernetClient client;
  22. EthernetUDP Udp;
  23. unsigned int localPort = 8888;
  24. char server[] = "192.168.216.51";
  25.  
  26.  
  27. // If using the breakout with SPI, define the pins for SPI communication.
  28. #define PN532_IRQ (2)
  29. #define PN532_RESET (3)
  30.  
  31. Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
  32.  
  33. #if defined(ARDUINO_ARCH_SAMD)
  34. // for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
  35. // also change #define in Adafruit_PN532.cpp library file
  36. #define Serial SerialUSB
  37. #endif
  38.  
  39. void setup(void) {
  40. Serial.begin(9600);
  41. Serial.println("Hello!");
  42.  
  43. nfc.begin();
  44.  
  45. // NTP
  46. Ethernet.begin(mac, ip);
  47. if(Ethernet.begin(mac)==1){
  48. Serial.println("[+] Successfully connected");
  49. }
  50. Serial.println("waiting for sync");
  51. Udp.begin(localPort);
  52. setSyncInterval(43200); // Once every 30 minutes to sync
  53. //setSyncProvider(getNtpTime);
  54. setTime(getNtpTime());
  55.  
  56.  
  57. uint32_t versiondata = nfc.getFirmwareVersion();
  58. if (! versiondata) {
  59. Serial.print("Didn't find PN53x board");
  60. while (1); // halt
  61. }
  62. // Got ok data, print it out!
  63. Serial.print("Found chip PN5");
  64. Serial.println((versiondata>>24) & 0xFF, HEX);
  65. Serial.print("Firmware ver. ");
  66. Serial.print((versiondata>>16) & 0xFF, DEC);
  67. Serial.print('.');
  68. Serial.println((versiondata>>8) & 0xFF, DEC);
  69.  
  70. // configure board to read RFID tags
  71. nfc.SAMConfig();
  72.  
  73. Serial.println("Waiting for an ISO14443A Card ...");
  74.  
  75. }
  76.  
  77. time_t prevDisplay = 0; // when the digital clock was displayed
  78.  
  79.  
  80.  
  81.  
  82.  
  83. void loop(void) {
  84. uint8_t success;
  85. uint8_t uid[] = {
  86. 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
  87. uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
  88.  
  89. success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 10000);
  90.  
  91. // NTP LOOP
  92.  
  93. if (success) {
  94. String time = urlencode(digitalClockDisplay());
  95. /*Serial.println("Found an ISO14443A card");
  96. Serial.print(" UID Length: ");
  97. Serial.print(uidLength, DEC);
  98. Serial.println(" bytes");
  99. Serial.print(" UID Value: ");
  100. nfc.PrintHex(uid, uidLength);*/
  101. uint32_t cardid = uid[0];
  102.  
  103. if (uidLength == 4)
  104. {
  105. // We probably have a Mifare Classic card ...
  106. //uint32_t cardid = uid[0]; M
  107. cardid <<= 8;
  108. cardid |= uid[1];
  109. cardid <<= 8;
  110. cardid |= uid[2];
  111. cardid <<= 8;
  112. cardid |= uid[3];
  113. //Serial.print("Seems to be a Mifare Classic card #");
  114. //Serial.println(cardid);
  115. //delay(2000);
  116.  
  117. }
  118. //abcdefghijklmopqrstuvwxyz
  119. // Web Request
  120. if(client.connect(server,80)){
  121. Serial.println("t="+time);
  122. Serial.println("s="+(String)cardid);
  123. Sha1.init();
  124. Sha1.print((String)cardid+"abc");
  125. String hashed = GetHash(Sha1.result());
  126. String request = "GET /index.php?t="+time+"&s="+(String)cardid+"&c="+hashed+" HTTP/1.1";
  127. Serial.println(request);
  128. client.println("Host: 192.168.216.51");
  129. client.println("Connection: close");
  130. client.println();
  131. }
  132. Serial.println("");
  133. while(nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 100)){
  134. }
  135. }
  136. else
  137. {
  138.  
  139. }
  140. while (client.available()) {
  141. char c = client.read();
  142. if(c == '\n') Serial.println();
  143. Serial.print(c);
  144. }
  145. resetEthernet(); // Only reset the ethernet settings when we are able to print request
  146. }
  147.  
  148. //Crypto
  149. String GetHash(uint8_t* hash) {
  150. char tmp[16];
  151. String sHash256 = "";
  152. int i;
  153. for (i=0; i<20; i++) {
  154. sprintf(tmp, "%.2x",hash[i]);
  155. sHash256.concat(tmp);
  156. }
  157. //Serial.println(sHash256);
  158. return sHash256;
  159. }
  160.  
  161. void resetEthernet(){
  162. client.stop();
  163. Ethernet.begin(mac, ip);
  164. }
  165.  
  166. /*-------- NTP code ----------*/
  167.  
  168. const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
  169. byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
  170.  
  171. time_t getNtpTime()
  172. {
  173. while (Udp.parsePacket() > 0) ; // discard any previously received packets
  174. Serial.println("Transmit NTP Request");
  175. sendNTPpacket(timeServer);
  176. uint32_t beginWait = millis();
  177. while (millis() - beginWait < 1500) {
  178. int size = Udp.parsePacket();
  179. if (size >= NTP_PACKET_SIZE) {
  180. Serial.println("Receive NTP Response");
  181. Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
  182. unsigned long secsSince1900;
  183. // convert four bytes starting at location 40 to a long integer
  184. secsSince1900 = (unsigned long)packetBuffer[40] << 24;
  185. secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
  186. secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
  187. secsSince1900 |= (unsigned long)packetBuffer[43];
  188. return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
  189. }
  190. }
  191. Serial.println("No NTP Response :-(");
  192. return 0; // return 0 if unable to get the time
  193. }
  194.  
  195. // send an NTP request to the time server at the given address
  196. void sendNTPpacket(IPAddress &address)
  197. {
  198. // set all bytes in the buffer to 0
  199. memset(packetBuffer, 0, NTP_PACKET_SIZE);
  200. // Initialize values needed to form NTP request
  201. // (see URL above for details on the packets)
  202. packetBuffer[0] = 0b11100011; // LI, Version, Mode
  203. packetBuffer[1] = 0; // Stratum, or type of clock
  204. packetBuffer[2] = 6; // Polling Interval
  205. packetBuffer[3] = 0xEC; // Peer Clock Precision
  206. // 8 bytes of zero for Root Delay & Root Dispersion
  207. packetBuffer[12] = 49;
  208. packetBuffer[13] = 0x4E;
  209. packetBuffer[14] = 49;
  210. packetBuffer[15] = 52;
  211. // all NTP fields have been given values, now
  212. // you can send a packet requesting a timestamp:
  213. Udp.beginPacket(address, 123); //NTP requests are to port 123
  214. Udp.write(packetBuffer, NTP_PACKET_SIZE);
  215. Udp.endPacket();
  216. }
  217.  
  218. String digitalClockDisplay(){
  219. String t;
  220. t += (String)year()+ "-" + (String)printDigits(month()) + "-" + (String)printDigits(day());
  221. t += " ";
  222. t += printHour(hour()) + ":" + printDigits(minute()) + ":" + printDigits(second());
  223. return t;
  224. }
  225.  
  226. String printDigits(int digits){
  227. if(digits < 10)
  228. return "0"+(String)digits;
  229.  
  230. return (String)digits;
  231.  
  232. }
  233.  
  234. String printHour(int digits){
  235. unsigned int iHour;
  236.  
  237. if(digits > 12 ){
  238. iHour = digits - 12;
  239. return "0"+(String)iHour;
  240. }
  241. if(digits < 10){
  242. return "0"+String(digits);
  243. }
  244.  
  245. return (String)digits;
  246. }
  247.  
  248. String urldecode(String str)
  249. {
  250.  
  251. String encodedString="";
  252. char c;
  253. char code0;
  254. char code1;
  255. for (int i =0; i < str.length(); i++){
  256. c=str.charAt(i);
  257. if (c == '+'){
  258. encodedString+=' ';
  259. }else if (c == '%') {
  260. i++;
  261. code0=str.charAt(i);
  262. i++;
  263. code1=str.charAt(i);
  264. c = (h2int(code0) << 4) | h2int(code1);
  265. encodedString+=c;
  266. } else{
  267.  
  268. encodedString+=c;
  269. }
  270.  
  271. //yield();
  272. }
  273.  
  274. return encodedString;
  275. }
  276.  
  277. String urlencode(String str)
  278. {
  279. String encodedString="";
  280. char c;
  281. char code0;
  282. char code1;
  283. char code2;
  284. for (int i =0; i < str.length(); i++){
  285. c=str.charAt(i);
  286. if (c == ' '){
  287. encodedString+= '+';
  288. } else if (isalnum(c)){
  289. encodedString+=c;
  290. } else{
  291. code1=(c & 0xf)+'0';
  292. if ((c & 0xf) >9){
  293. code1=(c & 0xf) - 10 + 'A';
  294. }
  295. c=(c>>4)&0xf;
  296. code0=c+'0';
  297. if (c > 9){
  298. code0=c - 10 + 'A';
  299. }
  300. code2='\0';
  301. encodedString+='%';
  302. encodedString+=code0;
  303. encodedString+=code1;
  304. //encodedString+=code2;
  305. }
  306. //yield();
  307. }
  308. return encodedString;
  309.  
  310. }
  311.  
  312. unsigned char h2int(char c)
  313. {
  314. if (c >= '0' && c <='9'){
  315. return((unsigned char)c - '0');
  316. }
  317. if (c >= 'a' && c <='f'){
  318. return((unsigned char)c - 'a' + 10);
  319. }
  320. if (c >= 'A' && c <='F'){
  321. return((unsigned char)c - 'A' + 10);
  322. }
  323. return(0);
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement