ajithkp560

Buffer Overflow Tutorial - Socket Program

Jan 19th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netdb.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. /*
  7. http://www.terminalcoders.blogspot.com
  8. BOF Tutorial
  9. Video: https://youtu.be/uPfBkU0LqBA
  10. */
  11. void copy(char str[8000]){ //Vulnerable function
  12.   char cpy[84];
  13.   strcpy(cpy, str); //Vulnerable section
  14. }
  15. void start_server(){
  16.     char str[8000], cpy[64];
  17.     int sfd, cfd;
  18.  
  19.     struct sockaddr_in sock;
  20.  
  21.     sfd = socket(AF_INET, SOCK_STREAM, 0);
  22.  
  23.     bzero(&sock, sizeof(sock));
  24.  
  25.     sock.sin_family = AF_INET;
  26.     sock.sin_addr.s_addr = htons(INADDR_ANY);
  27.     sock.sin_port = htons(5601); //Binding port
  28.  
  29.     bind(sfd, (struct sockaddr *) &sock, sizeof(sock));
  30.  
  31.     listen(sfd, 10);
  32.  
  33.     cfd = accept(sfd, (struct sockaddr*) NULL, NULL);
  34.  
  35.     while(1){
  36.         read(cfd,str,8000);
  37.         copy(str);
  38.         puts(str);
  39.         write(cfd, str, strlen(str)+1);
  40.     }
  41. }
  42. int main(int argc, char **argv){
  43.     start_server();
  44. }
Advertisement
Add Comment
Please, Sign In to add comment