Skip to content

Commit

Permalink
Create JSON API v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
exploitagency committed Mar 16, 2018
1 parent fd12869 commit a67d0dd
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 21 deletions.
158 changes: 158 additions & 0 deletions Source Code/esprfidtool/api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
void apiinfo(int prettify) {

FSInfo fs_info;
SPIFFS.info(fs_info);
String total;
total=fs_info.totalBytes;
String used;
used=fs_info.usedBytes;
String freespace;
freespace=fs_info.totalBytes-fs_info.usedBytes;

const size_t bufferSize = JSON_ARRAY_SIZE(5) + JSON_OBJECT_SIZE(3);
DynamicJsonBuffer jsonAPIbuffer(bufferSize);
JsonObject& apilog = jsonAPIbuffer.createObject();

apilog["Device"] = "ESP-RFID-Tool";
apilog["Firmware"] = version;
apilog["API"] = APIversion;
JsonObject& apifs = apilog.createNestedObject("File System");
apifs["Total Space"]=total;
apifs["Used Space"]=used;
apifs["Free Space"]=freespace;
apilog["Free Memory"] = String(ESP.getFreeHeap(),DEC);

String API_Response="";
if (prettify==1) {
apilog.prettyPrintTo(API_Response);
}
else {
apilog.printTo(API_Response);
}
server.send(200, "application/json", API_Response);
delay(50);
jsonAPIbuffer.clear();
}

void apilistlogs(int prettify) {
Dir dir = SPIFFS.openDir("/");
String FileList = "";
int logcount=0;

while (dir.next()) {
File f = dir.openFile("r");
String FileName = dir.fileName();
if((!FileName.startsWith("/payloads/"))&&(!FileName.startsWith("/esploit.json"))&&(!FileName.startsWith("/esportal.json"))&&(!FileName.startsWith("/esprfidtool.json"))&&(!FileName.startsWith("/config.json"))) {
logcount++;
}
f.close();
}

const size_t bufferSize = JSON_ARRAY_SIZE(5) + JSON_OBJECT_SIZE(1);
DynamicJsonBuffer jsonAPIbuffer(bufferSize);
JsonObject& apilog = jsonAPIbuffer.createObject();

apilog["Device"] = "ESP-RFID-Tool";
apilog["Firmware"] = version;
apilog["API"] = APIversion;
apilog["Log Count"] = logcount;

int currentlog=0;
Dir dir2ndrun = SPIFFS.openDir("/");
while (dir2ndrun.next()) {
File f = dir2ndrun.openFile("r");
String FileName = dir2ndrun.fileName();
if ((!FileName.startsWith("/payloads/"))&&(!FileName.startsWith("/esploit.json"))&&(!FileName.startsWith("/esportal.json"))&&(!FileName.startsWith("/esprfidtool.json"))&&(!FileName.startsWith("/config.json"))) {
currentlog++;
JsonObject& apilistlogs = apilog.createNestedObject(String(currentlog));
apilistlogs["File Name"]=FileName;
}
f.close();
}

String API_Response="";
if (prettify==1) {
apilog.prettyPrintTo(API_Response);
}
else {
apilog.printTo(API_Response);
}
server.send(200, "application/json", API_Response);
delay(50);
jsonAPIbuffer.clear();
}

