Skip to content

Instantly share code, notes, and snippets.

@hikari-no-yume
Created April 15, 2024 16:30
Show Gist options
  • Save hikari-no-yume/ea99e733f6d99cb9b43c5680b3245a51 to your computer and use it in GitHub Desktop.
Save hikari-no-yume/ea99e733f6d99cb9b43c5680b3245a51 to your computer and use it in GitHub Desktop.
test of /proc/xxx/mem
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(int argc, char **argv)
{
char *secret = strdup(argv[1]);
printf("I have a secret! It is located at %p and is %zu bytes long!\n", secret, strlen(secret));
while (1) {}
free(secret);
}
#define _GNU_SOURCE /* asprintf isn't in standard C before C23 */
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
int main(int argc, char **argv)
{
int pid = atoi(argv[1]);
size_t addr = (size_t)strtoll(argv[2], NULL, 0);
size_t size = (size_t)strtoll(argv[3], NULL, 0);
char *path;
asprintf(&path, "/proc/%d/mem", pid);
FILE *fp = fopen(path, "r");
if (!fp) {
perror("couldn't open file");
return 1;
}
free(path);
fseek(fp, (long)addr, SEEK_SET);
char *secret = malloc(size);
fread(secret, size, 1, fp);
fclose(fp);
printf("the secret is: %.*s\n", (int)size, secret);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment