~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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#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));
}