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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| //client.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 80
#define IP "192.168.1.1"
int connected(int fd,char *ip)
{
struct hostent *he;
struct sockaddr_in their_addr;
if ((he=gethostbyname(ip)) == NULL) {
herror("gethostbyname");
exit(1);
}
printf("[+] Connect to %s port %d\n",ip,PORT);
if ((fd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
perror("[+] Socket");
exit(1);
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(PORT);
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);
if (connect(fd, (struct sockaddr *)&their_addr,sizeof their_addr) == -1) {
perror("[+] Connect");
exit(1);
}else{printf("[+] Connected!\n\n");}
return(fd);
}
int packet(int fd,char *buffer){
if(write(fd,buffer,strlen(buffer))==-1){
perror("[+] Write ");
return 0;
}else{printf("[+] Requete send!\n");}
return 1;
}
int requete(int fd)
{
char *requete_post = "POST /~y0ug/up.php HTTP/1.1\r\n"\
"User-Agent: Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.8.1.6) Gecko/20070805 Firefox/2.0.0.6\r\n"\
"Host: 192.168.1.1\r\n"\
"Connection: Keep-Alive\r\n"\
"Cache-Control: no-cache\r\n";
char *requete_boundary = "Content-Type: multipart/form-data; boundary=-----------------------------myboundary\r\n";
char *requete_rn = "Content-Length: 178"\
"\r\n";
char *requete_boundary2 = "\r\n"\
"-----------------------------myboundary\r\n"\
"Content-Disposition: form-data; name='file'; filename='fichier.txt'\r\n"\
"Content-Type: text/plain\r\n"\
"\r\n";
char *fichier = "mon super fichier ....";
char *fin = "-----------------------------myboundary\r\n"\
"Content-Disposition: form-data; name='envoyer'\r\n"\
"\r\n"\
"send\r\n"\
"-----------------------------myboundary--\r\n";
packet(fd,requete_post);
packet(fd,requete_boundary);
packet(fd,requete_rn);
//packet(fd,requete_boundary2);
//packet(fd,fichier);
packet(fd,fin);
return 1;
}
int receive(int fd)
{
char *buffer[512];
int a=recv(fd,buffer,511,0);
printf("Receive:\n%s\n",buffer);
}
int main (int argc, char **argv)
{
printf("[+] PORT: %d\n",PORT);
printf("[+] IP: %s\n\n",IP);
int fd = connected(fd,IP);
requete(fd);
receive(fd);
close(fd);
} |
Partager