Advertisement
tchnmncr

HackArtDemo1.pde

Feb 20th, 2017
14,895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. /**
  2. * HACK.ART Demo 1
  3. * by tchnmncr @ hyperritual.com
  4. *
  5. * This is a Processing sketch demonstrating interaction with
  6. * Hak5's WiFi Pineapple and Arduino.
  7. * For more info see
  8. * http://hyperritual.com/blog/hack-art-wifi-pineapple-processing-arduino.
  9. *
  10. * Made with Processing 3.2.1.
  11. */
  12.  
  13. import http.requests.*;
  14. import processing.serial.*;
  15.  
  16. String API_TOKEN = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  17. PostRequest startScan, // POST to initiate scan
  18. scanStatus; // POST to retrieve scan results
  19. Serial arduino; // Arduino serial port
  20.  
  21. void setup() {
  22. size(640, 640);
  23. background(50);
  24. fill(200);
  25. noStroke();
  26. noLoop(); // I am running through the draw() loop once only and using delay(); in reality you would probably
  27. // repeat through it with a timer and use millis() to create a delay between scans
  28.  
  29. String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  30. arduino = new Serial(this, portName, 9600);
  31. }
  32.  
  33. void draw() {
  34.  
  35. /*** Instruct the Pineapple to perform a 15-second scan for only access points (no clients) ***/
  36.  
  37. // build and send the POST request
  38. // {
  39. // "module": "Recon",
  40. // "action": "startScan",
  41. // "scanType": "apOnly",
  42. // "scanDuration": "15"
  43. // "apiToken": API_TOKEN
  44. // }
  45. PostRequest startScan = new PostRequest("http://172.16.42.1:1471/api/");
  46. startScan.addHeader("Content-Type", "application/json");
  47. startScan.addJson("{\"module\":\"Recon\",\"action\":\"startScan\",\"scanType\":\"apOnly\",\"scanDuration\":\"15\",\"apiToken\":\"" + API_TOKEN + "\"}");
  48. startScan.send();
  49.  
  50. // wait 20 seconds for scan to complete; I am only using this with noLoop();
  51. // otherwise, you could continue to send() until the response includes "completed":true
  52. delay(20000);
  53.  
  54. // get the POST response
  55. String startScanResponse = startScan.getContent();
  56. String startScanResponseClean = startScanResponse.substring(6); // strip off ")]}',"
  57.  
  58. // create a JSON object from the POST response
  59. JSONObject jsonStartScan = parseJSONObject(startScanResponseClean);
  60.  
  61. // check to see if scan was successful
  62. if (jsonStartScan.getBoolean("success") == false) {
  63. println("There was a problem requesting the scan.");
  64. return; // break out of draw() loop if scan failed
  65. }
  66.  
  67. // get scan ID from JSON object
  68. int scanId = jsonStartScan.getInt("scanID");
  69.  
  70.  
  71. /*** Retrieve scan results from the Pineapple ***/
  72.  
  73. // build and send the POST request
  74. // {
  75. // "module": "Recon",
  76. // "action": "scanStatus",
  77. // "scanID": scanId,
  78. // "apiToken": API_TOKEN
  79. // }
  80. PostRequest scanStatus = new PostRequest("http://172.16.42.1:1471/api/");
  81. scanStatus.addHeader("Content-Type", "application/json");
  82. scanStatus.addJson("{\"module\":\"Recon\",\"action\":\"scanStatus\",\"scanID\":" + scanId + ",\"apiToken\":\"" + API_TOKEN + "\"}");
  83. scanStatus.send();
  84.  
  85. // get the POST response
  86. String scanStatusResponse = scanStatus.getContent();
  87. String scanStatusResponseClean = scanStatusResponse.substring(6); // strip off ")]}',"
  88.  
  89. // create a JSON object from the POST response
  90. JSONObject jsonScanStatus = parseJSONObject(scanStatusResponseClean);
  91.  
  92. // further parse the JSON data
  93. JSONObject results = jsonScanStatus.getJSONObject("results");
  94. JSONArray apList = results.getJSONArray("ap_list");
  95.  
  96. // write the BSSID (MAC) and power (dBm) values to arrays
  97. String[] bssid = {};
  98. int[] power = {};
  99. for (int i = 0; i < apList.size(); i++) {
  100.  
  101. JSONObject ap = apList.getJSONObject(i);
  102.  
  103. bssid = append(bssid, ap.getString("bssid"));
  104. power = append(power, ap.getInt("power"));
  105. }
  106.  
  107.  
  108. /*** Draw rectangles representing access points ***/
  109.  
  110. // calculate height of all rectangles according to total # of access points
  111. float h = (height * 1.0) / bssid.length;
  112.  
  113. for (int i = 0; i < bssid.length; i++) {
  114.  
  115. // set rectangle's color based on first three octets of MAC
  116. int r = unhex(bssid[i].substring(0, 2));
  117. int g = unhex(bssid[i].substring(3, 5));
  118. int b = unhex(bssid[i].substring(6, 8));
  119. fill(r, g, b);
  120.  
  121. // calculate length of rectangle according to its power (in dBm)
  122. float l = map(100 + power[i], 0, 100, 0, width);
  123.  
  124. rect(0, h * i, 100 + l, h);
  125. }
  126.  
  127.  
  128. /*** Send a signal to Arduino if target BSSID was found in scan ***/
  129.  
  130. String target = "00:FF:00:FF:00:FF"; // replace with target BSSID
  131. boolean targetFound = false;
  132.  
  133. // loop through bssid array, searching for target
  134. for (int i = 0; i < bssid.length; i++) {
  135.  
  136. if (bssid[i].equals(target)) {
  137. targetFound = true;
  138. break;
  139. }
  140. }
  141.  
  142. if (targetFound == true) {
  143. arduino.write('1');
  144. println("Target found!");
  145. } else {
  146. arduino.write('0');
  147. }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement