{{{~cpp 소규모 프로젝트를 진행 하려 한다. 예상 라인수는 2000~3000 라인 정도이며 프로젝트를 더 커질수록 라인수의 가변성 또한 존재한다. // 한가지 웃긴것은 C++로 짜면 C와 동일한 기능을 가졌음에도 라인의 수가 3배 이상 늘어난다는 것이다. // 6000~9000라인 정도 되려나... // 그런데 더 웃긴것은 C++에 숙달되면 C보다 0.8배 정도로 시간이 단축된다는 것이다. // 클래스의 상속성으로 인해 기존의 클래스를 고치지 않아서 인것 같다. 만들것 : IRC bot 목적 : irc에 접속해 있는 유저들을 자동적으로 공격하는 프로그램. & 차후 AI(망할 엉터리 AI 패러다임) bot이나 타자봇을 집어넣을 예정 OS : Linux 체제 Language : C & Linux System Function 방법론 : 프로세스를 여러개 만들어 비교적 프로그램이 쉬워지게 만든다. 주의점 : Zombie Process를 만들지 않도록 System Call 을 잘 관리한다. 1. Client Console에 메세지를 입력하면 IRC Server로 문자열을 전송한다. -> Main Process 2. 서버로부터 메세지 중 PING 부분 처리 -> 1번째 Child Process 3. 서버의 메세지 중 타유저들이 명령하는 것 처리 -> 2번째 Chile Process (3번에서 Master가 누군지 알아보게 하는 것 -> Private 메세지로 패스워드를 넘겨 IP를 인증 받는 방식.) main.c -> IRC Server로 메세지를 보내는 역할을 하고 자식 프로세스를 생성한다. parse.c -> IRC Server로 부터 오는 메세지를 파싱한다. file.c -> 파일 입출력을 다루는 함수와 메세지들을 Log하는 부분을 담당한다. 일단 프로그램을 어느 정도 만들고 구현하도록한다. request.c -> IRC Server로 부터 날아오는 PING에 대한 PONG 처리. bot.c -> 봇에 관한 함수. attack_#.c(# == 임의의 숫자) -> 공격 함수. 요즘 보안홀들을 체크하여 보안홀들을 공격하는 함수들을 집어넣자. // 향후 프로그램이 커지면 네트워크 부분은 따로 모듈을 만들어 낸다. connect.c -> 네트워크 부분. // 아마 커진것 같다. (2일에 한번씩 컴퓨터를 사용하므로...) 우선 구현할 부분 : main의 일부, parse의 PING 처리부분, request 부분 나중에 아랫부분만 컴파일 해보자. 컴파일러가 없다! 에러가 나올지 모른다. // signal은 나중에. // fork 부분만 구현한다. }}} {{{~cpp // 구분하지 않은 파일. 이 파일을 각 파일로 나눈다. // 심심해서 짜본걸 여기에 적음. // 자동으로 #linux 채널까지 접속 됨. // 각파일로 나눈 차후 채널로의 privmsg와 process call을 mirc처럼 분리해서 넣어야함. #include #include #include #include #include #include #include #include #include #define MSG_MAX 1024 #define NICK "whoami_" #define HOST "irc.hanirc.org" #define PORT "6667" void error_handling(char *msg); void handler(int sig); typedef struct _info{ char *nick; char *host; char *port; char *user_name; char *user_whois1; char *user3; char *user_whois2; } INFO; INFO info = {NICK, HOST, PORT, "a", "b", "c", "d"}; int main(int argc, char *argv[]) { int sockfd; struct sockaddr_in ina; struct sigaction act; struct hostent *h; pid_t pid; char msg[MSG_MAX]; int msg_len; char check_ping[7]; char pong[11]; int join_flag = 0; sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if(sockfd == -1) error_handling("socket() error: sockfd"); h = gethostbyname(info.host); memset(&ina, 0, sizeof(ina)); ina.sin_family = AF_INET; ina.sin_addr = *(struct in_addr *)h->h_addr_list[0]; ina.sin_port = htons(atoi(info.port)); if(connect(sockfd, (struct sockaddr *)&ina, sizeof(ina)) == -1) error_handling("connect() error: "); act.sa_handler = handler; sigemptyset(&act.sa_mask); act.sa_flags = 0; sigaction(SIGCHLD, &act, 0); pid = fork(); if(pid == -1) error_handling("fork() error: "); else if(pid == 0){ while(1){ msg_len = recv(sockfd, msg, MSG_MAX-1, 0); if(msg_len > 0){ msg[msg_len] = ''; printf(msg); } if(strstr(msg, "NOTICE AUTH :*** Checking Ident") != NULL){ sleep(1); sprintf(msg, "user %s %s %s %sn", info.user_name, info.user_whois1, info.user3, info.user_whois2); send(sockfd, msg, strlen(msg), 0); sprintf(msg, "NICK %sn", info.nick); send(sockfd, msg, strlen(msg), 0); printf("Connect Initialization Succeed!n"); } else{ sscanf(msg, "%6c%s",check_ping, pong); if(strncmp(check_ping, "PING :", 6) == 0){ sprintf(msg, "PONG %sn", pong); send(sockfd, msg, strlen(msg), 0); if(join_flag == 0){ sleep(1); join_flag = 1; send(sockfd, "join #linuxn", 12, 0); } } } msg[0] = ''; } exit(0); } else{ fprintf(stderr, "Child Process Created!: pid = %dn", pid); } while(1){ fgets(msg, MSG_MAX, stdin); send(sockfd, msg, strlen(msg), 0); if(strcmp(msg, "quitn") == 0) break; } kill(0, SIGKILL); // 부모가 종료하면 자식 프로세스 모두 종료한다. return 0; } void error_handling(char *msg) { fprintf(stderr, "%sn", msg); exit(-1); } void handler(int sig) { pid_t pid; int last; pid = waitpid(-1, &last, WNOHANG); fprintf(stderr, "Child Process Closed!: pid = %d, WEXITSTATUS = %Xn", pid, WEXITSTATUS(last)); } }}}