Advertisement
GodlyPacketz

RAW TCP Server Base

Oct 9th, 2017
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.94 KB | None | 0 0
  1. /*
  2. This is a basic RAW TCP server. You can use this to make a chat room type thing.
  3. Made by DaddyL33T
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>
  11. #include <unistd.h>
  12. #define MAXFDS 1000000
  13. #define BUFFER_SIZE 1024
  14. #define on_error(...) { fprintf(stderr, __VA_ARGS__); fflush(stderr); exit(1); }
  15. static volatile int OperatorsConnected = 0;
  16. struct login_info {
  17.     char username[20];
  18.     char password[20];
  19. };
  20. static struct login_info accounts[22];
  21. struct telnetdata_t {
  22.         int connected;
  23. } managements[MAXFDS];
  24. struct args {
  25.         int sock;
  26.         struct sockaddr_in cli_addr;
  27. };
  28. int fdgets(unsigned char *buffer, int bufferSize, int fd) {
  29.     int total = 0, got = 1;
  30.     while(got == 1 && total < bufferSize && *(buffer + total - 1) != '\n') { got = read(fd, buffer + total, 1); total++; }
  31.     return got;
  32. }
  33. void trim(char *str) {
  34.     int i;
  35.     int begin = 0;
  36.     int end = strlen(str) - 1;
  37.     while (isspace(str[begin])) begin++;
  38.     while ((end >= begin) && isspace(str[end])) end--;
  39.     for (i = begin; i <= end; i++) str[i - begin] = str[i];
  40.     str[i - begin] = '\0';
  41. }
  42. int Search_in_File(char *str)
  43. {
  44.     FILE *fp;
  45.     int line_num = 0;
  46.     int find_result = 0, find_line=0;
  47.     char temp[512];
  48.  
  49.     if((fp = fopen("logins.txt", "r")) == NULL){
  50.         return(-1);
  51.     }
  52.     while(fgets(temp, 512, fp) != NULL){
  53.         if((strstr(temp, str)) != NULL){
  54.             find_result++;
  55.             find_line = line_num;
  56.         }
  57.         line_num++;
  58.     }
  59.     if(fp)
  60.         fclose(fp);
  61.  
  62.     if(find_result == 0)return 0;
  63.  
  64.     return find_line;
  65. }
  66. void *titleWriter(void *sock)
  67. {
  68.         int thefd = (int)sock;
  69.         char string[2048];
  70.         while(1)
  71.         {
  72.                 memset(string, 0, 2048);
  73.         sprintf(string, "%c]0; [+] Simple Telnet Base [+]%c", '\033', '\007');
  74.                 if(send(thefd, string, strlen(string), MSG_NOSIGNAL) == -1) return;
  75.  
  76.                 sleep(3);
  77.         }
  78. }
  79. void broadcast(char *msg, int us, char *sender)
  80. {
  81.         int sendMGM = 1;
  82.         if(strcmp(msg, "PING") == 0) sendMGM = 0;
  83.         char *wot = malloc(strlen(msg) + 10);
  84.         memset(wot, 0, strlen(msg) + 10);
  85.         strcpy(wot, msg);
  86.         trim(wot);
  87.         time_t rawtime;
  88.         struct tm * timeinfo;
  89.         time(&rawtime);
  90.         timeinfo = localtime(&rawtime);
  91.         char *timestamp = asctime(timeinfo);
  92.         trim(timestamp);
  93.         int i;
  94.         for(i = 0; i < MAXFDS; i++)
  95.         {
  96.                 if(i == us || (sendMGM == 0 || !managements[i].connected)) continue;
  97.                 if(sendMGM && managements[i].connected)
  98.                 {
  99.                         send(i, "\x1b[1;35m", 9, MSG_NOSIGNAL);
  100.                         send(i, sender, strlen(sender), MSG_NOSIGNAL);
  101.                         send(i, ": ", 2, MSG_NOSIGNAL);
  102.                 }
  103.                 send(i, msg, strlen(msg), MSG_NOSIGNAL);
  104.                 if(sendMGM && managements[i].connected) send(i, "\r\n\x1b[1;31m~>\x1b[36m ", 19, MSG_NOSIGNAL);
  105.                 else send(i, "\n", 1, MSG_NOSIGNAL);
  106.         }
  107.         free(wot);
  108. }
  109. void *mainShit(void *sock)
  110. {
  111.         char usernamez[80];
  112.         int thefd = (int)sock;
  113.         int find_line;
  114.         OperatorsConnected++;
  115.         char counter[2048];
  116.         memset(counter, 0, 2048);
  117.         char buf[2048];
  118.         char* nickstring;
  119.         char* username;
  120.         char* password;
  121.         memset(buf, 0, sizeof buf);
  122.         char botnet[2048];
  123.         memset(botnet, 0, 2048);
  124.         FILE *fp;
  125.         int i=0;
  126.         int c;
  127.         fp=fopen("logins.txt", "r");
  128.         while(!feof(fp))
  129.         {
  130.                 c=fgetc(fp);
  131.                 ++i;
  132.         }
  133.         int j=0;
  134.         rewind(fp);
  135.         while(j!=i-1)
  136.         {
  137.             fscanf(fp, "%s %s", accounts[j].username, accounts[j].password);
  138.             ++j;
  139.         }
  140.        
  141.         if(send(thefd, "\x1b[35mName:\x1b[33m ", 16, MSG_NOSIGNAL) == -1) goto end;
  142.         if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  143.         trim(buf);
  144.         sprintf(usernamez, buf);
  145.         nickstring = ("%s", buf);
  146.         find_line = Search_in_File(nickstring);
  147.         if(strcmp(nickstring, accounts[find_line].username) == 0){                 
  148.         if(send(thefd, "\x1b[35mPasscode:\x1b[30m ", 20, MSG_NOSIGNAL) == -1) goto end;
  149.         if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  150.         trim(buf);
  151.         if(strcmp(buf, accounts[find_line].password) != 0) goto failed;
  152.         memset(buf, 0, 2048);
  153.         if(send(thefd, "\x1b[0;37m--->   Credentials Are Correct   <---\r\n--->Please Wait While We Prepare Stuff<---\r\n", 100, MSG_NOSIGNAL) == -1) goto end;
  154.         sleep(3);
  155.         goto dope;
  156.         }
  157.         failed:
  158.         if(send(thefd, "\033[1A", 6, MSG_NOSIGNAL) == -1) goto end;
  159.         if(send(thefd, "\x1b[0;37m***********************************\r\n", 50, MSG_NOSIGNAL) == -1) goto end;
  160.         if(send(thefd, "\x1b[0;37m*  Incorrect Credentials!         *\r\n", 50, MSG_NOSIGNAL) == -1) goto end;
  161.         if(send(thefd, "\x1b[0;37m***********************************\r\n", 50, MSG_NOSIGNAL) == -1) goto end;
  162.             sleep(5);
  163.         goto end;
  164.         dope:
  165.         titleWriter(sock);
  166.         char banner1 [80];
  167.         char banner2 [80];
  168.         char banner3 [80];
  169.         sprintf(banner1, "\x1b[0;37mThis is a blank TCP server coded in C\r\n");
  170.         sprintf(banner2, "\x1b[0;37mThere Are Currently %s People Connected\r\n", OperatorsConnected);
  171.         sprintf(banner3, "\x1b[0;37mType [1;35mHELP [0;37mfor commands\r\n");
  172.         char clearscreen [2048];
  173.         memset(clearscreen, 0, 2048);
  174.         if(send(thefd, "\033[2J\033[1;1H", 14, MSG_NOSIGNAL) == -1) goto end;
  175.         if(send(thefd, banner1, strlen(banner1), MSG_NOSIGNAL) == -1) goto end;
  176.         if(send(thefd, banner2, strlen(banner2), MSG_NOSIGNAL) == -1) goto end;
  177.         if(send(thefd, banner3, strlen(banner3), MSG_NOSIGNAL) == -1) goto end;
  178.         while(1) {
  179.         if(send(thefd, "\x1b[1;31m~>\x1b[36m ", 19, MSG_NOSIGNAL) == -1) goto end;
  180.         break;
  181.         }
  182.         managements[thefd].connected = 1;
  183.         while(fdgets(buf, sizeof buf, thefd) > 0)
  184.         {
  185.             if(strstr(buf, "HELP")) {
  186.                 char helpline1  [80];
  187.                 char helpline2  [80];
  188.                 char helpline3  [80];
  189.                 char helpline4  [80];
  190.  
  191.                 sprintf(helpline1,  "    \x1b[0;31m[+] [1;35mCommands [0;31m[+]\r\n");
  192.                 sprintf(helpline2,  "      CLEAR---| Clears The Screen?\r\n");
  193.                 sprintf(helpline3,  "      HELP----| Shows This Ya Retard\r\n");
  194.                 sprintf(helpline4,  "      EXIT----| Disconnects From This\r\n");;
  195.  
  196.                 if(send(thefd, helpline1,  strlen(helpline1),   MSG_NOSIGNAL) == -1) goto end;
  197.                 if(send(thefd, helpline2,  strlen(helpline2),   MSG_NOSIGNAL) == -1) goto end;
  198.                 if(send(thefd, helpline3,  strlen(helpline3),   MSG_NOSIGNAL) == -1) goto end;
  199.                 if(send(thefd, helpline4,  strlen(helpline4),   MSG_NOSIGNAL) == -1) goto end;
  200.                 if(send(thefd, "\x1b[1;31m~>\x1b[36m ", 19, MSG_NOSIGNAL) == -1) goto end;
  201.                 continue;
  202.             }
  203.             if(strstr(buf, "CLEAR")){
  204.                 if(send(thefd, "\033[2J\033[1;1H", 14, MSG_NOSIGNAL) == -1) goto end;
  205.                 if(send(thefd, banner1, strlen(banner1), MSG_NOSIGNAL) == -1) goto end;
  206.                 if(send(thefd, banner2, strlen(banner2), MSG_NOSIGNAL) == -1) goto end;
  207.                 if(send(thefd, banner3, strlen(banner3), MSG_NOSIGNAL) == -1) goto end;
  208.             }
  209.             if(strstr(buf, "LOGOUT")) {  
  210.                 sprintf(botnet, "Goodbye -> %s\r\n", accounts[find_line].username, buf);
  211.                 goto end;
  212.             }
  213.             trim(buf);
  214.             if(send(thefd, "\x1b[1;31m~>\x1b[36m ", 19, MSG_NOSIGNAL) == -1) goto end;
  215.             if(strlen(buf) == 0) continue;
  216.             printf("%s: \"%s\"\n",accounts[find_line].username, buf);
  217.             FILE *logFile;
  218.             logFile = fopen("Activity.log", "a");
  219.             fprintf(logFile, "%s: \"%s\"\n",accounts[find_line].username, buf);
  220.             fclose(logFile);
  221.             broadcast(buf, thefd, usernamez);
  222.             memset(buf, 0, 2048);
  223.         }
  224.        
  225.        
  226.        
  227.         end:
  228.         managements[thefd].connected = 0;
  229.         close(thefd);
  230.         OperatorsConnected--;
  231. }
  232. int main (int argc, char *argv[]) {
  233.   if (argc < 2) on_error("Usage: %s [port]\n", argv[0]);
  234.  
  235.   int port = atoi(argv[1]);
  236.  
  237.   int server_fd, client_fd, err;
  238.   struct sockaddr_in server, client;
  239.   char buf[BUFFER_SIZE];
  240.  
  241.   server_fd = socket(AF_INET, SOCK_STREAM, 0);
  242.   if (server_fd < 0) on_error("Could not create socket\n");
  243.  
  244.   server.sin_family = AF_INET;
  245.   server.sin_port = htons(port);
  246.   server.sin_addr.s_addr = htonl(INADDR_ANY);
  247.  
  248.   int opt_val = 1;
  249.   setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt_val, sizeof opt_val);
  250.  
  251.   err = bind(server_fd, (struct sockaddr *) &server, sizeof(server));
  252.   if (err < 0) on_error("Could not bind socket\n");
  253.  
  254.   err = listen(server_fd, 128);
  255.   if (err < 0) on_error("Could not listen on socket\n");
  256.  
  257.   printf("Server is listening on %d\n", port);
  258.  
  259.   while (1) {
  260.     socklen_t client_len = sizeof(client);
  261.     client_fd = accept(server_fd, (struct sockaddr *) &client, &client_len);
  262.     if (client_fd < 0) on_error("Could not establish new connection\n");
  263.     mainShit(client_fd);
  264.   }
  265.  
  266.   return 0;
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement