HID Card Reader - NonaSuomy/PuRe GitHub Wiki

Card Reader for unLab sign-ins

Hardware

500px

500px

Software

File:UnDoor.png

/home/unlab/unbot/unbot.conf

 source scripts/unsocket.tcl

unsocket.tcl

# Script: unSocket.tcl for EggDrop bot: unBot
# Hacked by: Wess Thornton
# Description: Opens a socket on the unBot and listens for text input to spam to the IRC Channel #unLab.
#              Currently getting text from a C Winsock script that scans our RFID Cards.
#              Message needs to be formatted "PASS SENDER WHO MESSAGE"
#              EX: "mypass123 unDoor @WessleeT haz Enter'd."


# SETTINGS START

# Port/IP for your script you want to listen on (you need to set same port in client app that sends the text)
set botlisten(port) "31337"
set botlisten(ip) "10.13.37.105"

# Channel you want to send output to.
set botlisten(devchan) "#unlab"

# Bot listen password. Makes so no external siglnals can get to it. use only if hosting on a different server.
set botlisten(password) "PASSWORDHERE"

# SETTINGS END

# Open our socket.
if {![info exists serverSocket]} {
   set serverSocket [socket -server main -myaddr $botlisten(ip) $botlisten(port)]
}

proc main { sock host port } {
   #-blocking 0
   fconfigure $sock -buffering line
   fileevent $sock readable [action $sock $host $port]
}

proc action { chan host port } {
   global botlisten
   set soc_data [gets $chan]
   if {![eof $chan]} {
      outputAction $soc_data $botlisten(devchan)
      set soc_data [gets $chan]
   } else {
      outputAction $soc_data $botlisten(devchan)
      close $chan
   }
}

# Check for password, format output and then spam channel.

proc outputAction { soc_data chan } {
  global botlisten
  # putlog $soc_data
  if {[string length $soc_data]} {
    lassign [split $soc_data] pass sender who
    set message [join [lrange [split $soc_data] 3 end]]
    # putlog $pass
    # putlog $sender
    # putlog $who
    # putlog $message
    # putlog $botlisten(password)
    if {[string equal $pass $botlisten(password)]} {
      #This Relays what ever the bot is sending to the selected channel, this could also be a user
      putquick "PRIVMSG $chan :<< $sender >> $who: $message"
      putlog "unSocket: message spammed in channel."
      if {[string equal $sender "unDoor"]} {
        # putlog "Sent message to AlphaSign."
        exec -- ~/signage.pl "unBot: Welcome to unLab" $who
        putlog "unSocket: Sent message to AlphaSign."
        #set url "http://10.13.37.222/jsonrpc?request=\{%22jsonrpc%22:%222.0%22,%22id%22:%221%22,%22method%22:%22Player.Open%22,%22params%22:\{%22item%22:\{%22file%22:%22special://home/media/Busy_Child-The_Crystal_Method-Vegas.mp3%22\}\}\}"
        set urls "http://10.13.37.222/jsonrpc?request=%7B%22jsonrpc%22%3A%222.0%22%2C%22id%22%3A%221%22%2C%22method%22%3A%22Player.Open%22%2C%22params%22%3A%7B%22item%22%3A%7B%22file%22%3A%22special%3A%2F%2Fhome%2Fmedia%2F"
        set urle ".mp3%22%7D%7D%7D"
        # putlog $urls
        # putlog $who
        # putlog $urle
        set token [::http::geturl $urls$who$urle]
        set data [::http::data $token]
        ::http::cleanup $token
        putlog "unSocket: Sent message to XBMC"
      }
    }
  }
}


unsockettest.py

## unsockettest.py
import sys, socket

def runQuery(serverAddress, toSend):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(serverAddress)
    sock.sendall(toSend)
    sock.shutdown(1)
    response = sock.makefile().read()
    print "Sent to server:", toSend
    print "Got reply:", response
    sock.close()

servAddr = sys.argv[1]
host, port = servAddr.split(":")
serverAddress = (host, int(port))
runQuery(serverAddress, "PASSWORDHERE unDoor TeknoJuce This is a test pang!")
# runQuery(serverAddress, "PASSWORDHERE Here is a longer string")

get_data.c Line:527-727

// Database process data to console and fire data at unBot to spam.
static int callback(void *data, int argc, char **argv, char **azColName){
	int i;
	printf("%s\n", argv[5] ? argv[5] : "NULL");

	fprintf(stderr, "%s: \n", (const char*) data);

	for (i = 0; i < argc; i++){
		printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
	}
	printf("\n");

	// Spam unBot with the sqlite3 membercard table options field.
	sendunbot(31337, "10.13.37.105", argv[4], argv[5]);
	
	return 0;
}

// Search sqlite3 database for card serial number.
int uncardbase(int argc, char argv [])
{
	sqlite3 *db;
	char *zErrMsg = 0;
	int rc;
	const char* data = "Callback function called";
	//char buffer1 [] = "80e84300fbff12e1";
	char *sql; 
	/* Open database */
	// Open SQL - Dont declare vars in here or you'll be sorry
	rc = sqlite3_open("unCard.db", &db);
	if (rc){
		fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
		exit(0);
	}
	else{
		fprintf(stderr, "Opened database successfully\n");
	}
	
	//char *zSQL = sqlite3_mprintf("SELECT price FROM warehouse WHERE p_ID='%q'", input_value);
	//sqlite3_prepare_v2(handle,zSQL,-1,&stmt,0);
	
	/* Create SQL statement */
	
	sql = sqlite3_mprintf("SELECT CardSerial, CardATR, FirstName, LastName, NickName, Options FROM membercard WHERE CardSerial = '%q'", argv);

	/* Execute SQL statement */
	rc = sqlite3_exec(db, sql, callback, (void*) data, &zErrMsg);
	if (rc != SQLITE_OK){
		fprintf(stderr, "SQL error: %s\n", zErrMsg);
		sqlite3_free(zErrMsg);
		//system("pause");
	}
	else{
		fprintf(stdout, "Operation done successfully\n");
		//system("pause");
	}
	sqlite3_close(db);
	return 0;
}

// Open

int sendunbot(int argc, char *argv, char *nickname, char *msgunbot) {
	char   *first = "PASSWORDHERE unDoor";
	char   *second = nickname;
	char   *third = msgunbot;
	char   *botmsg;
	char   *tpkt;                 /* message to send to host ("<unDoor> + nickname + msgunbot") */
	char   *servername;			  /* pointer to name of server */
	struct hostent *h;   		  /* getnamebyhost info returned */
	struct sockaddr_in  sa;		  /* socket address */
	int    soxdes;                /* socket descriptor */
	short  portnum;               /* port number of server */
	int    inlen;                 /* length of input message */
	char   inbuf[INSIZE];		  /* input message buffer */
	
	size_t len1 = strlen(first);
	size_t len2 = strlen(second);
	size_t len3 = strlen(third);

#ifdef WINSOCK
	WSADATA wsaData;			  /* Winsock parameters */

	WSAStartup(0x0101, &wsaData);

	botmsg = (char *)malloc(len1 + len2 + len3 + 3);
	memcpy(botmsg, first, len1);
	botmsg[len1] = ' ';
	memcpy(botmsg + len1 + 1, second, len2); // includes terminating null
	botmsg[len1 + len2 + 1] = ' ';
	memcpy(botmsg + len1 + len2 + 2, third, len3 + 1);
	//char botmsg[256];
	//_snprintf(botmsg, sizeof botmsg, "%s%s%s", first, second, third);
	//size_t size = strlen(first) + strlen(second) + strlen(third) + 3; botmsg = malloc(size); snprintf(botmsg, size, "%s %s %s", first, second, third);
	tpkt = botmsg;
	//printf_s(tpkt);
	/* ask for the server name and port */
	//puts("\nEnter the IP name of the server >");
	servername = (char *) malloc(80);
	//scanf_s("%s", servername);
	//puts("\nEnter the port number used by the server >");
	//scanf_s("%d", &portnum);
	servername = argv;
	portnum = argc;
#else
	servername = argv[1];
	portnum = atoi(argv[2]);
#endif

	/* get host address */
	h = gethostbyname(servername);
	if (h == NULL) {
		perror("gethostbyname");
		//system("PAUSE");
		//exit(5);
	}

	/*  build socket structure  */
	memcpy((char *) &sa.sin_addr, (char *) h->h_addr, h->h_length);
	sa.sin_family = h->h_addrtype;
	sa.sin_port = htons(portnum);

	/*  allocate an open socket */
	soxdes = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (soxdes == -1) {
		perror("socket");
		//system("PAUSE");
		//exit(7);
	}
	else
		printf("socket created. result is %X\n", soxdes);

	/*  connect to the server */
	if (connect(soxdes, (const struct sockaddr*) &sa, sizeof sa) < 0) {
		perror("connect");
		//system("PAUSE");
		//exit(7);
	}
	puts("connected");

	/* send a packet to the server */
	if (send(soxdes, tpkt, strlen(tpkt), 0) == -1) {
		perror("write to socket");
		//system("PAUSE");
		//exit(5);
	}
	puts(tpkt);
	puts("packet sent to server");

	/* wait for a reply */
	//inlen = recv(soxdes, inbuf, INSIZE, 0);
	//printf("%d byte reply from server >%s\n", inlen, inbuf);
	//system("PAUSE");

#ifdef WINSOCK
	WSACleanup();
#else
	close(soxdes);
#endif
}

void ShowCardEntry(BYTE bParam, BYTE pbData[], DWORD dwLength)
{
	DWORD i;
	char buffer[17];
	char temp[3];
	int k;

	printf("\t");
	switch (bParam)
	{
		case 0x00 : printf("Serial number       : "); break;
		case 0x01 : printf("Historical bytes    : "); break;
		case 0xF0 : printf("Complete identifier : "); break;
		case 0xF1 : printf("PIX.SS & PIX.NN     : "); break;
		case 0xF2 : printf("Truncated serial n. : "); break;

		default   : printf("Unknown entry %02X  : ", bParam);
	}
	
	// get the byte value into this form to send to the database select statement function above...
	//uncardbase(1, "80e84300fbff12e1");
	
	k= 0;
	for (i = 0; i < dwLength; i++)
	{
		sprintf(temp,"%02X", pbData[i]);
		buffer[k] = temp[0];
		buffer[k+1] = temp[1];
		k += 2;
	}
	buffer[16] = 0;
	//Test Debug Sample.
	//uncardbase(1, "80E84300FBFF12E1");
	//strtoul(buffer, NULL, 16);
	printf(buffer);
	printf("\n");
	printf("\n");
	uncardbase(1, buffer);
}
⚠️ **GitHub.com Fallback** ⚠️