void apilog(String logfile,int prettify) {
File f = SPIFFS.open(String()+"/"+logfile, "r");
if (!f) {
server.send(200, "application/json", "Log file not found");
delay(50);
}
else {
int apiCAPTUREcount=0;
while(f.available()) {
String line = f.readStringUntil('\n');
if(line.indexOf(",Binary:") > 0) {
apiCAPTUREcount++;
int firstIndex = line.indexOf(",Binary:");
int secondIndex = line.indexOf(",", firstIndex + 1);
String binaryCaptureLINE=line.substring(firstIndex+8, secondIndex);
}
}
f.close();
const size_t bufferSize = JSON_ARRAY_SIZE(6) + JSON_OBJECT_SIZE(4);
DynamicJsonBuffer jsonAPIbuffer(bufferSize);
JsonObject& apilog = jsonAPIbuffer.createObject();

apilog["Device"] = "ESP-RFID-Tool";
apilog["Firmware"] = version;
apilog["API"] = APIversion;
apilog["Log File"] = logfile;
apilog["Captures"] = apiCAPTUREcount;

int apiCURRENTcapture=0;
File f = SPIFFS.open(String()+"/"+logfile, "r");
while(f.available()) {
String line = f.readStringUntil('\n');

if(line.indexOf(",Binary:") > 0) {
apiCURRENTcapture++;
int firstIndex = line.indexOf(",Binary:");
int secondIndex = line.indexOf(",", firstIndex + 1);
String binaryCaptureLINE=line.substring(firstIndex+8, secondIndex);
if ( binaryCaptureLINE.indexOf(" ") > 0 ) {
binaryCaptureLINE=binaryCaptureLINE.substring(binaryCaptureLINE.indexOf(" ")+1);
}
binaryCaptureLINE.replace("\r","");
JsonObject& apiCURRENTcaptureOBJECT = apilog.createNestedObject(String(apiCURRENTcapture));
apiCURRENTcaptureOBJECT["Bit Count"]=binaryCaptureLINE.length();
apiCURRENTcaptureOBJECT["Binary"]=binaryCaptureLINE;
if(line.indexOf(",HEX:") > 0) {
int hfirstIndex = line.indexOf(",HEX:");
int hsecondIndex = line.indexOf(",", hfirstIndex + 1);
String hexCURRENT=line.substring(hfirstIndex+5, hsecondIndex);
hexCURRENT.replace("\r","");
apiCURRENTcaptureOBJECT["Hexidecimal"]=hexCURRENT;
}
if(line.indexOf(",Keypad Code:") > 0) {
int kfirstIndex = line.indexOf(",Keypad Code:");
int ksecondIndex = line.indexOf(",", kfirstIndex + 1);
String pinCURRENT=line.substring(kfirstIndex+13, ksecondIndex);
pinCURRENT.replace("\r","");
apiCURRENTcaptureOBJECT["Keypad Press"]=pinCURRENT;
}
}
}
f.close();
String API_Response="";
if (prettify==1) {
apilog.prettyPrintTo(API_Response);
}
else {
apilog.printTo(API_Response);
}
server.send(200, "application/json", API_Response);
delay(50);
jsonAPIbuffer.clear();
}
}
43 changes: 43 additions & 0 deletions Source Code/esprfidtool/api_server.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
server.on("/api/help", [](){
String apihelpHTML=F(
"<a href=\"/\"><- BACK TO INDEX</a><br><br>"
"<b>/api/info</b><br>"
"<small>Usage: [server]/api/info</small><br>"
"<b>/api/viewlog</b><br>"
"<small>Usage: [server]/api/viewlog?logfile=[log.txt]</small><br>"
"<b>/api/listlogs</b><br>"
"<small>Usage: [server]/api/listlogs</small><br>"
"<b>Optional Arguments</b><br>"
"<small>Prettify: [api-url]?[args]<u>&prettify=1</u></small><br>"
);
server.send(200, "text/html", apihelpHTML);
});

server.on("/api/info", [](){
int prettify=0;
if (server.hasArg("prettify")) {
prettify=1;
}
apiinfo(prettify);
});

server.on("/api/listlogs", [](){
int prettify=0;
if (server.hasArg("prettify")) {
prettify=1;
}
apilistlogs(prettify);
});

server.on("/api/viewlog", [](){
int prettify=0;
if (server.hasArg("prettify")) {
prettify=1;
}
if (server.hasArg("logfile")) {
apilog(server.arg("logfile"),prettify);
}
else {
server.send(200, "application/json", F("Usage: [server]/api/viewlog?logfile=[logfile.txt]"));
}
});
15 changes: 14 additions & 1 deletion Source Code/esprfidtool/esprfidtool.ino
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ DNSServer dnsServer;

