⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ec_inet_macosx.c

📁 Ettercap最初设计为交换网上的sniffer
💻 C
📖 第 1 页 / 共 2 页
字号:
/*    ettercap -- inet utilities -- Module for MacOsX darwin 1.[34]                                                    darwin 5.[012]    Copyright (C) 2001  ALoR <alor@users.sourceforge.net>, NaGA <crwm@freemail.it>    This program is free software; you can redistribute it and/or modify    it under the terms of the GNU General Public License as published by    the Free Software Foundation; either version 2 of the License, or    (at your option) any later version.    This program is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    GNU General Public License for more details.    You should have received a copy of the GNU General Public License    along with this program; if not, write to the Free Software    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.    $Id: ec_inet_macosx.c,v 1.14 2002/06/12 16:19:38 alor Exp $*/#include "../../include/ec_main.h"#include "../../include/ec_thread.h"#include "../../include/ec_buffer.h"#include "../../include/ec_inet.h"#include "../../include/ec_inet_forge.h"#include "../../include/ec_inet_structures.h"#include <ctype.h>#include <sys/ioctl.h>#include <fcntl.h>#include <sys/sysctl.h>#include <sys/param.h>#include <sys/timeb.h>#include <sys/file.h>#include <sys/ioctl.h>#include <ctype.h>#include <sys/types.h>#include <sys/time.h>#include <net/bpf.h>#include <sys/socket.h>#include <net/if_dl.h>#include <net/route.h>#include <net/if_types.h>#include <arpa/inet.h>#include <netinet/if_ether.h>#ifdef HAVE_NET_ETHERNET_H   #include <net/ethernet.h>#endifint IpForward_status;      // old ipforward statusint bpf_in_use;int size;int SocketBuffer = -1;static char ARP_BROADCAST[6] = {0x0,0x0,0x0,0x0,0x0,0x0};// protos...int Inet_FindIFace(char *iface);int Inet_CorrectIface(char *iface);int Inet_GetIfaceInfo(char *iface, int *MTU, char *MyMAC, u_long *IP, u_long *NetMask);int Inet_SetPromisc(char *iface);int Inet_OpenRawSock(char *iface);void Inet_CloseRawSock(int sock);int Inet_GetRawPacket(int sock, char *buffer, int MTU, short *type);int Inet_SendRawPacket(int sock, char *buffer, int len);void Inet_SetNonBlock(int sock);void Inet_Restore_ifr(void);void Inet_DisableForwarding(void);void Inet_RestoreForwarding(void);char *Inet_MacFromIP(unsigned long ip);// ----------------------------------------------int Inet_FindIFace(char *iface)     // adapded from pcap_lookupdev{   int fd, minunit, n;   char *cp;   struct ifreq *ifrp, *ifend, *ifnext, *mp;   struct ifconf ifc;   char *buf;   struct ifreq ifr;   unsigned int buf_size;   fd = socket(AF_INET, SOCK_DGRAM, 0);   if (fd < 0)      ERROR_MSG("socket()");   buf_size = 8192;   for (;;) {      buf = malloc (buf_size);      if (buf == NULL)         ERROR_MSG("malloc()");      ifc.ifc_len = buf_size;      ifc.ifc_buf = buf;      memset (buf, 0, buf_size);      if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 && errno != EINVAL)         ERROR_MSG("ioctl(SIOCGIFCONF)");      if (ifc.ifc_len < buf_size)         break;      free (buf);      buf_size *= 2;   }   ifrp = (struct ifreq *)buf;   ifend = (struct ifreq *)(buf + ifc.ifc_len);   mp = NULL;   minunit = 666;   for (; ifrp < ifend; ifrp = ifnext)   {      const char *endcp;#ifdef HAVE_SOCKADDR_SA_LEN   n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);   if (n < sizeof(*ifrp))      ifnext = ifrp + 1;   else      ifnext = (struct ifreq *)((char *)ifrp + n);   if (ifrp->ifr_addr.sa_family != AF_INET)      continue;#else   ifnext = ifrp + 1;#endif      strncpy(ifr.ifr_name, ifrp->ifr_name, sizeof(ifr.ifr_name));      if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0)      {         if (errno == ENXIO)            continue;         ERROR_MSG("ioctl(SIOCGIFFLAGS)");      }      DEBUG_MSG("Inet_FindIFace -- check for [%s]", ifr.ifr_name);      /* Must be up and not the loopback */      if ((ifr.ifr_flags & IFF_UP) == 0 || (ifr.ifr_flags & IFF_LOOPBACK) != 0)         continue;      endcp = ifrp->ifr_name + strlen(ifrp->ifr_name);      for (cp = ifrp->ifr_name; cp < endcp && !isdigit(*cp); ++cp)         continue;      if (isdigit (*cp)) {         n = atoi(cp);      } else {         n = 0;      }      if (n < minunit) {         minunit = n;         mp = ifrp;      }   }   close(fd);   if (mp == NULL)   // no device found   {      free(buf);      return -1;   }   strlcpy(iface, mp->ifr_name, sizeof(Options.netiface));   free(buf);   DEBUG_MSG("Inet_FindIFace -- %s found !!", iface);   return 0;}int Inet_CorrectIface(char *iface){   int sock;   struct ifreq ifr;   DEBUG_MSG("Inet_CorrectIface\t\tIface: %s", iface);   sock = socket(AF_INET, SOCK_DGRAM, 0);   if (sock < 0)      ERROR_MSG("socket()");   memset(&ifr, 0, sizeof(ifr));   strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));   if ( ioctl(sock, SIOCGIFFLAGS, &ifr) < 0)             // check for iface   {      close(sock);      return -1;   }   if (!(ifr.ifr_flags & IFF_UP ))                       // check for flag UP   {      close(sock);      return -1;   }   if (ifr.ifr_flags & IFF_LOOPBACK )                    // check for loopback   {      Options.normal = 1;      Error_msg("Ettercap can't be run on loopback device");   }   if ( ioctl(sock, SIOCGIFADDR, &ifr) < 0 )             // check for alias   {      close(sock);      return -1;   }   close(sock);   return 0;}int Inet_GetIfaceInfo(char *iface, int *MTU, char *MyMAC, unsigned long *IP, unsigned long *NetMask){   int sock;   struct ifreq ifr;   sock = socket(PF_INET, SOCK_DGRAM, 0);   memset(&ifr, 0, sizeof(ifr));   strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));   if (MTU != NULL)   {      if ( ioctl(sock, SIOCGIFMTU, &ifr) < 0)      {         DEBUG_MSG("Inet_GetIfaceInfo -- MTU FAILED... assuming 1500");         *MTU = 1500;      }      else         *MTU = ifr.ifr_mtu;   }   if (MyMAC != NULL)   {      int mib[6];      size_t len;      char *buf, *next, *end;      struct if_msghdr *ifm;      struct sockaddr_dl *sdl;      mib[0] = CTL_NET;      mib[1] = AF_ROUTE;      mib[2] = 0;      mib[3] = AF_LINK;      mib[4] = NET_RT_IFLIST;      mib[5] = 0;      if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)         ERROR_MSG("sysctl()");      if ((buf = (char *)malloc(len)) == NULL )         ERROR_MSG("malloc()");      if (sysctl(mib, 6, buf, &len, NULL, 0) < 0)         ERROR_MSG("sysctl()");      end = buf + len;      for (next = buf ; next < end ; next += ifm->ifm_msglen)      {         ifm = (struct if_msghdr *)next;         if (ifm->ifm_type == RTM_IFINFO)         {            sdl = (struct sockaddr_dl *)(ifm + 1);            if (strncmp(&sdl->sdl_data[0], iface, sdl->sdl_nlen) == 0)            {                memcpy(MyMAC, LLADDR(sdl), ETHER_ADDR_LEN);                break;            }         }      }      free(buf);   }   if (IP != NULL)   {      if ( ioctl(sock, SIOCGIFADDR, &ifr) < 0 )         ERROR_MSG("ioctl(SIOCGIFADDR)");      memcpy((char *)IP, ifr.ifr_addr.sa_data+2, 4);   }   if (NetMask != NULL)   {      if ( ioctl(sock, SIOCGIFNETMASK, &ifr) < 0 )         ERROR_MSG("ioctl(SIOCGIFNETMASK)");      memcpy((char *)NetMask, ifr.ifr_addr.sa_data+2, 4);      if (strcmp(Options.netmask, ""))       // specified on command line         *NetMask = inet_addr(Options.netmask);   }   close(sock);   return 0;}void Inet_SetNonBlock(int sock){   DEBUG_MSG("Inet_SetNonBlock fd = %d", sock);   fcntl(sock, F_SETFL, O_NONBLOCK);}void Inet_CloseRawSock(int sock){   DEBUG_MSG("Inet_CloseRawSock \t wrappered to NULL");   //close(sock);    // the bpf is close when ettercap exits}int Inet_OpenRawSock(char *iface){   int fd, i = 0, type;   char device[sizeof "/dev/bpf0000"];   struct bpf_version bv;   struct ifreq ifr;   char MyMAC[6];   // this BPF will ignore all outgoing packets   static struct bpf_insn insns[] = {      BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 6),           // load mac address [1][2]      BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x00, 0, 4), // k is left 0x00 and will be set later... insns[1].k = htons(*(short *)MyMAC);      BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 8),           // load mac address [3][4]      BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x00, 0, 2), // if equal check the third part, else jump 2      BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 10),          // load mac address [5][6]      BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x00, 1, 0), // if equal this is outgoing ! drop it !      BPF_STMT(BPF_RET|BPF_K, (u_int)-1),          // no filter, passing the wole packet      BPF_STMT(BPF_RET|BPF_K, 0),                  // ignore the packet   };   static struct bpf_program filter = {      sizeof insns / sizeof(insns[0]),      insns   };

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -