Skip to content

Instantly share code, notes, and snippets.

@AmarOk1412
Last active Apr 19, 2018
Embed
What would you like to do?
#include <stdio.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <service.h>
int compute(char part[12], int base) {
char* p = part;
int v = base;
while ((*p) && (p < part + 8))
v = v + *(p++);
return v;
}
int validate (char* sn) {
int value1, value2;
char buf[8];
sn[8] = 0;
strcpy(buf, sn);
value1 = compute(buf, 0);
strcpy(buf, (sn + 9));
value2 = compute(buf, 0x41414141);
return value1 == value2;
}
void session(int cliSd)
{
char buff[28];
int recv_size, end_ptr = 0, done = 0;
struct sockaddr_in cliAddr;
int cliLen;
cliLen = sizeof(cliAddr);
getpeername(cliSd, (struct sockaddr *)&cliAddr, &cliLen);
printf("%s\t: Connected\n", inet_ntoa(cliAddr.sin_addr));
send (cliSd, "Please enter your serial number:\r\n", 35, 0);
while (!done) {
recv_size = recv (cliSd, (char *) buff + end_ptr, sizeof (buff) - end_ptr, 0);
if (recv_size == 0) break;
if (recv_size == -1) continue;
end_ptr += recv_size;
if ((buff[end_ptr - 1] == 0) && end_ptr > 0) end_ptr--;
if ((buff[end_ptr - 1] == '\n') || (buff[end_ptr - 1] == '\r')) {
buff[end_ptr - 1] = 0;
if (validate(buff)) {
done = 1;
printf("%s\t: Succeeded!! <------------------\n", inet_ntoa(cliAddr.sin_addr));
send (cliSd, "Thanks you for your registration.\r\n", 36, 0);
send (cliSd, "The confirmation number is " CONFIRM_CODE "\r\n", 30 + sizeof(CONFIRM_CODE), 0);
} else {
send (cliSd, "Sorry, wrong serial number.\r\n", 29, 0);
end_ptr = 0;
}
}
}
printf("%s\t: Disconnected or bad overflow\n", inet_ntoa(cliAddr.sin_addr));
close(cliSd);
}
int main () {
struct sockaddr_in servAddr;
int servSd, cliSd;
int one = 1;
servSd = socket (PF_INET, SOCK_STREAM, 0);
setsockopt (servSd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof (one));
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons (12345);
servAddr.sin_addr.s_addr = htonl (INADDR_ANY);
if (bind (servSd, (struct sockaddr *) &servAddr, sizeof (servAddr)) < 0){ perror ("Can't bind\n"); return 1;}
if (listen (servSd, 3) < 0) { perror ("Can't listen\n"); return 1;}
while (1){
printf ("Server is ready!\n");
cliSd = accept (servSd, NULL, NULL);
if (fork()==0){
session(cliSd);
return 0;
}
}
return 0;
}
@AmarOk1412
Copy link
Author

AmarOk1412 commented Apr 19, 2018

service.h =

#define CONFIRM_CODE "local_confirmation_code"

To compile

gcc -I./ -fno-stack-protector service.c -o serviceExecute -m32

To test:

./serviceExecute

# and for the other
nc localhost 12345

Goal get The confirmation number is xxxxxxx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment