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 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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);
  15. static void _asmsh_env_child(const char *childpath);
  16. /** Return a path (that should be freed) of a temporary executable
  17. * child that can be exec on */
  18. static char *asmsh_env_tmpexec();
  19. /* binary buffer of the child elf */
  20. extern unsigned char _binary_child_start;
  21. extern unsigned char _binary_child_end;
  22. asmsh_env_t* asmsh_env(const char *childpath)
  23. {
  24. asmsh_env_t *res;
  25. int err;
  26. if((res = malloc(sizeof(*res))) == NULL)
  27. {
  28. err = errno;
  29. asmsh_log_perror("Unable to allocate env");
  30. errno = err;
  31. return NULL;
  32. }
  33. child_mmap_init(&(res->mmap));
  34. res->childpath = NULL;
  35. if(childpath && (res->childpath = strdup(childpath)) == NULL)
  36. {
  37. err=errno;
  38. goto err_pathdup;
  39. }
  40. if(_asmsh_env_spawn(res) < 0)
  41. {
  42. err = errno;
  43. goto err;
  44. }
  45. if(asmsh_env_update(res) < 0)
  46. {
  47. err=errno;
  48. goto err;
  49. }
  50. res->txt_map_addr = (void*)res->regs.rax;
  51. res->txt_map_sz = res->regs.r14;
  52. res->stack_addr = (void*)res->regs.r15;
  53. res->stack_sz = (size_t)res->stack_addr - res->regs.rsp;
  54. res->code_write_ptr = res->txt_map_addr;
  55. return res;
  56. err:
  57. if(res->childpath)
  58. {
  59. free(res->childpath);
  60. }
  61. err_pathdup:
  62. free(res);
  63. errno = err;
  64. return NULL;
  65. }
  66. void asmsh_env_free(asmsh_env_t *asmenv)
  67. {
  68. if(asmenv->mmap.maps)
  69. {
  70. free(asmenv->mmap.maps);
  71. }
  72. kill(asmenv->pid, SIGKILL);
  73. free(asmenv->childpath);
  74. free(asmenv);
  75. }
  76. int asmsh_env_write_mem(asmsh_env_t *env, void *addr, const unsigned char *buf, size_t buf_sz)
  77. {
  78. int err;
  79. u_int64_t data;
  80. unsigned char written;
  81. char bleft = (u_int64_t)addr % 8;
  82. written = 0;
  83. if(bleft)
  84. {
  85. // First write to correct further write allignement
  86. void *wr_addr = (void*)(((u_int64_t)addr / 8) * 8);
  87. char towrite = 8 - bleft;
  88. towrite = (towrite > buf_sz)?buf_sz:towrite;
  89. errno = 0;
  90. data = ptrace(PTRACE_PEEKTEXT, env->pid, wr_addr, NULL);
  91. if(errno)
  92. {
  93. err = errno;
  94. asmsh_log_perror("Unable to peektext in order to allign write");
  95. errno = err;
  96. return -1;
  97. }
  98. memcpy(((unsigned char*)&data)+bleft, &(buf[written]), towrite);
  99. if(ptrace(PTRACE_POKETEXT, env->pid, wr_addr, data) < 0)
  100. {
  101. err = errno;
  102. asmsh_log_perror("Unable to poketext in order to allign write");
  103. errno = err;
  104. return -1;
  105. }
  106. written += towrite;
  107. env->code_write_ptr += towrite;
  108. }
  109. if(written >= buf_sz)
  110. {
  111. return 0;
  112. }
  113. // env->code_write_ptr is now word alligned and "written" bytes are
  114. // allready written
  115. while(written < buf_sz)
  116. {
  117. char towrite = buf_sz - written;
  118. if(towrite >= 8)
  119. {
  120. data = *(u_int64_t*)(&buf[written]);
  121. }
  122. else
  123. {
  124. data = 0;
  125. memcpy(&data, &buf[written], towrite);
  126. }
  127. if(ptrace(PTRACE_POKETEXT, env->pid, env->code_write_ptr, data) < 0)
  128. {
  129. err = errno;
  130. asmsh_log_perror("Unable to poketext");
  131. errno = err;
  132. return -1;
  133. }
  134. written += towrite;
  135. env->code_write_ptr += towrite;
  136. }
  137. return 0;
  138. }
  139. int asmsh_env_write_code(asmsh_env_t *env, asmsh_bytecode_t *bcode)
  140. {
  141. return asmsh_env_write_mem(env, env->code_write_ptr,
  142. bcode->bytes, bcode->size);
  143. }
  144. int asmsh_env_step(asmsh_env_t *env, int *status)
  145. {
  146. int err;
  147. if(status) { *status = 0; }
  148. if(ptrace(PTRACE_SINGLESTEP, env->pid, NULL, 0) < 0)
  149. {
  150. err = errno;
  151. asmsh_log_perror("Unable to ptrace singlestep");
  152. goto err;
  153. }
  154. if(waitpid(env->pid, &env->status, 0) < 0)
  155. {
  156. err = errno;
  157. asmsh_log_perror("Unable to wait for child process to stop on step");
  158. goto err;
  159. }
  160. if(status) { *status = env->status; }
  161. if(!WIFSTOPPED(env->status) || env->status >> 8 != 5)
  162. {
  163. goto err_wstatus;
  164. }
  165. return 0;
  166. /// TODO replace by an utility function that logs ?
  167. err_wstatus:
  168. if(WIFEXITED(env->status))
  169. {
  170. dprintf(2, "Child exited with status %d\n", WEXITSTATUS(env->status));
  171. }
  172. else if(WIFSIGNALED(env->status))
  173. {
  174. if(WCOREDUMP(env->status))
  175. {
  176. dprintf(2, "Child segfault\n");
  177. }
  178. else
  179. {
  180. dprintf(2, "Child killed by sig %d\n", WTERMSIG(env->status));
  181. }
  182. }
  183. else if(WIFSTOPPED(env->status))
  184. {
  185. dprintf(2, "Child stopped by %s(sig#%d)\n",
  186. strsignal(WSTOPSIG(env->status)),
  187. WSTOPSIG(env->status));
  188. }
  189. else
  190. {
  191. dprintf(2, "Unexpected child status 0x%04X\n", env->status);
  192. }
  193. errno = ECHILD;
  194. return 1;
  195. err:
  196. errno = err;
  197. return -1;
  198. }
  199. int asmsh_env_update(asmsh_env_t *asmenv)
  200. {
  201. if(asmsh_env_update_regs(asmenv) < 0)
  202. {
  203. return -1;
  204. }
  205. return child_mmap_get(asmenv->pid, &(asmenv->mmap));
  206. }
  207. int asmsh_env_update_maps(asmsh_env_t *asmenv)
  208. {
  209. if(child_mmap_get(asmenv->pid, &(asmenv->mmap)) < 0)
  210. {
  211. return -1;
  212. }
  213. return 0;
  214. }
  215. int asmsh_env_update_regs(asmsh_env_t *asmenv)
  216. {
  217. bzero(&(asmenv->regs), sizeof(asmenv->regs));
  218. if(ptrace(PTRACE_GETREGS, asmenv->pid, NULL, &(asmenv->regs)) < 0)
  219. {
  220. int err = errno;
  221. asmsh_log_perror("ptrace getregs error");
  222. errno = err;
  223. return -1;
  224. }
  225. return 0;
  226. }
  227. static int _asmsh_env_spawn(asmsh_env_t *env)
  228. {
  229. int err;
  230. int wstatus;
  231. const char *childpath = env->childpath?env->childpath:asmsh_env_tmpexec();
  232. if(!childpath)
  233. {
  234. return -1; // Error in asmsh_env_tmpexec()
  235. }
  236. if((env->pid = fork()) == -1)
  237. {
  238. err = errno;
  239. asmsh_log_perror("Unable to fork!");
  240. goto err_fork;
  241. }
  242. else if(env->pid == 0)
  243. {
  244. _asmsh_env_child(childpath);
  245. }
  246. if(waitpid(env->pid, &wstatus, WUNTRACED) < 0)
  247. {
  248. err=errno;
  249. asmsh_log_perror("Unable to wait for child process");
  250. goto err;
  251. }
  252. if(!WIFSTOPPED(wstatus))
  253. {
  254. dprintf(2, "child didn't stop as expected");
  255. goto err_wstatus;
  256. }
  257. if(ptrace(PTRACE_ATTACH, env->pid, 0, 0) == -1)
  258. {
  259. err=errno;
  260. asmsh_log_perror("Unable to attach to child process");
  261. goto err;
  262. }
  263. if(waitpid(env->pid, &wstatus, 0) < 0)
  264. {
  265. err=errno;
  266. asmsh_log_perror("Unable to wait for child process");
  267. goto err;
  268. }
  269. if(!WIFSTOPPED(wstatus))
  270. {
  271. dprintf(2, "child didn't stop as expected");
  272. goto err_wstatus;
  273. }
  274. // Attached to child
  275. // tell child to stop on exec
  276. if(ptrace(PTRACE_SETOPTIONS, env->pid, NULL, PTRACE_O_TRACEEXEC) < 0)
  277. {
  278. err = errno;
  279. asmsh_log_perror("ptrace setoptions failed");
  280. goto err;
  281. }
  282. for(int i=0; i<2; i++) // cont kill & ptrace
  283. {
  284. if(ptrace(PTRACE_CONT, env->pid, NULL, 0) < 0)
  285. {
  286. err = errno;
  287. asmsh_log_perror("ptrace CONT failed after attach");
  288. goto err;
  289. }
  290. if(waitpid(env->pid, &wstatus, 0) < 0)
  291. {
  292. err = errno;
  293. asmsh_log_perror("Unable to wait for child process to stop after exec");
  294. goto err;
  295. }
  296. if(wstatus >> 8 != (SIGTRAP | (PTRACE_EVENT_EXEC<<8)) && \
  297. !WIFSTOPPED(wstatus))
  298. {
  299. goto err_wstatus;
  300. }
  301. }
  302. // next child events are : execve ret, mmap in, mmap out
  303. for(int i=0; i<3; i++)
  304. {
  305. if(ptrace(PTRACE_SYSCALL, env->pid, NULL, 0) < 0)
  306. {
  307. err = errno;
  308. asmsh_log_error("Unable to ptrace syscall on %dth time : %s",
  309. i+1, strerror(errno));
  310. goto err;
  311. }
  312. if(waitpid(env->pid, &wstatus, 0) < 0)
  313. {
  314. err = errno;
  315. asmsh_log_perror("Unable to wait for child process to stop on syscall");
  316. goto err;
  317. }
  318. if(wstatus != 1407 && wstatus != 263551)
  319. {
  320. dprintf(2, "unexpected status after ptrace syscall %d\n", wstatus);
  321. goto err_wstatus;
  322. }
  323. }
  324. // mmap done by child process
  325. // WARNING totally depends on child.s source
  326. // right now there is only 4 instructions (the fourth is the jmp to the txt_map)
  327. // before reaching the start of the code mmap
  328. for(int i=0; i<4; i++)
  329. {
  330. /* // DEBUG to monitor placement in child exec
  331. asmsh_env_update_regs(env);
  332. dprintf(2, "%d) rax: %08X rip : %08X mmap_addr = %08X\n",
  333. i, env->regs.rax, env->regs.rip, env->txt_map_ptr);
  334. */
  335. if(asmsh_env_step(env, NULL))
  336. {
  337. err=errno;
  338. goto err;
  339. }
  340. }
  341. if(!env->childpath) { unlink(childpath); } // rm tmp child exec
  342. return 0;
  343. /// TODO replace by an utility function that logs ?
  344. err_wstatus:
  345. if(WIFEXITED(wstatus))
  346. {
  347. dprintf(2, "Child exited with status %d\n", WEXITSTATUS(wstatus));
  348. }
  349. else if(WIFSIGNALED(wstatus))
  350. {
  351. if(WCOREDUMP(wstatus))
  352. {
  353. dprintf(2, "Child segfault\n");
  354. }
  355. else
  356. {
  357. dprintf(2, "Child killed by sig %d\n", WTERMSIG(wstatus));
  358. }
  359. }
  360. else if(WIFSTOPPED(wstatus))
  361. {
  362. dprintf(2, "Child stopped by sig %d\n", WSTOPSIG(wstatus));
  363. }
  364. else
  365. {
  366. dprintf(2, "Unexpected child status 0x%04X\n", wstatus);
  367. }
  368. err = ECHILD;
  369. err:
  370. kill(env->pid, SIGKILL);
  371. err_fork:
  372. if(!env->childpath) { unlink(childpath); } // rm tmp child exec
  373. errno = err;
  374. return -1;
  375. }
  376. static void _asmsh_env_child(const char *childpath)
  377. {
  378. char *argv[] = {NULL, NULL};
  379. char *envp[] = {NULL};
  380. if(!(argv[0] = strdupa(childpath)))
  381. {
  382. int err = errno;
  383. perror("Unable to strdupa asmsh childpath :/");
  384. //asmsh_log_perror("Unable to strdupa asmsh childpath :/");
  385. ///! @todo dump logs before exit !
  386. exit(err?err:-1);
  387. }
  388. kill(getpid(), SIGSTOP);
  389. execve(childpath, argv, envp);
  390. int err = errno;
  391. perror("Unable to execve");
  392. //asmsh_log_perror("Child is unable to execve");
  393. ///! @todo dump logs before exit !
  394. exit(err?err:-1);
  395. }
  396. static char *asmsh_env_tmpexec()
  397. {
  398. char *ret = strdup("asmsh_child_XXXXXXXXX");
  399. int err;
  400. if(!ret)
  401. {
  402. err = errno;
  403. asmsh_log_perror("getting a temporary file name");
  404. errno = err;
  405. return NULL;
  406. }
  407. int tmpfd = mkstemp(ret);
  408. if(tmpfd < 0)
  409. {
  410. err = errno;
  411. asmsh_log_perror("Unable to mk temporary file");
  412. errno = err;
  413. return NULL;
  414. }
  415. const int sz = &_binary_child_end - &_binary_child_start;
  416. int rsz = write(tmpfd, &_binary_child_start, sz);
  417. if(rsz<sz)
  418. {
  419. // TODO : differenciate incomplete write & error !
  420. err = errno;
  421. asmsh_log_perror("Unable to write the child executable");
  422. free(ret);
  423. errno = err;
  424. return NULL;
  425. }
  426. fchmod(tmpfd, 0555);
  427. close(tmpfd);
  428. return ret;
  429. }