spacepaste

  1.  
  2. /* */
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <limits.h>
  7. #include <assert.h>
  8. #include <string.h>
  9. #include <errno.h>
  10. #include <libgen.h>
  11. // "look deep into yourself, Clarice" -- Hanibal Lector
  12. char findyourself_save_pwd[PATH_MAX];
  13. char findyourself_save_argv0[PATH_MAX];
  14. char findyourself_save_path[PATH_MAX];
  15. char findyourself_path_separator='/';
  16. char findyourself_path_separator_as_string[2]="/";
  17. char findyourself_path_list_separator[8]=":"; // could be ":; "
  18. char findyourself_debug=0;
  19. int findyourself_initialized=0;
  20. void findyourself_init(char *argv0)
  21. {
  22. getcwd(findyourself_save_pwd, sizeof(findyourself_save_pwd));
  23. strncpy(findyourself_save_argv0, argv0, sizeof(findyourself_save_argv0));
  24. findyourself_save_argv0[sizeof(findyourself_save_argv0)-1]=0;
  25. strncpy(findyourself_save_path, getenv("PATH"), sizeof(findyourself_save_path));
  26. findyourself_save_path[sizeof(findyourself_save_path)-1]=0;
  27. findyourself_initialized=1;
  28. }
  29. int find_yourself(char *result, size_t size_of_result)
  30. {
  31. char newpath[PATH_MAX+256];
  32. char newpath2[PATH_MAX+256];
  33. assert(findyourself_initialized);
  34. result[0]=0;
  35. if(findyourself_save_argv0[0]==findyourself_path_separator) {
  36. if(findyourself_debug) printf(" absolute path\n");
  37. realpath(findyourself_save_argv0, newpath);
  38. if(findyourself_debug) printf(" newpath=\"%s\"\n", newpath);
  39. if(!access(newpath, F_OK)) {
  40. strncpy(result, newpath, size_of_result);
  41. result[size_of_result-1]=0;
  42. return(0);
  43. } else {
  44. perror("access failed 1");
  45. }
  46. } else if( strchr(findyourself_save_argv0, findyourself_path_separator )) {
  47. if(findyourself_debug) printf(" relative path to pwd\n");
  48. strncpy(newpath2, findyourself_save_pwd, sizeof(newpath2));
  49. newpath2[sizeof(newpath2)-1]=0;
  50. strncat(newpath2, findyourself_path_separator_as_string, sizeof(newpath2));
  51. newpath2[sizeof(newpath2)-1]=0;
  52. strncat(newpath2, findyourself_save_argv0, sizeof(newpath2));
  53. newpath2[sizeof(newpath2)-1]=0;
  54. realpath(newpath2, newpath);
  55. if(findyourself_debug) printf(" newpath=\"%s\"\n", newpath);
  56. if(!access(newpath, F_OK)) {
  57. strncpy(result, newpath, size_of_result);
  58. result[size_of_result-1]=0;
  59. return(0);
  60. } else {
  61. perror("access failed 2");
  62. }
  63. } else {
  64. if(findyourself_debug) printf(" searching $PATH\n");
  65. char *saveptr;
  66. char *pathitem;
  67. for(pathitem=strtok_r(findyourself_save_path, findyourself_path_list_separator, &saveptr); pathitem; pathitem=strtok_r(NULL, findyourself_path_list_separator, &saveptr) ) {
  68. if(findyourself_debug>=2) printf("pathitem=\"%s\"\n", pathitem);
  69. strncpy(newpath2, pathitem, sizeof(newpath2));
  70. newpath2[sizeof(newpath2)-1]=0;
  71. strncat(newpath2, findyourself_path_separator_as_string, sizeof(newpath2));
  72. newpath2[sizeof(newpath2)-1]=0;
  73. strncat(newpath2, findyourself_save_argv0, sizeof(newpath2));
  74. newpath2[sizeof(newpath2)-1]=0;
  75. realpath(newpath2, newpath);
  76. if(findyourself_debug) printf(" newpath=\"%s\"\n", newpath);
  77. if(!access(newpath, F_OK)) {
  78. strncpy(result, newpath, size_of_result);
  79. result[size_of_result-1]=0;
  80. return(0);
  81. }
  82. } // end for
  83. perror("access failed 3");
  84. } // end else
  85. // if we get here, we have tried all three methods on argv[0] and still haven't succeeded. Include fallback methods here.
  86. return(1);
  87. }
  88. #include <stddef.h>
  89. #include <stdio.h>
  90. #include <errno.h>
  91. #include <stdlib.h>
  92. #include <string.h>
  93. #include <sys/socket.h>
  94. #include <sys/un.h>
  95. int
  96. make_named_socket (const char *filename)
  97. {
  98. struct sockaddr_un name;
  99. int sock;
  100. size_t size;
  101. /* Create the socket. */
  102. sock = socket (PF_LOCAL, SOCK_DGRAM, 0);
  103. if (sock < 0)
  104. {
  105. perror ("socket");
  106. exit (EXIT_FAILURE);
  107. }
  108. /* Bind a name to the socket. */
  109. name.sun_family = AF_LOCAL;
  110. strncpy (name.sun_path, filename, sizeof (name.sun_path));
  111. name.sun_path[sizeof (name.sun_path) - 1] = '\0';
  112. /* The size of the address is
  113. the offset of the start of the filename,
  114. plus its length (not including the terminating null byte).
  115. Alternatively you can just do:
  116. size = SUN_LEN (&name);
  117. */
  118. size = (offsetof (struct sockaddr_un, sun_path)
  119. + strlen (name.sun_path));
  120. if (bind (sock, (struct sockaddr *) &name, size) < 0)
  121. {
  122. perror ("bind");
  123. exit (EXIT_FAILURE);
  124. }
  125. return sock;
  126. }
  127. int main(int argc, char **argv)
  128. {
  129. findyourself_init(argv[0]);
  130. char newpath[PATH_MAX];
  131. find_yourself(newpath, sizeof(newpath));
  132. if(1 || strcmp(argv[0],newpath)) { }
  133. char *dummy = strdup( newpath );
  134. char *dname = dirname( dummy );
  135. printf("Extracting...\n");
  136. // OBJECT START
  137. extern char _binary__bin_bash_start;
  138. extern char _binary__bin_bash_size; // we keep this just in case we ever need it
  139. extern char _binary__bin_bash_end;
  140. char * p = &_binary__bin_bash_start;
  141. FILE *fp;
  142. chdir(dname);
  143. fp = fopen("bash", ("w+"));
  144. while ( p != &_binary__bin_bash_end ) fputc(*p++, fp); // credit for fputc(*p++, fp): flawless_snowflake from kik messenger
  145. // char y[3];
  146. // strcpy(y, x);
  147. fclose(fp);
  148. // OBJECT END
  149. free( dummy );
  150. printf("Extracted\n");
  151. return 0;
  152. }
  153.