Create a gist now

Instantly share code, notes, and snippets.

@eQu1NoX /pindetector.c Secret
Created Feb 23, 2015

What would you like to do?
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ptrace.h>
uint32_t check_imagename(char *imagename, const char *substring) {
char *ptr;
int ilength = strlen(imagename);
int slength = strlen(substring);
ptr = imagename + ilength - slength;
if (!strcmp(ptr, substring))
return -1;
return 0;
}
int32_t parse_mapline(char *line) {
uint32_t startaddr, endaddr;
char *prot = (char *)malloc(sizeof(char)*4);
char i_prot = 0;
uint32_t length;
uint32_t temp, temp2, temp3;
char *imagename = (char *)malloc(sizeof(char)*500);
int32_t retval;
sscanf(line, "%08x-%08x %s %d %d:%d %d\t%s\n",
&startaddr,
&endaddr,
prot,
&length,
&temp2,
&temp3,
&temp,
imagename);
retval = check_imagename(imagename, "pinbin");
if (imagename) {
free(imagename); imagename = NULL;
}
if (prot) {
free(prot); prot = NULL;
}
}
int check_pin() {
int pid = getpid();
char buffer[100];
FILE *fp;
int read;
char *line=NULL;
size_t len;
int retval;
sprintf(buffer, "/proc/%d/maps", pid);
fp = fopen(buffer, "r");
if (fp == NULL) {
printf("Cant open\n"); fflush(stdout);
exit(0);
}
while ((read = getline(&line, &len, fp)) != -1) {
retval = parse_mapline(line);
if (line) {
free(line);
line = NULL;
}
if (retval == -1) { fclose(fp); return retval; }
}
fclose(fp);
return 0;
}
int main(int argc, const char *argv[])
{
if (ptrace(PTRACE_TRACEME, 0, 1, 0) == -1)
return -1;
printf("No ptrace thingy detected yet\n");
printf("PIN being used = %d\n", (-1 == check_pin()));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment