Skip to content

Instantly share code, notes, and snippets.

@guilt
Last active December 13, 2023 13:59
Show Gist options
  • Save guilt/3e77d19c71d52252fd3c2ddc6196cc4c to your computer and use it in GitHub Desktop.
Save guilt/3e77d19c71d52252fd3c2ddc6196cc4c to your computer and use it in GitHub Desktop.
NSF hosted CTF ca. 2007
//Centralized password authentication server.
//Written by clever kid.
#include <netlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define PASSFILE "password.txt"
#define SERV_HOST "0.0.0.0"
#define SERV_PORT "24312"
#define PASSLEN 256
#define REPLYLEN 256
//#define FORKED
char passwd[PASSLEN];
int handleMe(fd *accept, char *ipbuff){
int replylen;
char readpasswd[PASSLEN];
char reply[REPLYLEN];
printf("Connection from : %s\n",ipbuff);
*readpasswd=0;
*reply=0;
replylen=0;
if(Readline(*accept,readpasswd,PASSLEN)<0)
return NET_ERROR;
if(strncmp(readpasswd,passwd,PASSLEN)) {
//TODO
//replylen=snprintf(reply,REPLYLEN,readpasswd,"Read: %s Original: %s",passwd);
replylen=snprintf(reply,REPLYLEN,readpasswd,"%s",passwd);
} else {
replylen=snprintf(reply,REPLYLEN,"%s","Success");
}
if(Writeline(*accept,reply)<replylen)
return NET_ERROR;
return NET_OK;
}
int readPasswd(){
int i=0;
FILE *fp=fopen(PASSFILE,"rt");
*passwd=0;
if(!fp) return -1;
fgets(passwd,PASSLEN,fp);
for(;passwd[i]&&i<PASSLEN;++i) {
if(passwd[i]=='\r'||passwd[i]=='\n'||passwd[i]==-1) {
passwd[i]=0;
}
}
fclose(fp); fp=NULL;
return 0;
}
int main(){
fd in,accept;
int status;
char ipbuff[256];
#ifdef FORKED
pid_t pid;
char *tmpipbuff;
#endif
srand(time(NULL));
if(Initialize()<0) {
printf("Data Error\n");
return -1;
}
if(readPasswd()<0) {
printf("Data Error\n");
return -1;
}
if(Bind(&in,SERV_HOST,SERV_PORT)<0) {
printf("Bind Error\n");
return -1;
}
printf("Bound on : %s:%s\n",SERV_HOST,SERV_PORT);
while(1) {
if(Accept(&in,&accept,ipbuff,NULL)<0){
printf("Accept Error\n");
continue;
}
#ifdef FORKED
tmpipbuff=malloc(sizeof(char)*256);
if(!tmpipbuff) {
printf("Unable to alloc Client Details Buffer\n");
Close(&accept);
continue;
}
strncpy(tmpipbuff,ipbuff,256*sizeof(char));
pid=fork();
if(pid<0) {
printf("Fork Error\n");
Close(&accept);
continue;
}
if(!pid){
Close(&in);
printf("Child: %d\n",getpid());
status=handleMe(&accept,tmpipbuff);
free(tmpipbuff);
Close(&accept);
return status;
} else {
Close(&accept);
}
#else
status=handleMe(&accept,ipbuff);
Close(&accept);
#endif
}
Close(&in);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment