From 1894e2ce7d4eff4796078bb688fce39bfab09cc0 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Wed, 26 Apr 2006 00:36:48 +0200 Subject: [PATCH] Initial roughly working version of blindtty, retty --- COPYING | 340 +++++++++++++++++++++++++++ Makefile | 13 ++ blindtty.c | 54 +++++ retty.c | 763 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 1170 insertions(+) create mode 100644 COPYING create mode 100644 Makefile create mode 100644 blindtty.c create mode 100644 retty.c diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..5b6e7c6 --- /dev/null +++ b/COPYING @@ -0,0 +1,340 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c67e3f2 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +CFLAGS=-Wall -g +LIBS=-lutil +LDFLAGS=$(LIBS) + +EXE=blindtty retty + +all: $(EXE) + +blindtty: blindtty.c +retty: retty.c + +clean: + rm -f *.o $(EXE) diff --git a/blindtty.c b/blindtty.c new file mode 100644 index 0000000..3b20b89 --- /dev/null +++ b/blindtty.c @@ -0,0 +1,54 @@ +/* + * blindtty - run command in a detached terminal + * + * Usage: blindtty CMD [ARGS]... + * + * Copyright (c) 2006 Petr Baudis, Jan Sembera + */ + +#include +#include +#include +#include +#include + + +void +sigchld(int s) +{ + _exit(0); +} + +int +main(int argc, char *argv[]) +{ + int ptm; + pid_t pid; + + if (argc < 2) { + fprintf(stderr, "Usage: blindtty CMD [ARG]...\n"); + return 1; + } + + if (fork()) + return 0; + setsid(); + + pid = forkpty(&ptm, NULL, NULL, NULL); + if (!pid) { + int i; for (i=0; i + */ + + +#define _XOPEN_SOURCE 500 /* include pread,pwrite */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +unsigned long openaddr = 0xabcdef, dupaddr, dup2addr, ioctladdr, raiseaddr, closeaddr; + + +/* memory map for libraries */ +#define MAX_NAME_LEN 256 +#define MEMORY_ONLY "[memory]" +struct mm { + char name[MAX_NAME_LEN]; + unsigned long start, end; +}; + +typedef struct symtab *symtab_t; +struct symlist { + Elf32_Sym *sym; /* symbols */ + char *str; /* symbol strings */ + unsigned num; /* number of symbols */ +}; +struct symtab { + struct symlist *st; /* "static" symbols */ + struct symlist *dyn; /* dynamic symbols */ +}; + +static void * +xmalloc(size_t size) +{ + void *p; + p = malloc(size); + if (!p) { + fprintf(stderr, "Out of memory\n"); + exit(1); + } + return p; +} + +static struct symlist * +get_syms(int fd, Elf32_Shdr *symh, Elf32_Shdr *strh) +{ + struct symlist *sl, *ret; + int rv; + + ret = NULL; + sl = (struct symlist *) xmalloc(sizeof(struct symlist)); + sl->str = NULL; + sl->sym = NULL; + + /* sanity */ + if (symh->sh_size % sizeof(Elf32_Sym)) { + fprintf(stderr, "elf_error\n"); + goto out; + } + + /* symbol table */ + sl->num = symh->sh_size / sizeof(Elf32_Sym); + sl->sym = (Elf32_Sym *) xmalloc(symh->sh_size); + rv = pread(fd, sl->sym, symh->sh_size, symh->sh_offset); + if (0 > rv) { + perror("read"); + goto out; + } + if (rv != symh->sh_size) { + fprintf(stderr, "elf error\n"); + goto out; + } + + /* string table */ + sl->str = (char *) xmalloc(strh->sh_size); + rv = pread(fd, sl->str, strh->sh_size, strh->sh_offset); + if (0 > rv) { + perror("read"); + goto out; + } + if (rv != strh->sh_size) { + fprintf(stderr, "elf error"); + goto out; + } + + ret = sl; +out: + return ret; +} + +static int +do_load(int fd, symtab_t symtab) +{ + int rv; + size_t size; + Elf32_Ehdr ehdr; + Elf32_Shdr *shdr = NULL, *p; + Elf32_Shdr *dynsymh, *dynstrh; + Elf32_Shdr *symh, *strh; + char *shstrtab = NULL; + int i; + int ret = -1; + + /* elf header */ + rv = read(fd, &ehdr, sizeof(ehdr)); + if (0 > rv) { + perror("read"); + goto out; + } + if (rv != sizeof(ehdr)) { + fprintf(stderr, "elf error\n"); + goto out; + } + if (strncmp(ELFMAG, ehdr.e_ident, SELFMAG)) { /* sanity */ + fprintf(stderr, "not an elf\n"); + goto out; + } + if (sizeof(Elf32_Shdr) != ehdr.e_shentsize) { /* sanity */ + fprintf(stderr, "elf error\n"); + goto out; + } + + /* section header table */ + size = ehdr.e_shentsize * ehdr.e_shnum; + shdr = (Elf32_Shdr *) xmalloc(size); + rv = pread(fd, shdr, size, ehdr.e_shoff); + if (0 > rv) { + perror("read"); + goto out; + } + if (rv != size) { + fprintf(stderr, "elf error"); + goto out; + } + + /* section header string table */ + size = shdr[ehdr.e_shstrndx].sh_size; + shstrtab = (char *) xmalloc(size); + rv = pread(fd, shstrtab, size, shdr[ehdr.e_shstrndx].sh_offset); + if (0 > rv) { + perror("read"); + goto out; + } + if (rv != size) { + fprintf(stderr, "elf error\n"); + goto out; + } + + /* symbol table headers */ + symh = dynsymh = NULL; + strh = dynstrh = NULL; + for (i = 0, p = shdr; i < ehdr.e_shnum; i++, p++) + if (SHT_SYMTAB == p->sh_type) { + if (symh) { + fprintf(stderr, "too many symbol tables\n"); + goto out; + } + symh = p; + } else if (SHT_DYNSYM == p->sh_type) { + if (dynsymh) { + fprintf(stderr, "too many symbol tables\n"); + goto out; + } + dynsymh = p; + } else if (SHT_STRTAB == p->sh_type + && !strncmp(shstrtab+p->sh_name, ".strtab", 7)) { + if (strh) { + fprintf(stderr, "too many string tables\n"); + goto out; + } + strh = p; + } else if (SHT_STRTAB == p->sh_type + && !strncmp(shstrtab+p->sh_name, ".dynstr", 7)) { + if (dynstrh) { + fprintf(stderr, "too many string tables\n"); + goto out; + } + dynstrh = p; + } + /* sanity checks */ + if ((!dynsymh && dynstrh) || (dynsymh && !dynstrh)) { + fprintf(stderr, "bad dynamic symbol table"); + goto out; + } + if ((!symh && strh) || (symh && !strh)) { + fprintf(stderr, "bad symbol table"); + goto out; + } + if (!dynsymh && !symh) { + fprintf(stderr, "no symbol table"); + goto out; + } + + /* symbol tables */ + if (dynsymh) + symtab->dyn = get_syms(fd, dynsymh, dynstrh); + if (symh) + symtab->st = get_syms(fd, symh, strh); + ret = 0; +out: + free(shstrtab); + free(shdr); + return ret; +} + +static symtab_t +load_symtab(char *filename) +{ + int fd; + symtab_t symtab; + + symtab = (symtab_t) xmalloc(sizeof(*symtab)); + memset(symtab, 0, sizeof(*symtab)); + + fd = open(filename, O_RDONLY); + if (0 > fd) { + perror("open"); + return NULL; + } + if (0 > do_load(fd, symtab)) { + fprintf(stderr, "Error ELF parsing %s\n", filename); + free(symtab); + symtab = NULL; + } + close(fd); + return symtab; +} + + +static int +load_memmap(pid_t pid, struct mm *mm, int *nmmp) +{ + char raw[10000]; + char name[MAX_NAME_LEN]; + char *p; + unsigned long start, end; + struct mm *m; + int nmm = 0; + int fd, rv; + int i; + + sprintf(raw, "/proc/%d/maps", pid); + fd = open(raw, O_RDONLY); + if (0 > fd) { + fprintf(stderr, "Can't open %s for reading\n", raw); + return -1; + } + + /* Zero to ensure data is null terminated */ + memset(raw, 0, sizeof(raw)); + + p = raw; + while (1) { + rv = read(fd, p, sizeof(raw)-(p-raw)); + if (0 > rv) { + perror("read"); + return -1; + } + if (0 == rv) + break; + p += rv; + if (p-raw >= sizeof(raw)) { + fprintf(stderr, "Too many memory mapping\n"); + return -1; + } + } + close(fd); + + p = strtok(raw, "\n"); + m = mm; + while (p) { + /* parse current map line */ + rv = sscanf(p, "%08lx-%08lx %*s %*s %*s %*s %s\n", + &start, &end, name); + p = strtok(NULL, "\n"); + + if (rv == 2) { + m = &mm[nmm++]; + m->start = start; + m->end = end; + strcpy(m->name, MEMORY_ONLY); + continue; + } + + /* search backward for other mapping with same name */ + for (i = nmm-1; i >= 0; i--) { + m = &mm[i]; + if (!strcmp(m->name, name)) + break; + } + + if (i >= 0) { + if (start < m->start) + m->start = start; + if (end > m->end) + m->end = end; + } else { + /* new entry */ + m = &mm[nmm++]; + m->start = start; + m->end = end; + strcpy(m->name, name); + } + } + *nmmp = nmm; + return 0; +} + +/* Find libc in MM, storing no more than LEN-1 chars of + its name in NAME and set START to its starting + address. If libc cannot be found return -1 and + leave NAME and START untouched. Otherwise return 0 + and null-terminated NAME. */ +static int +find_libc(char *name, int len, unsigned long *start, + struct mm *mm, int nmm) +{ + int i; + struct mm *m; + char *p; + for (i = 0, m = mm; i < nmm; i++, m++) { + if (!strcmp(m->name, MEMORY_ONLY)) + continue; + p = strrchr(m->name, '/'); + if (!p) + continue; + p++; + if (strncmp("libc", p, 4)) + continue; + p += 4; + + /* here comes our crude test -> 'libc.so' or 'libc-[0-9]' */ + if (!strncmp(".so", p, 3) || (p[0] == '-' && isdigit(p[1]))) + break; + } + if (i >= nmm) + /* not found */ + return -1; + + *start = m->start; + strncpy(name, m->name, len); + if (strlen(m->name) >= len) + name[len-1] = '\0'; + return 0; +} + +static int +lookup2(struct symlist *sl, unsigned char type, + char *name, unsigned long *val) +{ + Elf32_Sym *p; + int len; + int i; + + len = strlen(name); + for (i = 0, p = sl->sym; i < sl->num; i++, p++) { + if (!strcmp(sl->str+p->st_name, name) + && ELF32_ST_TYPE(p->st_info) == type) { + *val = p->st_value; + return 0; + } + } + return -1; +} + +static int +lookup_sym(symtab_t s, unsigned char type, + char *name, unsigned long *val) +{ + if (s->dyn && !lookup2(s->dyn, type, name, val)) + return 0; + if (s->st && !lookup2(s->st, type, name, val)) + return 0; + return -1; +} + +static int +lookup_func_sym(symtab_t s, char *name, unsigned long *val) +{ + return lookup_sym(s, STT_FUNC, name, val); +} + +static int +finddlopen(pid_t pid) +{ + struct mm mm[50]; + unsigned long libcaddr = 0x111111; + int nmm; + char libc[256]; + symtab_t s; + + if (0 > load_memmap(pid, mm, &nmm)) { + fprintf(stderr, "cannot read memory map\n"); + return -1; + } + if (0 > find_libc(libc, sizeof(libc), &libcaddr, mm, nmm)) { + fprintf(stderr, "cannot find libc\n"); + return -1; + } + s = load_symtab(libc); + if (!s) { + fprintf(stderr, "cannot read symbol table\n"); + return -1; + } +#define l(n_, s_) \ + if (0 > lookup_func_sym(s, n_, &s_)) { \ + fprintf(stderr, "cannot find "n_"\n"); \ + return -1; \ + } \ + s_ += libcaddr; \ + printf("%s: %lx <- libc: %lx\n", n_, s_, libcaddr); + l("open", openaddr); + l("dup", dupaddr); + l("dup2", dup2addr); + l("raise", raiseaddr); + l("ioctl", ioctladdr); + l("close", closeaddr); + return 0; +} + +/* Write NLONG 4 byte words from BUF into PID starting + at address POS. Calling process must be attached to PID. */ +static int +write_mem(pid_t pid, unsigned long *buf, int nlong, unsigned long pos) +{ + unsigned long *p; + int i; + + for (p = buf, i = 0; i < nlong; p++, i++) + if (0 > ptrace(PTRACE_POKEDATA, pid, pos+(i*4), *p)) + return -1; + return 0; +} + +/* + Stack layout before we hijack the process. + The stack grows up. $OLDEIP is EIP when we attach. + Args to _dl_open are passed in registers (EAX,EDX,ECX). + + $NEWESP, EIP -> pushad + mov $NEWESP, esp + call _dl_open + ECX -> mov $OLDESP, esp + popad + ret + EAX -> "libname.so" + $OLDESP -> savedregs (32 bytes) + ESP -> $OLDEIP + Top of user stack +*/ +#if 0 +static char code[] = { + 0x60, /* pushad (0x60) */ + 0xbc, 0x0, 0x0, 0x0, 0x0, /* mov $0, %esp */ + 0xe8, 0x0, 0x0, 0x0, 0x0, /* call $0 */ + 0xbc, 0x0, 0x0, 0x0, 0x0, /* mov $0, %oldesp */ + 0x61, /* popad */ + 0xc3, /* ret */ + 0x0, 0x0 /* pad */ +}; + +enum { + OFF_NEWESP = 2, + OFF_DLOPEN = 7, + OFF_DLRET = 11, + OFF_OLDESP = 12 +}; +#endif + +static char code[] = { + 0x90, + 0x90, + 0x90, + 0x90, + 0x90, + 0x90, + 0x90, + 0x90, + 0x90, + 0x90, + 0x90, + 0x90, + 0x90, + 0x90, + 0x90, + 0x90, +/* 3+1:*/ 0x60, /* pushad */ +/* 0: */ 0x55, /* push %ebp */ +/* 1: */ 0x89, 0xe5, /* mov %esp,%ebp */ +/* 3: */ 0x83, 0xec, 0x68, /* sub $0x18,%esp */ + +/** 6:**/ 0xc7, 0x44, 0x24, 0x04, 0x02, 0x00, 0x00,0x00, // movl $0x2,0x4(%esp) +/** e:**/ 0x8b, 0x45, 0x24, // mov 0x8(%ebp),%eax +/** 11:**/ 0x89, 0x04, 0x24, // mov %eax,(%esp) +/** 14:**/ 0xe8, 0xfc, 0xff, 0xff, 0xff, // call 15 + +/** 19:**/ 0x89, 0x45, 0xf4, // mov %eax,0xfffffffc(%ebp) +/* 6: */ 0xc7, 0x04, 0x24, 0x00, 0x00, 0x00, 0x00, /* movl $0x0,(%esp) */ +/* d: */ 0xe8, 0xfc, 0xff, 0xff, 0xff, /* call e */ + +/* 12: */ 0x89, 0x45, 0xf0, /* mov %eax,0xfffffffc(%ebp) */ +/* 15: */ 0xc7, 0x04, 0x24, 0x01, 0x00, 0x00, 0x00, /* movl $0x1,(%esp) */ +/* 1c: */ 0xe8, 0xfc, 0xff, 0xff, 0xff, /* call 1d */ + +/* 21: */ 0x89, 0x45, 0xec, /* mov %eax,0xfffffff8(%ebp) */ +/* 24: */ 0xc7, 0x04, 0x24, 0x02, 0x00, 0x00, 0x00, /* movl $0x2,(%esp) */ +/* 2b: */ 0xe8, 0xfc, 0xff, 0xff, 0xff, /* call 2c */ + +/* 30: */ 0x89, 0x45, 0xe8, /* mov %eax,0xfffffff4(%ebp) */ +/* 33: */ 0xc7, 0x04, 0x24, 0x00, 0x00, 0x00, 0x00, /* movl $0x0,(%esp) */ +/* 3a: */ 0xe8, 0xfc, 0xff, 0xff, 0xff, /* call 3b */ + +/* 3f: */ 0xc7, 0x04, 0x24, 0x01, 0x00, 0x00, 0x00, /* movl $0x1,(%esp) */ +/* 46: */ 0xe8, 0xfc, 0xff, 0xff, 0xff, /* call 47 */ + +/* 4b: */ 0xc7, 0x04, 0x24, 0x02, 0x00, 0x00, 0x00, /* movl $0x2,(%esp) */ +/* 52: */ 0xe8, 0xfc, 0xff, 0xff, 0xff, /* call 53 */ + +/* 57: */ 0xc7, 0x44, 0x24, 0x04, 0x00, 0x00, 0x00,0x00,/* movl $0x0,0x4(%esp) */ +/* 5f: */ 0x8b, 0x45, 0xf4, /* mov 0x8(%ebp),%eax */ +/* 62: */ 0x89, 0x04, 0x24, /* mov %eax,(%esp) */ +/* 65: */ 0xe8, 0xfc, 0xff, 0xff, 0xff, /* call 66 */ + +/* 6a: */ 0xc7, 0x44, 0x24, 0x04, 0x01, 0x00, 0x00,0x00,/* movl $0x1,0x4(%esp) */ +/* 72: */ 0x8b, 0x45, 0xf4, /* mov 0x8(%ebp),%eax */ +/* 75: */ 0x89, 0x04, 0x24, /* mov %eax,(%esp) */ +/* 78: */ 0xe8, 0xfc, 0xff, 0xff, 0xff, /* call 79 */ + +/* 7d: */ 0xc7, 0x44, 0x24, 0x04, 0x02, 0x00, 0x00,0x00,/* movl $0x2,0x4(%esp) */ +/* 85: */ 0x8b, 0x45, 0xf4, /* mov 0x8(%ebp),%eax */ +/* 88: */ 0x89, 0x04, 0x24, /* mov %eax,(%esp) */ +/* 8b: */ 0xe8, 0xfc, 0xff, 0xff, 0xff, /* call 8c */ + +/* a6: */ 0x8b, 0x45, 0xf4, // mov 0xfffffff4(%ebp),%eax +/* a9: */ 0x89, 0x04, 0x24, // mov %eax,(%esp) +/* ac: */ 0xe8, 0xfc, 0xff, 0xff, 0xff, // call ad +/* b1: */ 0x8d, 0x45, 0xa8, // lea 0xffffffa8(%ebp),%eax +/* b4: */ 0x89, 0x44, 0x24, 0x08, // mov %eax,0x8(%esp) +/* b8: */ 0xc7, 0x44, 0x24, 0x04, 0x01, 0x54, 0x00, // movl $0x5401,0x4(%esp) +/* bf: */ 0x00, +/* c0: */ 0x8b, 0x45, 0xf0, // mov 0xfffffff0(%ebp),%eax +/* c3: */ 0x89, 0x04, 0x24, // mov %eax,(%esp) +/* c6: */ 0xe8, 0xfc, 0xff, 0xff, 0xff, // call c7 +/* cb: */ 0x8d, 0x45, 0xa8, // lea 0xffffffa8(%ebp),%eax +/* ce: */ 0x89, 0x44, 0x24, 0x08, // mov %eax,0x8(%esp) +/* d2: */ 0xc7, 0x44, 0x24, 0x04, 0x02, 0x54, 0x00, // movl $0x5402,0x4(%esp) +/* d9: */ 0x00, +/* da: */ 0xc7, 0x04, 0x24, 0x00, 0x00, 0x00, 0x00, // movl $0x0,(%esp) +/* e1: */ 0xe8, 0xfc, 0xff, 0xff, 0xff, // call e2 +/* e6: */ 0x8d, 0x45, 0xa8, // lea 0xffffffa8(%ebp),%eax +/* e9: */ 0x89, 0x44, 0x24, 0x08, // mov %eax,0x8(%esp) +/* ed: */ 0xc7, 0x44, 0x24, 0x04, 0x01, 0x54, 0x00, // movl $0x5401,0x4(%esp) +/* f4: */ 0x00, +/* f5: */ 0x8b, 0x45, 0xec, // mov 0xffffffec(%ebp),%eax +/* f8: */ 0x89, 0x04, 0x24, // mov %eax,(%esp) +/* fb: */ 0xe8, 0xfc, 0xff, 0xff, 0xff, // call fc +/* 100: */ 0x8d, 0x45, 0xa8, // lea 0xffffffa8(%ebp),%eax +/* 103: */ 0x89, 0x44, 0x24, 0x08, // mov %eax,0x8(%esp) +/* 107: */ 0xc7, 0x44, 0x24, 0x04, 0x02, 0x54, 0x00, // movl $0x5402,0x4(%esp) +/* 10e: */ 0x00, +/* 10f: */ 0xc7, 0x04, 0x24, 0x01, 0x00, 0x00, 0x00, // movl $0x1,(%esp) +/* 116: */ 0xe8, 0xfc, 0xff, 0xff, 0xff, // call 117 +/* 11b: */ 0x8d, 0x45, 0xa8, // lea 0xffffffa8(%ebp),%eax +/* 11e: */ 0x89, 0x44, 0x24, 0x08, // mov %eax,0x8(%esp) +/* 122: */ 0xc7, 0x44, 0x24, 0x04, 0x01, 0x54, 0x00, // movl $0x5401,0x4(%esp) +/* 129: */ 0x00, +/* 12a: */ 0x8b, 0x45, 0xe8, // mov 0xffffffe8(%ebp),%eax +/* 12d: */ 0x89, 0x04, 0x24, // mov %eax,(%esp) +/* 130: */ 0xe8, 0xfc, 0xff, 0xff, 0xff, // call 131 +/* 135: */ 0x8d, 0x45, 0xa8, // lea 0xffffffa8(%ebp),%eax +/* 138: */ 0x89, 0x44, 0x24, 0x08, // mov %eax,0x8(%esp) +/* 13c: */ 0xc7, 0x44, 0x24, 0x04, 0x02, 0x54, 0x00, // movl $0x5402,0x4(%esp) +/* 143: */ 0x00, +/* 144: */ 0xc7, 0x04, 0x24, 0x02, 0x00, 0x00, 0x00, // movl $0x2,(%esp) +/* 14b: */ 0xe8, 0xfc, 0xff, 0xff, 0xff, // call 14c + + +/* 90: */ 0xc7, 0x04, 0x24, 0x1c, 0x00, 0x00, 0x00, /* movl $0x1c,(%esp) */ +/* 97: */ 0xe8, 0xfc, 0xff, 0xff, 0xff, /* call 98 */ +/* 9c: */ 0xc9, /* leave */ +/* 9c+1:*/ 0x61, /* popad */ +///* 3: */ 0x83, 0xc4, 0x00, /* sub $0x18,%esp */ // sizeof(code)+ +//0x81, 0xec, 0x00, 0x00, 0x00, 0x00,//sizeof(code)+n+4 +0x81, 0xc4, 0x00, 0x00, 0x00, 0x00,//sizeof(code)+n+4 +/* 9d: */ 0xc3, /* ret */ +0x00,0x00 +}; + +int oopen[] = {0x16-7}; +int odup[] = {0x1e, 0x2d, 0x3c}; +int oclose[] = {0x4b, 0x57, 0x63, 0xae - 0x7}; +int odup2[] = {0x76, 0x89, 0x9c}; +int oioctl[] = {0xc8-7, 0xe3-7, 0xfd-7, 0x118-7, 0x132-7, 0x14d-7}; +int oraise[] = {0x159-7}; + +#define rewrite(code, offsets, value) rewrite_(code, offsets, sizeof(offsets)/sizeof(*offsets), value) +void +rewrite_(char code[], int offsets[], int n_offsets, unsigned long value) +{ + int i,j; + for (i = 0; i < n_offsets; i++) { + /* :) */ + unsigned long *p = (unsigned long *) &code[23+offsets[i]]; + *p = value - (23+offsets[i]) - 4; + printf("[%x] %lx\n", 7+offsets[i], value); + printf("\t"); + for (j = -1; j < 6; j++) + printf("%x ", (int)code[7+offsets[i]+j]); + printf("\n"); + } +} + +int +main(int argc, char *argv[]) +{ + pid_t pid; + struct user_regs_struct regs; + unsigned long codeaddr, ptsnameaddr; + int fd, n; + char buf[32]; + char *arg; + int ptm; + char *pts; + char stack[128]; + int i; + + if (argc != 2) { + fprintf(stderr, "usage: %s PID\n", argv[0]); + exit(1); + } + pid = strtol(argv[1], NULL, 0); + + if (0 > finddlopen(pid)) { + fprintf(stderr, "parse failed\n"); + exit(1); + } + + /* Setup pty */ + ptm = getpt(); + grantpt(ptm); + unlockpt(ptm); + pts = ptsname(ptm); + + tcflush(ptm, TCIOFLUSH); + (void) ioctl(ptm, TIOCEXCL, (char *) 0); + + n = strlen(pts)+1; + n = n/4 + (n%4 ? 1 : 0); + arg = xmalloc(n*sizeof(unsigned long)); + memcpy(arg, pts, n*4); + + + /* Attach */ + if (0 > ptrace(PTRACE_ATTACH, pid, 0, 0)) { + fprintf(stderr, "cannot attach to %d\n", pid); + exit(1); + } + waitpid(pid, NULL, 0); + sprintf(buf, "/proc/%d/mem", pid); + fd = open(buf, O_WRONLY); + if (0 > fd) { + fprintf(stderr, "cannot open %s\n", buf); + exit(1); + } + ptrace(PTRACE_GETREGS, pid, 0, ®s); + + for (i=0; i<32; i++) + ptrace(PTRACE_PEEKDATA, pid, regs.esp+i*4, stack+i*4); + + /* push EIP */ + regs.esp -= 4; + ptrace(PTRACE_POKEDATA, pid, regs.esp, regs.eip); + + /* finish code and push it */ + regs.esp -= sizeof(code); + codeaddr = regs.esp; + printf("codesize: %x codeaddr: %lx sizeof(long): %d\n", sizeof(code), codeaddr, sizeof(long)); + rewrite(code, oopen, openaddr - codeaddr); + rewrite(code, odup, dupaddr - codeaddr); + rewrite(code, odup2, dup2addr - codeaddr); + rewrite(code, oclose, closeaddr - codeaddr); + rewrite(code, oioctl, ioctladdr - codeaddr); + rewrite(code, oraise, raiseaddr - codeaddr); + *((int*)&code[sizeof(code)-7]) = sizeof(code) + n*4 + 4; + if (0 > write_mem(pid, (unsigned long*)&code, sizeof(code)/sizeof(long), regs.esp)) { + fprintf(stderr, "cannot write code\n"); + exit(1); + } + + /* push ptsname[] */ + regs.esp -= n*4; + ptsnameaddr = regs.esp; + if (0 > write_mem(pid, (unsigned long*)arg, n, regs.esp)) { + fprintf(stderr, "cannot write bla argument (%s)\n", + strerror(errno)); + exit(1); + } + + /* push ptsname */ + regs.esp -= 4; + ptrace(PTRACE_POKEDATA, pid, regs.esp, ptsnameaddr); + + regs.eip = codeaddr+8; + printf("stack: %lx eip: %lx sub:%x\n", regs.esp, regs.eip, (int) code[sizeof(code)-7]); + + /* Detach and continue */ + ptrace(PTRACE_SETREGS, pid, 0, ®s); + kill(pid, SIGWINCH); + ptrace(PTRACE_DETACH, pid, 0, 0); + + while (1) { + struct termios t; + fd_set fds; + + FD_ZERO(&fds); + FD_SET(ptm, &fds); + FD_SET(0, &fds); + select(ptm+1, &fds, NULL, NULL, NULL); + + ioctl(ptm, TCGETS, &t); + ioctl(0, TCSETS, &t); + + if (FD_ISSET(ptm, &fds)) { + char buf[256]; + ssize_t len = read(ptm, buf, 256); + write(1, buf, len); + } + + if (FD_ISSET(0, &fds)) { + char buf[256]; + ssize_t len = read(0, buf, 256); + write(ptm, buf, len); + } + } + + return 0; +} -- 2.11.4.GIT