HTTPClient http;

#include "api.h"

const char* update_path = "/update";
int accesspointmode;
char ssid[32];
Expand Down Expand Up @@ -531,6 +533,9 @@ void LogWiegand(WiegandNG &tempwg) {
f.println("?");
}
}
else if (countedBits==248) {
f.println(",");
}
else {
f.println("");
}
Expand Down Expand Up @@ -790,6 +795,8 @@ bool loadDefaults() {
json["safemode"] = "0";
File configFile = SPIFFS.open("/esprfidtool.json", "w");
json.printTo(configFile);
configFile.close();
jsonBuffer.clear();
loadConfig();
}

Expand Down Expand Up @@ -895,7 +902,8 @@ bool loadConfig() {
// Serial.print("IP address = ");
// Serial.println(WiFi.localIP());
}

configFile.close();
jsonBuffer.clear();
return true;
}

Expand Down Expand Up @@ -925,6 +933,8 @@ bool saveConfig() {

File configFile = SPIFFS.open("/esprfidtool.json", "w");
json.printTo(configFile);
configFile.close();
jsonBuffer.clear();
return true;
}

Expand All @@ -949,6 +959,7 @@ void ListLogs(){
File f = dir.openFile("r");
FileList += " ";
if((!FileName.startsWith("/payloads/"))&&(!FileName.startsWith("/esploit.json"))&&(!FileName.startsWith("/esportal.json"))&&(!FileName.startsWith("/esprfidtool.json"))&&(!FileName.startsWith("/config.json"))) FileList += "<tr><td><a href=\"/viewlog?payload="+FileName+"\">"+FileName+"</a></td>"+"<td>"+f.size()+"</td><td><a href=\""+FileName+"\"><button>Download File</button></td><td><a href=\"/deletelog?payload="+FileName+"\"><button>Delete File</button></td></tr>";
f.close();
}
FileList += "</table>";
server.send(200, "text/html", FileList);
Expand Down Expand Up @@ -1215,6 +1226,8 @@ void setup() {
dataCONVERSION="";
});

#include "api_server.h"

server.on("/stoptx", [](){
server.send(200, "text/html", F("<html><body>This will kill any ongoing transmissions.<br><br>Are you sure?<br><br><a href=\"/stoptx/yes\">YES</a> - <a href=\"/\">NO</a></body></html>"));
});
Expand Down
38 changes: 19 additions & 19 deletions Source Code/esprfidtool/pinSEND.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
void pinSEND(int pinDELAY,String pinBIN) {
for (int i=0; i<=pinBIN.length(); i++) {
if (pinBIN.charAt(i) == '0') {
digitalWrite(DATA0, LOW);
delayMicroseconds(txdelayus);
digitalWrite(DATA0, HIGH);
}
else if (pinBIN.charAt(i) == '1') {
digitalWrite(DATA1, LOW);
delayMicroseconds(txdelayus);
digitalWrite(DATA1, HIGH);
}
delay(txdelayms);
}
yield();
delay(pinDELAY);
pinBIN="";
pinDELAY=100;
}
void pinSEND(int pinDELAY,String pinBIN) {
for (int i=0; i<=pinBIN.length(); i++) {
if (pinBIN.charAt(i) == '0') {
digitalWrite(DATA0, LOW);
delayMicroseconds(txdelayus);
digitalWrite(DATA0, HIGH);
}
else if (pinBIN.charAt(i) == '1') {
digitalWrite(DATA1, LOW);
delayMicroseconds(txdelayus);
digitalWrite(DATA1, HIGH);
}
delay(txdelayms);
}
yield();
delay(pinDELAY);
pinBIN="";
pinDELAY=100;
}
3 changes: 2 additions & 1 deletion Source Code/esprfidtool/version.h
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
String version = "1.1.5";
String version = "1.1.6";
String APIversion = "1.0.0";

0 comments on commit a67d0dd

Please sign in to comment.