Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <limits.h>
  6. #include <inttypes.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <fcntl.h>
  10.  
  11. int main() {
  12. char file[PATH_MAX];
  13. struct stat st;
  14. char *nl;
  15. while (fgets(file, sizeof(file), stdin)) {
  16. nl = memchr(file, '\n', sizeof(file));
  17. if (nl) *nl = '\0';
  18. int exec = open(file, O_RDONLY);
  19. //if file exists, not empty, and marked as executable
  20. if (exec > 0 && fstat(exec, &st) != -1 && st.st_mode & S_IXUSR) {
  21. //if it to short(shorter then #!) it is invalid executable
  22. if (lseek(exec, 0, SEEK_END) < 2) {puts(file); continue; }
  23. lseek(exec, 0, SEEK_SET);
  24. char hash_sym = -1, exclamation_sym = -1;
  25. read(exec, &hash_sym, sizeof(char));
  26. read(exec, &exclamation_sym, sizeof(char));
  27. //if it is have #! header we check for path and is it content marked as executable
  28. if (hash_sym == '#' && exclamation_sym == '!') {
  29. char buffer[PATH_MAX];
  30. read(exec, buffer, sizeof(buffer));
  31. nl = memchr(buffer, '\n', sizeof(buffer));
  32. if (nl) *nl = '\0';
  33. struct stat interpreter;
  34. if (stat(buffer, &interpreter) == -1 || !(interpreter.st_mode & S_IXUSR)) puts(file);
  35. } else {
  36. //we check for ELF file
  37. lseek(exec, 0, SEEK_SET);
  38. char del = -1, e = -1, l = -1, f = -1;
  39. read(exec, &del, sizeof(char));
  40. read(exec, &e, sizeof(char));
  41. read(exec, &l, sizeof(char));
  42. read(exec, &f, sizeof(char));
  43. if (del != 0x7f || e != 'E' || l != 'L' || f != 'F') puts(file);
  44. }
  45. }
  46. }
  47. return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement