A shell that runs x86_64 assembly
c
x86-64
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

asm_env.c 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /* Copyright Yann Weber <asmsh@yannweb.net>
  2. This file is part of asmsh.
  3. asmsh is free software: you can redistribute it and/or modify it under the
  4. terms of the GNU General Public License as published by the Free Software
  5. Foundation, either version 3 of the License, or any later version.
  6. asmsh is distributed in the hope that it will be useful, but WITHOUT ANY
  7. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  9. details.
  10. You should have received a copy of the GNU General Public License along
  11. with asmsh. If not, see <https://www.gnu.org/licenses/>.
  12. */
  13. #include "asm_env.h"
  14. static int _asmsh_env_spawn(asmsh_env_t *asmenv, const char *childpath);
  15. static void _asmsh_env_child(const char *childpath);
  16. asmsh_env_t* asmsh_env(const char *childpath)
  17. {
  18. asmsh_env_t *res;
  19. int err;
  20. if((res = malloc(sizeof(*res))) == NULL)
  21. {
  22. err = errno;
  23. perror("Unable to allocate env");
  24. errno = err;
  25. return NULL;
  26. }
  27. child_mmap_init(&(res->mmap));
  28. if((res->childpath = strdup(childpath)) == NULL)
  29. {
  30. goto err_pathdup;
  31. }
  32. if(_asmsh_env_spawn(res, childpath) < 0)
  33. {
  34. err = errno;
  35. goto err;
  36. }
  37. if(asmsh_env_update(res) < 0)
  38. {
  39. goto err;
  40. }
  41. res->txt_map_addr = (void*)res->regs.rax;
  42. res->txt_map_sz = res->regs.r14;
  43. res->stack_addr = (void*)res->regs.r15;
  44. res->stack_sz = (size_t)res->stack_addr - res->regs.rsp;
  45. res->code_write_ptr = res->txt_map_addr;
  46. return res;
  47. err:
  48. free(res->childpath);
  49. err_pathdup:
  50. free(res);
  51. errno = err;
  52. return NULL;
  53. }
  54. void asmsh_env_free(asmsh_env_t *asmenv)
  55. {
  56. if(asmenv->mmap.maps)
  57. {
  58. free(asmenv->mmap.maps);
  59. }
  60. kill(asmenv->pid, SIGKILL);
  61. free(asmenv->childpath);
  62. free(asmenv);
  63. }
  64. int asmsh_env_write_mem(asmsh_env_t *env, void *addr, const unsigned char *buf, size_t buf_sz)
  65. {
  66. int err;
  67. u_int64_t data;
  68. char written;
  69. char bleft = (u_int64_t)addr % 8;
  70. written = 0;
  71. if(bleft)
  72. {
  73. // First write to correct further write allignement
  74. void *wr_addr = (void*)(((u_int64_t)addr / 8) * 8);
  75. char towrite = 8 - bleft;
  76. towrite = (towrite > buf_sz)?buf_sz:towrite;
  77. errno = 0;
  78. data = ptrace(PTRACE_PEEKTEXT, env->pid, wr_addr, NULL);
  79. if(errno)
  80. {
  81. err = errno;
  82. perror("Unable to peektext in order to allign write");
  83. errno = err;
  84. return -1;
  85. }
  86. memcpy(((unsigned char*)&data)+bleft, &(buf[written]), towrite);
  87. if(ptrace(PTRACE_POKETEXT, env->pid, wr_addr, data) < 0)
  88. {
  89. err = errno;
  90. perror("Unable to poketext in order to allign write");
  91. errno = err;
  92. return -1;
  93. }
  94. written += towrite;
  95. env->code_write_ptr += towrite;
  96. }
  97. if(written >= buf_sz)
  98. {
  99. return 0;
  100. }
  101. // env->code_write_ptr is now word alligned and "written" bytes are
  102. // allready written
  103. while(written < buf_sz)
  104. {
  105. char towrite = buf_sz - written;
  106. if(towrite >= 8)
  107. {
  108. data = *(u_int64_t*)(&buf[written]);
  109. }
  110. else
  111. {
  112. data = 0;
  113. memcpy(&data, &buf[written], towrite);
  114. }
  115. if(ptrace(PTRACE_POKETEXT, env->pid, env->code_write_ptr, data) < 0)
  116. {
  117. err = errno;
  118. perror("Unable to poketext");
  119. errno = err;
  120. return -1;
  121. }
  122. written += towrite;
  123. env->code_write_ptr += towrite;
  124. }
  125. return 0;
  126. }
  127. int asmsh_env_write_code(asmsh_env_t *env, asmsh_bytecode_t *bcode)
  128. {
  129. return asmsh_env_write_mem(env, env->code_write_ptr,
  130. bcode->bytes, bcode->size);
  131. }
  132. int asmsh_env_step(asmsh_env_t *env, int *status)
  133. {
  134. int err, ret;
  135. if(status) { *status = 0; }
  136. if(ptrace(PTRACE_SINGLESTEP, env->pid, NULL, 0) < 0)
  137. {
  138. err = errno;
  139. perror("Unable to ptrace singlestep");
  140. goto err;
  141. }
  142. if(waitpid(env->pid, &env->status, 0) < 0)
  143. {
  144. err = errno;
  145. perror("Unable to wait for child process to stop on step");
  146. goto err;
  147. }
  148. if(status) { *status = env->status; }
  149. if(!WIFSTOPPED(env->status) || env->status >> 8 != 5)
  150. {
  151. goto err_wstatus;
  152. }
  153. return 0;
  154. /// TODO replace by an utility function that logs ?
  155. err_wstatus:
  156. if(WIFEXITED(env->status))
  157. {
  158. dprintf(2, "Child exited with status %d\n", WEXITSTATUS(env->status));
  159. }
  160. else if(WIFSIGNALED(env->status))
  161. {
  162. if(WCOREDUMP(env->status))
  163. {
  164. dprintf(2, "Child segfault\n");
  165. }
  166. else
  167. {
  168. dprintf(2, "Child killed by sig %d\n", WTERMSIG(env->status));
  169. }
  170. }
  171. else if(WIFSTOPPED(env->status))
  172. {
  173. dprintf(2, "Child stopped by %s(sig#%d)\n",
  174. strsignal(WSTOPSIG(env->status)),
  175. WSTOPSIG(env->status));
  176. }
  177. else
  178. {
  179. dprintf(2, "Unexpected child status 0x%04X\n", env->status);
  180. }
  181. errno = ECHILD;
  182. return 1;
  183. err:
  184. errno = err;
  185. return -1;
  186. }
  187. int asmsh_env_update(asmsh_env_t *asmenv)
  188. {
  189. if(asmsh_env_update_regs(asmenv) < 0)
  190. {
  191. return -1;
  192. }
  193. return child_mmap_get(asmenv->pid, &(asmenv->mmap));
  194. }
  195. int asmsh_env_update_maps(asmsh_env_t *asmenv)
  196. {
  197. if(child_mmap_get(asmenv->pid, &(asmenv->mmap)) < 0)
  198. {
  199. return -1;
  200. }
  201. return 0;
  202. }
  203. int asmsh_env_update_regs(asmsh_env_t *asmenv)
  204. {
  205. bzero(&(asmenv->regs), sizeof(asmenv->regs));
  206. if(ptrace(PTRACE_GETREGS, asmenv->pid, NULL, &(asmenv->regs)) < 0)
  207. {
  208. int err = errno;
  209. perror("ptrace getregs error");
  210. errno = err;
  211. return -1;
  212. }
  213. return 0;
  214. }
  215. static int _asmsh_env_spawn(asmsh_env_t *env, const char *childpath)
  216. {
  217. int err;
  218. int wstatus;
  219. if((env->pid = fork()) == -1)
  220. {
  221. err = errno;
  222. perror("Unable to fork!");
  223. goto err_fork;
  224. }
  225. else if(env->pid == 0)
  226. {
  227. free(env);
  228. _asmsh_env_child(childpath?childpath:ASMSH_CHILD_PATH_DEFAULT);
  229. }
  230. if(ptrace(PTRACE_ATTACH, env->pid, 0, 0) == -1)
  231. {
  232. perror("Unable to attach to child process");
  233. goto err;
  234. }
  235. if(waitpid(env->pid, &wstatus, 0) < 0)
  236. {
  237. perror("Unable to wait for child process");
  238. goto err;
  239. }
  240. if(!WIFSTOPPED(wstatus))
  241. {
  242. goto err_wstatus;
  243. }
  244. // Attached to child
  245. // tell child to stop on exec
  246. if(ptrace(PTRACE_SETOPTIONS, env->pid, NULL, PTRACE_O_TRACEEXEC) < 0)
  247. {
  248. err = errno;
  249. perror("ptrace setoptions failed");
  250. goto err;
  251. }
  252. if(ptrace(PTRACE_CONT, env->pid, NULL, 0) < 0)
  253. {
  254. err = errno;
  255. perror("ptrace CONT failed after attach");
  256. goto err;
  257. }
  258. if(waitpid(env->pid, &wstatus, 0) < 0)
  259. {
  260. err = errno;
  261. perror("Unable to wait for child process to stop after exec");
  262. goto err;
  263. }
  264. if(wstatus >> 8 != (SIGTRAP | (PTRACE_EVENT_EXEC<<8)))
  265. {
  266. goto err_wstatus;
  267. }
  268. // next child events are : execve ret, mmap in, mmap out
  269. for(int i=0; i<3; i++)
  270. {
  271. if(ptrace(PTRACE_SYSCALL, env->pid, NULL, 0) < 0)
  272. {
  273. err = errno;
  274. perror("Unable to ptrace syscall");
  275. dprintf(2, "ptrace syscall failed on %d time\n", i+1);
  276. goto err;
  277. }
  278. if(waitpid(env->pid, &wstatus, 0) < 0)
  279. {
  280. err = errno;
  281. perror("Unable to wait for child process to stop on syscall");
  282. goto err;
  283. }
  284. if(wstatus != 1407)
  285. {
  286. goto err_wstatus;
  287. }
  288. }
  289. // mmap done by child process
  290. // WARNING totally depends on child.s source
  291. // right now there is only 4 instructions (the fourth is the jmp to the txt_map)
  292. // before reaching the start of the code mmap
  293. for(int i=0; i<4; i++)
  294. {
  295. /* // DEBUG to monitor placement in child exec
  296. asmsh_env_update_regs(env);
  297. dprintf(2, "%d) rax: %08X rip : %08X mmap_addr = %08X\n",
  298. i, env->regs.rax, env->regs.rip, env->txt_map_ptr);
  299. */
  300. if(asmsh_env_step(env, NULL))
  301. {
  302. goto err;
  303. }
  304. }
  305. return 0;
  306. /// TODO replace by an utility function that logs ?
  307. err_wstatus:
  308. if(WIFEXITED(wstatus))
  309. {
  310. dprintf(2, "Child exited with status %d\n", WEXITSTATUS(wstatus));
  311. }
  312. else if(WIFSIGNALED(wstatus))
  313. {
  314. if(WCOREDUMP(wstatus))
  315. {
  316. dprintf(2, "Child segfault\n");
  317. }
  318. else
  319. {
  320. dprintf(2, "Child killed by sig %d\n", WTERMSIG(wstatus));
  321. }
  322. }
  323. else if(WIFSTOPPED(wstatus))
  324. {
  325. dprintf(2, "Child stopped by sig %d\n", WSTOPSIG(wstatus));
  326. }
  327. else
  328. {
  329. dprintf(2, "Unexpected child status 0x%04X\n", wstatus);
  330. }
  331. err = ECHILD;
  332. err:
  333. kill(env->pid, SIGKILL);
  334. err_fork:
  335. errno = err;
  336. return -1;
  337. }
  338. static void _asmsh_env_child(const char *childpath)
  339. {
  340. char *argv[] = {NULL, NULL};
  341. char *envp[] = {NULL};
  342. if(!(argv[0] = strdupa(childpath)))
  343. {
  344. int err = errno;
  345. perror("Unable to strdupa asmsh childpath :/");
  346. exit(err?err:-1);
  347. }
  348. execve(childpath, argv, envp);
  349. int err = errno;
  350. perror("Unable to execve");
  351. exit(err?err:-1);
  352. }