SHARE
TWEET

bindp revision

a guest Mar 6th, 2017 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int bind (int fd, const struct sockaddr *sk, socklen_t sl){
  2.     int debug_enabled = 1 ;
  3.     /*
  4.         As defined in linux/socket.h ,__kernel_sa_family_t is 2 bytes wide.
  5.         We read the first two bytes of sk without using cast to protocol families
  6.    
  7.     */
  8.    
  9.     unsigned short _pf = *((unsigned short*) sk);
  10.     switch (_pf){
  11.     case AF_INET:
  12.     {
  13.         static struct sockaddr_in *lsk_in;
  14.  
  15.         lsk_in = (struct sockaddr_in *)sk;
  16.        
  17.         if (debug_enabled){
  18.             char original_ip [INET_ADDRSTRLEN];
  19.             inet_ntop(AF_INET,&(lsk_in->sin_addr),original_ip,INET_ADDRSTRLEN);
  20.             int original_port = ntohs(lsk_in->sin_port);
  21.             char *l_bind_addr = getenv ("BIND_ADDR");
  22.             char *l_bind_port = getenv ("BIND_PORT");
  23.             printf("[-] LIB received AF_INET bind request\n");
  24.             if (l_bind_addr && l_bind_port){                    
  25.                 printf("[-] Changing %s:%d to %s:%s\n" , original_ip,original_port,l_bind_addr,l_bind_port);      
  26.             }else{            
  27.                 printf("[!] AF_INET: Leaving request unchanged\n");
  28.             }      
  29.         }
  30.        
  31.  
  32.         if(bind_addr_saddr)
  33.             lsk_in->sin_addr.s_addr = bind_addr_saddr;
  34.  
  35.         if (bind_port_saddr)
  36.             lsk_in->sin_port = htons (bind_port_saddr);
  37.        
  38.     }
  39.         break;
  40.     case AF_UNIX:
  41.         printf("[-] LIB received AF_UNIX bind request\n");
  42.         printf("[-] AF_UNIX: Leaving request unchanged\n");
  43.         break;
  44.        
  45.     /*
  46.         Other families handling
  47.        
  48.     */
  49.    
  50.     default:
  51.    
  52.         break;
  53.  
  54.     }
  55.  
  56.    
  57.    
  58.     /*
  59.         FIXME: Be careful when using setsockopt  
  60.         Is it valid to use these options for AF_UNIX?
  61.         Must be checked
  62.    
  63.     */
  64.     if (reuse_addr){
  65.         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_addr, sizeof(reuse_addr));
  66.     }
  67.  
  68. #ifdef SO_REUSEPORT
  69.     if (reuse_port){
  70.         setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &reuse_port, sizeof(reuse_port));
  71.     }
  72. #endif
  73.  
  74.     if (ip_transparent){
  75.         int opt =1;
  76.         setsockopt(fd, SOL_IP, IP_TRANSPARENT, &ip_transparent, sizeof(ip_transparent));
  77.     }
  78.  
  79.     return real_bind (fd, sk, sl);
  80. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top