mirror of
https://github.com/nmap/nmap.git
synced 2026-01-15 19:09:01 +00:00
no more winip
This commit is contained in:
@@ -1,405 +0,0 @@
|
||||
/************************************************************************/
|
||||
/* Copyright (C) Stas Khirman 1998. All rights reserved. */
|
||||
/* Written by Stas Khirman (staskh@rocketmail.com). */
|
||||
/* and */
|
||||
/* Raz Galili (razgalili@hotmail.com) */
|
||||
/* */
|
||||
/* Free software: no warranty; use anywhere is ok; spread the */
|
||||
/* sources; note any modifications; share variations and */
|
||||
/* derivatives (including sending to staskh@rocketmail.com). */
|
||||
/* */
|
||||
/************************************************************************/
|
||||
|
||||
// Modified by Andy Lutomirski (AMLuto@hotmail.com) on Sept 8, 2001
|
||||
// Changes: added the MIBACCESS_SIMPLE flag to turn it
|
||||
// into a simple wrapper
|
||||
|
||||
// Also added the MIBTraverser class
|
||||
|
||||
|
||||
// This file is _not_ LGPL -- see above license
|
||||
|
||||
|
||||
#include "..\tcpip.h"
|
||||
#include "winip.h"
|
||||
#include <winsock2.h>
|
||||
#include "MibAccess.h"
|
||||
|
||||
MibII *MIBTraverser::m = 0;
|
||||
|
||||
|
||||
/*
|
||||
o http://members.tripod.com/~staskh to find example code for reading your Windows PC's MIB.
|
||||
o http://www.cyberport.com/~tangent/programming/winsock/examples/getmac-snmp.html for example code.
|
||||
o Read the following keys for more information:
|
||||
|
||||
1.3.6.1.2.1.2.1 - # of NIC Entries
|
||||
1.3.6.1.2.1.4.20.1.1 - IP Address (one per address)
|
||||
1.3.6.1.2.1.4.20.1.2 - Interface Index (one per interface, cross references to Interface Entry Number)
|
||||
1.3.6.1.2.1.4.20.1.3 - Subnet Mask (one per address)
|
||||
1.3.6.1.2.1.2.2.1.1 - Interface Entry Number (one per interface)
|
||||
1.3.6.1.2.1.2.2.1.2 - Description (one per interface)
|
||||
1.3.6.1.2.1.2.2.1.3 - Type (one per interface, 6 = Ethernet or DUN, 24 = Loopback)
|
||||
1.3.6.1.2.1.2.2.1.6 - MAC Address (one per interface, also use to rule out DUNs)
|
||||
1.3.6.1.2.1.4.21.1 - IP routing table (I assume that this holds the gateway information, but have not needed it and therefore
|
||||
have not looked at it with more detail).
|
||||
|
||||
For a complete description of the MIB, see RFCs 1155 (SMI), 1156 (MIB), 1157 (SNMP), and 1213 (MIB-II). After reviewing RFC 1155 (SMI)
|
||||
and RFC 1156 (MIB) briefly, I use RFC 1213 (MIB-II) almost exclusively.
|
||||
*/
|
||||
|
||||
|
||||
MibExtLoad::MibExtLoad(LPSTR MibDllName)
|
||||
{
|
||||
m_Init = NULL;
|
||||
m_InitEx = NULL;
|
||||
m_Query = NULL;
|
||||
m_Trap = NULL;
|
||||
|
||||
m_hInst = LoadLibrary(MibDllName);
|
||||
if (m_hInst < (HINSTANCE) HINSTANCE_ERROR)
|
||||
{
|
||||
m_hInst = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
m_Init = (pSnmpExtensionInit) GetProcAddress(m_hInst ,"SnmpExtensionInit");
|
||||
m_InitEx = (pSnmpExtensionInitEx) GetProcAddress(m_hInst ,"SnmpExtensionInitEx");
|
||||
m_Query = (pSnmpExtensionQuery) GetProcAddress(m_hInst ,"SnmpExtensionQuery");
|
||||
m_Trap = (pSnmpExtensionTrap) GetProcAddress(m_hInst ,"SnmpExtensionTrap");
|
||||
}
|
||||
|
||||
|
||||
MibExtLoad::~MibExtLoad()
|
||||
{
|
||||
if (m_hInst)
|
||||
FreeLibrary(m_hInst);
|
||||
|
||||
m_hInst = NULL;
|
||||
}
|
||||
|
||||
|
||||
BOOL MibExtLoad::Init(DWORD dwTimeZeroReference, HANDLE *hPollForTrapEvent,
|
||||
AsnObjectIdentifier *supportedView)
|
||||
{
|
||||
if (m_hInst && m_Init)
|
||||
return m_Init(dwTimeZeroReference, hPollForTrapEvent, supportedView);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
BOOL MibExtLoad::InitEx(AsnObjectIdentifier *supportedView)
|
||||
{
|
||||
if (m_hInst && m_InitEx)
|
||||
return m_InitEx(supportedView);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
BOOL MibExtLoad::Query(BYTE requestType, OUT RFC1157VarBindList *variableBindings,
|
||||
AsnInteger *errorStatus, AsnInteger *errorIndex)
|
||||
{
|
||||
if (m_hInst && m_Query)
|
||||
return m_Query(requestType, variableBindings, errorStatus, errorIndex);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
BOOL MibExtLoad::Trap(AsnObjectIdentifier *enterprise, AsnInteger *genericTrap,
|
||||
AsnInteger *specificTrap, AsnTimeticks *timeStamp,
|
||||
RFC1157VarBindList *variableBindings)
|
||||
{
|
||||
if (m_hInst && m_Trap)
|
||||
return m_Trap(enterprise, genericTrap, specificTrap, timeStamp, variableBindings);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
BOOL MibExtLoad::GetDLLStatus()
|
||||
{
|
||||
if (m_hInst == NULL)
|
||||
return FALSE;
|
||||
else
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
MibII::MibII() : MibExtLoad("inetmib1.dll")
|
||||
#ifndef MIBACCESS_SIMPLE
|
||||
,m_pNICInfo(0), m_ifIndex(0), m_ifEntryNum(0)
|
||||
#endif
|
||||
{
|
||||
#ifndef MIBACCESS_SIMPLE
|
||||
WSADATA wsa;
|
||||
|
||||
m_rvWSA = WSAStartup(MAKEWORD(1, 1), &wsa);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
MibII::~MibII()
|
||||
{
|
||||
#ifndef MIBACCESS_SIMPLE
|
||||
WSACleanup();
|
||||
|
||||
if (m_ifCount > 0)
|
||||
{
|
||||
delete m_pNICInfo;
|
||||
delete m_ifIndex;
|
||||
delete m_ifEntryNum;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
int MibII::Init()
|
||||
{
|
||||
// If there was an error when accessing INETMIB1.DLL ...
|
||||
if (!GetDLLStatus())
|
||||
return ERROR_MIB_DLL;
|
||||
|
||||
// If there was an error when starting Winsock ...
|
||||
#ifndef MIBACCESS_SIMPLE
|
||||
if (m_rvWSA)
|
||||
return ERROR_MIB_WINSOCK;
|
||||
#endif
|
||||
|
||||
HANDLE PollForTrapEvent;
|
||||
AsnObjectIdentifier SupportedView;
|
||||
|
||||
if (!MibExtLoad::Init(GetTickCount(), &PollForTrapEvent, &SupportedView))
|
||||
return ERROR_MIB_INIT;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifndef MIBACCESS_SIMPLE
|
||||
UINT MibII::GetNICCount(BOOL bDialup, BOOL bLoopback)
|
||||
{
|
||||
#define NUM_VARBIND_LIST 7
|
||||
|
||||
// SNMP interface for # of NIC Entries.
|
||||
UINT OID_ifNumEntries[] = {1, 3, 6, 1, 2, 1, 2, 1};
|
||||
AsnObjectIdentifier MIB_ifNumEntries = {sizeof(OID_ifNumEntries) / sizeof(UINT), OID_ifNumEntries};
|
||||
|
||||
// SNMP interface for Entry Type.
|
||||
UINT OID_ifEntryType[] = {1, 3, 6, 1, 2, 1, 2, 2, 1, 3};
|
||||
AsnObjectIdentifier MIB_ifEntryType = {sizeof(OID_ifEntryType) / sizeof(UINT), OID_ifEntryType};
|
||||
|
||||
// SNMP interface for MAC Address.
|
||||
UINT OID_ifMAC[] = {1, 3, 6, 1, 2, 1, 2, 2, 1, 6};
|
||||
AsnObjectIdentifier MIB_ifMAC = {sizeof(OID_ifMAC) / sizeof(UINT), OID_ifMAC};
|
||||
|
||||
// SNMP interface for IP Address.
|
||||
UINT OID_ifIPAddr[] = {1, 3, 6, 1, 2, 1, 4, 20, 1, 1};
|
||||
AsnObjectIdentifier MIB_ifIPAddr = {sizeof(OID_ifIPAddr) / sizeof(UINT), OID_ifIPAddr};
|
||||
|
||||
// SNMP interface for Subnet Mask.
|
||||
UINT OID_ifSubnetMask[] = {1, 3, 6, 1, 2, 1, 4, 20, 1, 3};
|
||||
AsnObjectIdentifier MIB_ifSubnetMask = {sizeof(OID_ifSubnetMask) / sizeof(UINT), OID_ifSubnetMask};
|
||||
|
||||
// SNMP interface for Description.
|
||||
UINT OID_ifDesc[] = {1, 3, 6, 1, 2, 1, 2, 2, 1, 2};
|
||||
AsnObjectIdentifier MIB_ifDesc = {sizeof(OID_ifDesc) / sizeof(UINT), OID_ifDesc};
|
||||
|
||||
// SNMP interface for Interface Index
|
||||
UINT OID_ifIndex[] = {1, 3, 6, 1, 2, 1, 4, 20, 1, 2};
|
||||
AsnObjectIdentifier MIB_ifIndex = {sizeof(OID_ifIndex) / sizeof(UINT), OID_ifIndex};
|
||||
|
||||
// SNMP interface for IP Routing Table
|
||||
UINT OID_ifIPRouteTable[] = {1, 3, 6, 1, 2, 1, 4, 21, 1};
|
||||
AsnObjectIdentifier MIB_ifIPRouteTable = {sizeof(OID_ifIPRouteTable) / sizeof(UINT), OID_ifIPRouteTable};
|
||||
|
||||
// SNMP interface for Interface Entry Number
|
||||
UINT OID_ifEntryNum[] = {1, 3, 6, 1, 2, 1, 2, 2, 1, 1};
|
||||
AsnObjectIdentifier MIB_ifEntryNum = {sizeof(OID_ifEntryNum) / sizeof(UINT), OID_ifEntryNum};
|
||||
|
||||
RFC1157VarBindList varBindList;
|
||||
RFC1157VarBind varBind[NUM_VARBIND_LIST];
|
||||
AsnInteger errorStatus;
|
||||
AsnInteger errorIndex;
|
||||
AsnObjectIdentifier MIB_NULL = {0, 0};
|
||||
int ret;
|
||||
UINT NICCount = 0, ifIndex = 0, i;
|
||||
|
||||
// Initialize the variable list to be retrieved by Query
|
||||
varBindList.list = varBind;
|
||||
// varBind[0].name = MIB_NULL;
|
||||
|
||||
// If the user wants to get the # of NICs in the system, then use only Num Entries.
|
||||
// Copy in the OID to find the # of entries in the Interface table
|
||||
varBindList.len = 1;
|
||||
SNMP_oidcpy(&varBind[0].name, &MIB_ifNumEntries);
|
||||
ret = Query(ASN_RFC1157_GETNEXTREQUEST, &varBindList, &errorStatus, &errorIndex);
|
||||
m_ifCount = varBind[0].value.asnValue.number;
|
||||
if (m_ifCount > 0)
|
||||
{
|
||||
m_pNICInfo = new tSTRUCTNICINFO [m_ifCount];
|
||||
m_ifIndex = new DWORD [m_ifCount];
|
||||
m_ifEntryNum = new DWORD [m_ifCount];
|
||||
m_bDialup = bDialup;
|
||||
m_bLoopback = bLoopback;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
|
||||
// Copy in the OID for the type of interface
|
||||
SNMP_oidcpy(&varBind[0].name, &MIB_ifEntryType);
|
||||
|
||||
// Copy in the OID for MAC Address
|
||||
SNMP_oidcpy(&varBind[1].name, &MIB_ifMAC);
|
||||
|
||||
// If the user wants to get the # of NICs in the system, then use only Entry Type and MAC Address,
|
||||
// otherwise also retrieve IP Address, Subnet Mask, Description, Interface Index, and Interface Entry Number.
|
||||
varBindList.len = NUM_VARBIND_LIST;
|
||||
|
||||
// Copy in the OID for IP Address
|
||||
SNMP_oidcpy(&varBind[2].name, &MIB_ifIPAddr);
|
||||
|
||||
// Copy in the OID for Subnet Mask
|
||||
SNMP_oidcpy(&varBind[3].name, &MIB_ifSubnetMask);
|
||||
|
||||
// Copy in the OID for Description
|
||||
SNMP_oidcpy(&varBind[4].name, &MIB_ifDesc);
|
||||
|
||||
// Copy in the OID for Interface Index
|
||||
SNMP_oidcpy(&varBind[5].name, &MIB_ifIndex);
|
||||
|
||||
// Copy in the OID for Interface Entry Number
|
||||
SNMP_oidcpy(&varBind[6].name, &MIB_ifEntryNum);
|
||||
|
||||
memset(m_pNICInfo, 0, sizeof(tSTRUCTNICINFO) * m_ifCount);
|
||||
|
||||
do
|
||||
{
|
||||
// Submit the query. Responses will be loaded into varBindList. We can expect this call to
|
||||
// succeed a # of times corresponding to the # of adapters reported to be in the system.
|
||||
ret = Query(ASN_RFC1157_GETNEXTREQUEST, &varBindList, &errorStatus, &errorIndex);
|
||||
if (!ret)
|
||||
ret = 1;
|
||||
else
|
||||
// Confirm that the proper type has been returned
|
||||
ret = SNMP_oidncmp(&varBind[0].name, &MIB_ifEntryType, MIB_ifEntryType.idLength);
|
||||
|
||||
if (!ret)
|
||||
{
|
||||
// Confirm that we have an address here
|
||||
ret = SNMP_oidncmp(&varBind[1].name, &MIB_ifMAC, MIB_ifMAC.idLength);
|
||||
if (!ret)
|
||||
{
|
||||
NICCount++;
|
||||
|
||||
// Ignore Loopback devices
|
||||
if ((varBind[1].value.asnValue.address.length == 0 && !m_bLoopback) ||
|
||||
// Ignore Dial-Up Networking adapters
|
||||
(varBind[1].value.asnValue.address.length > 0 &&
|
||||
varBind[1].value.asnValue.address.stream[0] == 0x44 &&
|
||||
varBind[1].value.asnValue.address.stream[1] == 0x45 &&
|
||||
varBind[1].value.asnValue.address.stream[2] == 0x53 &&
|
||||
varBind[1].value.asnValue.address.stream[3] == 0x54 && !m_bDialup) ||
|
||||
// Ignore NULL addresses returned by other network interfaces
|
||||
(varBind[1].value.asnValue.address.length > 0 &&
|
||||
varBind[1].value.asnValue.address.stream[0] == 0x00 &&
|
||||
varBind[1].value.asnValue.address.stream[1] == 0x00 &&
|
||||
varBind[1].value.asnValue.address.stream[2] == 0x00 &&
|
||||
varBind[1].value.asnValue.address.stream[3] == 0x00 &&
|
||||
varBind[1].value.asnValue.address.stream[4] == 0x00 &&
|
||||
varBind[1].value.asnValue.address.stream[5] == 0x00))
|
||||
NICCount--;
|
||||
|
||||
// Store Interface Index and Entry Numbers so we can match up the data later.
|
||||
m_ifIndex[ifIndex] = varBind[5].value.asnValue.number;
|
||||
m_ifEntryNum[ifIndex] = varBind[6].value.asnValue.number;
|
||||
|
||||
// Store data and increment counter.
|
||||
m_pNICInfo[ifIndex].type = varBind[0].value.asnValue.number;
|
||||
m_pNICInfo[ifIndex].MACLength = varBind[1].value.asnValue.address.length;
|
||||
for (i = 0; i < varBind[1].value.asnValue.address.length; i++)
|
||||
m_pNICInfo[ifIndex].MAC[i] = varBind[1].value.asnValue.address.stream[i];
|
||||
|
||||
if (!SNMP_oidncmp(&varBind[2].name, &MIB_ifIPAddr, MIB_ifIPAddr.idLength))
|
||||
{
|
||||
for (i = 0; i < 4; i++)
|
||||
m_pNICInfo[ifIndex].IP[i] = varBind[2].value.asnValue.address.stream[i];
|
||||
}
|
||||
|
||||
if (!SNMP_oidncmp(&varBind[3].name, &MIB_ifSubnetMask, MIB_ifSubnetMask.idLength))
|
||||
{
|
||||
for (i = 0; i < 4; i++)
|
||||
m_pNICInfo[ifIndex].SubnetMask[i] = varBind[3].value.asnValue.address.stream[i];
|
||||
}
|
||||
|
||||
// Leave the last byte as a NULL terminator
|
||||
i = sizeof(m_pNICInfo[ifIndex].Description) - 1;
|
||||
if (varBind[4].value.asnValue.address.length < i)
|
||||
i = varBind[4].value.asnValue.address.length;
|
||||
memcpy(m_pNICInfo[ifIndex].Description, varBind[4].value.asnValue.address.stream, i);
|
||||
ifIndex++;
|
||||
}
|
||||
}
|
||||
}
|
||||
while (!ret);
|
||||
// Stop only on an error. An error will occur when the list of interfaces is exhausted.
|
||||
|
||||
// Free the bindings
|
||||
for (i = 0; i < varBindList.len; i++)
|
||||
SNMP_FreeVarBind(&varBind[i]);
|
||||
|
||||
return NICCount;
|
||||
}
|
||||
|
||||
/*
|
||||
Because IP Address, Interface Index, and Subnet Mask are in the same OID (4.20.1.x), and Interface Entry Number, Description,
|
||||
Type, and MAC Address are in another OID (2.2.1.x), you have to cross reference the Interface Index with the Interface Entry
|
||||
Number. All IP Address and Subnet Mask values belong together, but you have to find the matching Interface Entry Number to get
|
||||
the corresponding Description, Type, and MAC Address.
|
||||
*/
|
||||
void MibII::GetNICInfo(tSTRUCTNICINFO *pNICInfo)
|
||||
{
|
||||
tSTRUCTNICINFO tempStruct;
|
||||
UINT i, j, k, validNICIndex = 0;
|
||||
|
||||
for (i = 0; i < m_ifCount; i++)
|
||||
{
|
||||
memcpy(tempStruct.IP, m_pNICInfo[i].IP, sizeof(tempStruct.IP));
|
||||
memcpy(tempStruct.SubnetMask, m_pNICInfo[i].SubnetMask, sizeof(tempStruct.SubnetMask));
|
||||
// Find the Interface Entry Number that matches the Interface Index.
|
||||
for (j = 0; j < m_ifCount; j++)
|
||||
{
|
||||
if (m_ifIndex[i] == m_ifEntryNum[j])
|
||||
break;
|
||||
}
|
||||
tempStruct.type = m_pNICInfo[j].type;
|
||||
memcpy(tempStruct.Description, m_pNICInfo[j].Description, sizeof(tempStruct.Description));
|
||||
tempStruct.MACLength = m_pNICInfo[j].MACLength;
|
||||
memcpy(tempStruct.MAC, m_pNICInfo[j].MAC, tempStruct.MACLength);
|
||||
|
||||
// Ignore Loopback devices
|
||||
if ((tempStruct.MACLength == 0 && !m_bLoopback) ||
|
||||
// Ignore Dial-Up Networking adapters
|
||||
(tempStruct.MAC[0] == 0x44 &&
|
||||
tempStruct.MAC[1] == 0x45 &&
|
||||
tempStruct.MAC[2] == 0x53 &&
|
||||
tempStruct.MAC[3] == 0x54 && !m_bDialup) ||
|
||||
// Ignore NULL addresses returned by other network interfaces
|
||||
(tempStruct.MAC[0] == 0x00 &&
|
||||
tempStruct.MAC[1] == 0x00 &&
|
||||
tempStruct.MAC[2] == 0x00 &&
|
||||
tempStruct.MAC[3] == 0x00 &&
|
||||
tempStruct.MAC[4] == 0x00 &&
|
||||
tempStruct.MAC[5] == 0x00))
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(&pNICInfo[validNICIndex], &tempStruct, sizeof(tSTRUCTNICINFO));
|
||||
validNICIndex++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif // MIBACCESS_SIMPLE
|
||||
@@ -1,206 +0,0 @@
|
||||
/************************************************************************/
|
||||
/* Copyright (C) Stas Khirman 1998. All rights reserved. */
|
||||
/* Written by Stas Khirman (staskh@rocketmail.com). */
|
||||
/* and */
|
||||
/* Raz Galili (razgalili@hotmail.com) */
|
||||
/* */
|
||||
/* Free software: no warranty; use anywhere is ok; spread the */
|
||||
/* sources; note any modifications; share variations and */
|
||||
/* derivatives (including sending to staskh@rocketmail.com). */
|
||||
/* */
|
||||
/************************************************************************/
|
||||
|
||||
// Modified by Andy Lutomirski (AMLuto@hotmail.com) on Sept 8, 2001
|
||||
// Changes: added the MIBACCESS_SIMPLE flag to turn it
|
||||
// into a simple wrapper
|
||||
|
||||
// Modified the include statements in the cpp files
|
||||
|
||||
// Also added the MIBTraverser class
|
||||
|
||||
|
||||
// This file is _not_ LGPL -- see above license
|
||||
|
||||
//////////////////////////////////////////////////////
|
||||
// FILE : MibAccess.h
|
||||
//
|
||||
//
|
||||
|
||||
#ifndef _SNMP_ACCESS_H_
|
||||
#define _SNMP_ACCESS_H_
|
||||
|
||||
#include <snmp.h>
|
||||
|
||||
#define MIBACCESS_SIMPLE
|
||||
|
||||
//////////////////////////////////////////////////////////////
|
||||
// Definition of pointers to the four functions in the Mib Dll
|
||||
//
|
||||
typedef BOOL (WINAPI *pSnmpExtensionInit)(IN DWORD dwTimeZeroReference,
|
||||
OUT HANDLE *hPollForTrapEvent,
|
||||
OUT AsnObjectIdentifier *supportedView);
|
||||
|
||||
typedef BOOL (WINAPI *pSnmpExtensionTrap)(OUT AsnObjectIdentifier *enterprise,
|
||||
OUT AsnInteger *genericTrap,
|
||||
OUT AsnInteger *specificTrap,
|
||||
OUT AsnTimeticks *timeStamp,
|
||||
OUT RFC1157VarBindList *variableBindings);
|
||||
|
||||
typedef BOOL (WINAPI *pSnmpExtensionQuery)(IN BYTE requestType,
|
||||
IN OUT RFC1157VarBindList *variableBindings,
|
||||
OUT AsnInteger *errorStatus,
|
||||
OUT AsnInteger *errorIndex);
|
||||
|
||||
typedef BOOL (WINAPI *pSnmpExtensionInitEx)(OUT AsnObjectIdentifier *supportedView);
|
||||
|
||||
|
||||
#ifndef MIBACCESS_SIMPLE
|
||||
typedef struct
|
||||
{
|
||||
long type;
|
||||
BYTE MACLength;
|
||||
BYTE MAC[14];
|
||||
BYTE IP[4];
|
||||
BYTE SubnetMask[4];
|
||||
BYTE Description[64];
|
||||
} tSTRUCTNICINFO;
|
||||
#endif
|
||||
|
||||
|
||||
#define ERROR_MIB_DLL -1
|
||||
#define ERROR_MIB_WINSOCK -2
|
||||
#define ERROR_MIB_INIT -3
|
||||
|
||||
|
||||
class MibExtLoad
|
||||
{
|
||||
public:
|
||||
MibExtLoad(LPSTR MibDllName);
|
||||
~MibExtLoad();
|
||||
|
||||
BOOL Init(DWORD dwTimeZeroReference, HANDLE *hPollForTrapEvent, AsnObjectIdentifier *supportedView);
|
||||
BOOL InitEx(AsnObjectIdentifier *supportedView);
|
||||
BOOL Query(BYTE requestType, OUT RFC1157VarBindList *variableBindings,
|
||||
AsnInteger *errorStatus, AsnInteger *errorIndex);
|
||||
BOOL Trap(AsnObjectIdentifier *enterprise, AsnInteger *genericTrap,
|
||||
AsnInteger *specificTrap, AsnTimeticks *timeStamp, RFC1157VarBindList *variableBindings);
|
||||
|
||||
BOOL GetDLLStatus();
|
||||
|
||||
private:
|
||||
HINSTANCE m_hInst;
|
||||
pSnmpExtensionInit m_Init;
|
||||
pSnmpExtensionInitEx m_InitEx;
|
||||
pSnmpExtensionQuery m_Query;
|
||||
pSnmpExtensionTrap m_Trap;
|
||||
};
|
||||
|
||||
|
||||
class MibII: public MibExtLoad
|
||||
{
|
||||
public:
|
||||
MibII();
|
||||
~MibII();
|
||||
|
||||
int Init();
|
||||
|
||||
#ifndef MIBACCESS_SIMPLE
|
||||
UINT GetNICCount(BOOL bDialup, BOOL bLoopback);
|
||||
void GetNICInfo(tSTRUCTNICINFO *pNICInfo);
|
||||
#endif
|
||||
|
||||
private:
|
||||
|
||||
#ifndef MIBACCESS_SIMPLE
|
||||
int m_rvWSA;
|
||||
UINT m_ifCount;
|
||||
DWORD *m_ifIndex;
|
||||
DWORD *m_ifEntryNum;
|
||||
tSTRUCTNICINFO *m_pNICInfo;
|
||||
BOOL m_bDialup;
|
||||
BOOL m_bLoopback;
|
||||
|
||||
void MatchNICEntries(UINT NICCount, tSTRUCTNICINFO *pNICInfo);
|
||||
#endif
|
||||
};
|
||||
|
||||
// This is cheap, but it works courtesy of the big-endianness of IP's
|
||||
#define ASN_IP(x) ( * reinterpret_cast<DWORD*>(x.string.stream) )
|
||||
|
||||
class MIBTraverser
|
||||
{
|
||||
private:
|
||||
AsnObjectIdentifier *desc;
|
||||
UINT len; // number of elements in desc
|
||||
|
||||
SnmpVarBindList vbl;
|
||||
SnmpVarBind *vb;
|
||||
|
||||
public:
|
||||
MIBTraverser() : desc(0), len(0), vb(0) {}
|
||||
~MIBTraverser() { clean(); }
|
||||
|
||||
static MibII *m; // set this before using
|
||||
|
||||
void Init(AsnObjectIdentifier *list, UINT sz)
|
||||
{
|
||||
clean();
|
||||
|
||||
desc = list;
|
||||
len = sz;
|
||||
vb = new SnmpVarBind[len];
|
||||
vbl.list = vb;
|
||||
vbl.len = len;
|
||||
|
||||
ZeroMemory(vb, len * sizeof(vb));
|
||||
int i;
|
||||
for(i = 0; i < len; i++)
|
||||
SNMP_oidcpy(&vb[i].name, &desc[i]);
|
||||
}
|
||||
|
||||
void clean()
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < len; i++)
|
||||
SnmpUtilVarBindFree(vb + i);
|
||||
|
||||
delete[] vb;
|
||||
vb = 0;
|
||||
}
|
||||
|
||||
inline UINT length() {return len;}
|
||||
inline SnmpVarBind &operator [] (UINT index) {return vb[index];}
|
||||
|
||||
bool Next(AsnInteger32 *stat = 0, AsnInteger32 *errindex = 0)
|
||||
{
|
||||
AsnInteger32 mystat, myind;
|
||||
if(!stat) stat = &mystat;
|
||||
if(!errindex) errindex = &myind;
|
||||
|
||||
if(!m->Query(ASN_RFC1157_GETNEXTREQUEST, &vbl, stat, errindex))
|
||||
return false;
|
||||
|
||||
if(*stat != SNMP_ERRORSTATUS_NOERROR) return false;
|
||||
|
||||
if(SnmpUtilOidNCmp(&vb[0].name, &desc[0], desc[0].idLength))
|
||||
return false; // passed end
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Get(AsnInteger32 *stat = 0, AsnInteger32 *errindex = 0)
|
||||
{
|
||||
AsnInteger32 mystat, myind;
|
||||
if(!stat) stat = &mystat;
|
||||
if(!errindex) errindex = &myind;
|
||||
|
||||
if(!m->Query(ASN_RFC1157_GETREQUEST, &vbl, stat, errindex))
|
||||
return false;
|
||||
|
||||
if(*stat != SNMP_ERRORSTATUS_NOERROR) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,33 +0,0 @@
|
||||
@echo off
|
||||
|
||||
rem iphlpapi.def: fools lib into correctly generating iphlpapi.lib
|
||||
rem Copyright (C) 2000 Andy Lutomirski
|
||||
|
||||
rem This library is free softwarerem you can redistribute it and/or
|
||||
rem modify it under the terms of the GNU Lesser General Public
|
||||
rem License, version 2.1, as published by the Free Software
|
||||
rem Foundation, with the exception that if this copy of the library
|
||||
rem is distributed under the Lesser GNU Public License (as opposed
|
||||
rem to the ordinary GPL), you may ignore section 6b, and that all
|
||||
rem copies distributed without exercising section 3 must retain this
|
||||
rem paragraph in its entirety.
|
||||
|
||||
rem This library is distributed in the hope that it will be useful,
|
||||
rem but WITHOUT ANY WARRANTYrem without even the implied warranty of
|
||||
rem MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
rem Lesser General Public License for more details.
|
||||
|
||||
rem You should have received a copy of the GNU Lesser General Public
|
||||
rem License along with this libraryrem if not, write to the Free Software
|
||||
rem Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
echo Rebuilding iphlpapi.lib...
|
||||
cl /c /Zl /nologo iphlpapi.c
|
||||
lib /nologo /def:iphlpapi.def iphlpapi.obj
|
||||
del iphlpapi.obj iphlpapi.exp
|
||||
|
||||
rem Clean up after VC
|
||||
if exist debug rd debug
|
||||
if exist release rd release
|
||||
|
||||
echo Done.
|
||||
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
|
||||
iphlpapi.c: fools lib into correctly generating iphlpapi.lib
|
||||
Copyright (C) 2000 Andy Lutomirski
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License, version 2.1, as published by the Free Software
|
||||
Foundation, with the exception that if this copy of the library
|
||||
is distributed under the Lesser GNU Public License (as opposed
|
||||
to the ordinary GPL), you may ignore section 6b, and that all
|
||||
copies distributed without exercising section 3 must retain this
|
||||
paragraph in its entirety.
|
||||
|
||||
This library 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
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
*/
|
||||
|
||||
void __declspec(dllexport) __stdcall GetIpAddrTable(int p1, int p2, int p3) {}
|
||||
void __declspec(dllexport) __stdcall GetIpForwardTable(int p1, int p2, int p3) {}
|
||||
void __declspec(dllexport) __stdcall GetIfTable(int p1, int p2, int p3) {}
|
||||
void __declspec(dllexport) __stdcall GetIpNetTable(int p1, int p2, int p3) {}
|
||||
void __declspec(dllexport) __stdcall SendARP( int p1, int p2, int p3, int p4) {}
|
||||
@@ -1,29 +0,0 @@
|
||||
;iphlpapi.def: fools lib into correctly generating iphlpapi.lib
|
||||
;Copyright (C) 2000 Andy Lutomirski
|
||||
|
||||
;This library is free software; you can redistribute it and/or
|
||||
;modify it under the terms of the GNU Lesser General Public
|
||||
;License, version 2.1, as published by the Free Software
|
||||
;Foundation, with the exception that if this copy of the library
|
||||
;is distributed under the Lesser GNU Public License (as opposed
|
||||
;to the ordinary GPL), you may ignore section 6b, and that all
|
||||
;copies distributed without exercising section 3 must retain this
|
||||
;paragraph in its entirety.
|
||||
|
||||
;This library 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
|
||||
;Lesser General Public License for more details.
|
||||
|
||||
;You should have received a copy of the GNU Lesser General Public
|
||||
;License along with this library; if not, write to the Free Software
|
||||
;Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
LIBRARY iphlpapi.dll
|
||||
|
||||
EXPORTS
|
||||
GetIpAddrTable
|
||||
GetIpForwardTable
|
||||
GetIfTable
|
||||
GetIpNetTable
|
||||
SendARP
|
||||
@@ -1,101 +0,0 @@
|
||||
# Microsoft Developer Studio Project File - Name="iphlpapi" - Package Owner=<4>
|
||||
# Microsoft Developer Studio Generated Build File, Format Version 6.00
|
||||
# ** DO NOT EDIT **
|
||||
|
||||
# TARGTYPE "Win32 (x86) Generic Project" 0x010a
|
||||
|
||||
CFG=iphlpapi - Win32 Debug
|
||||
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
|
||||
!MESSAGE use the Export Makefile command and run
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "iphlpapi.mak".
|
||||
!MESSAGE
|
||||
!MESSAGE You can specify a configuration when running NMAKE
|
||||
!MESSAGE by defining the macro CFG on the command line. For example:
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "iphlpapi.mak" CFG="iphlpapi - Win32 Debug"
|
||||
!MESSAGE
|
||||
!MESSAGE Possible choices for configuration are:
|
||||
!MESSAGE
|
||||
!MESSAGE "iphlpapi - Win32 Release" (based on "Win32 (x86) Generic Project")
|
||||
!MESSAGE "iphlpapi - Win32 Debug" (based on "Win32 (x86) Generic Project")
|
||||
!MESSAGE
|
||||
|
||||
# Begin Project
|
||||
# PROP AllowPerConfigDependencies 0
|
||||
# PROP Scc_ProjName ""
|
||||
# PROP Scc_LocalPath ""
|
||||
MTL=midl.exe
|
||||
|
||||
!IF "$(CFG)" == "iphlpapi - Win32 Release"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 0
|
||||
# PROP BASE Output_Dir "Release"
|
||||
# PROP BASE Intermediate_Dir "Release"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 0
|
||||
# PROP Output_Dir "Release"
|
||||
# PROP Intermediate_Dir "Release"
|
||||
# PROP Target_Dir ""
|
||||
|
||||
!ELSEIF "$(CFG)" == "iphlpapi - Win32 Debug"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 1
|
||||
# PROP BASE Output_Dir "Debug"
|
||||
# PROP BASE Intermediate_Dir "Debug"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 1
|
||||
# PROP Output_Dir "Debug"
|
||||
# PROP Intermediate_Dir "Debug"
|
||||
# PROP Target_Dir ""
|
||||
|
||||
!ENDIF
|
||||
|
||||
# Begin Target
|
||||
|
||||
# Name "iphlpapi - Win32 Release"
|
||||
# Name "iphlpapi - Win32 Debug"
|
||||
# Begin Group "Source Files"
|
||||
|
||||
# PROP Default_Filter ""
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\iphlpapi.c
|
||||
|
||||
!IF "$(CFG)" == "iphlpapi - Win32 Release"
|
||||
|
||||
USERDEP__IPHLP="iphlpapi.def"
|
||||
# Begin Custom Build
|
||||
InputPath=.\iphlpapi.c
|
||||
|
||||
"iphlpapi.lib" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
|
||||
iphlpapi
|
||||
|
||||
# End Custom Build
|
||||
|
||||
!ELSEIF "$(CFG)" == "iphlpapi - Win32 Debug"
|
||||
|
||||
USERDEP__IPHLP="iphlpapi.def"
|
||||
# Begin Custom Build
|
||||
InputPath=.\iphlpapi.c
|
||||
|
||||
"iphlpapi.lib" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
|
||||
iphlpapi
|
||||
|
||||
# End Custom Build
|
||||
|
||||
!ENDIF
|
||||
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\iphlpapi.def
|
||||
# PROP Exclude_From_Build 1
|
||||
# End Source File
|
||||
# End Group
|
||||
# End Target
|
||||
# End Project
|
||||
@@ -1,3 +0,0 @@
|
||||
iphlpapi.lib was created in such a way that is not copyright Microsoft.
|
||||
|
||||
Run iphlpapi.bat to rebuild it.
|
||||
@@ -1,97 +0,0 @@
|
||||
<?xml version="1.0" encoding="Windows-1252"?>
|
||||
<VisualStudioProject
|
||||
ProjectType="Visual C++"
|
||||
Version="7.10"
|
||||
Name="iphlpapi"
|
||||
SccProjectName=""
|
||||
SccLocalPath=""
|
||||
Keyword="MakeFileProj">
|
||||
<Platforms>
|
||||
<Platform
|
||||
Name="Win32"/>
|
||||
</Platforms>
|
||||
<Configurations>
|
||||
<Configuration
|
||||
Name="Release|Win32"
|
||||
OutputDirectory=".\Release"
|
||||
IntermediateDirectory=".\Release"
|
||||
ConfigurationType="10"
|
||||
UseOfMFC="0"
|
||||
ATLMinimizesCRunTimeLibraryUsage="FALSE">
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
TypeLibraryName=".\Release/iphlpapi.tlb"
|
||||
HeaderFileName=""/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"/>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"/>
|
||||
</Configuration>
|
||||
<Configuration
|
||||
Name="Debug|Win32"
|
||||
OutputDirectory=".\Debug"
|
||||
IntermediateDirectory=".\Debug"
|
||||
ConfigurationType="10"
|
||||
UseOfMFC="0"
|
||||
ATLMinimizesCRunTimeLibraryUsage="FALSE">
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
TypeLibraryName=".\Debug/iphlpapi.tlb"
|
||||
HeaderFileName=""/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"/>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"/>
|
||||
</Configuration>
|
||||
</Configurations>
|
||||
<References>
|
||||
</References>
|
||||
<Files>
|
||||
<Filter
|
||||
Name="Source Files"
|
||||
Filter="">
|
||||
<File
|
||||
RelativePath="iphlpapi.c">
|
||||
<FileConfiguration
|
||||
Name="Release|Win32">
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
CommandLine="iphlpapi
|
||||
"
|
||||
AdditionalDependencies="iphlpapi.def;"
|
||||
Outputs="iphlpapi.lib"/>
|
||||
</FileConfiguration>
|
||||
<FileConfiguration
|
||||
Name="Debug|Win32">
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
CommandLine="iphlpapi
|
||||
"
|
||||
AdditionalDependencies="iphlpapi.def;"
|
||||
Outputs="iphlpapi.lib"/>
|
||||
</FileConfiguration>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="iphlpapi.def">
|
||||
<FileConfiguration
|
||||
Name="Release|Win32"
|
||||
ExcludedFromBuild="TRUE">
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"/>
|
||||
</FileConfiguration>
|
||||
<FileConfiguration
|
||||
Name="Debug|Win32"
|
||||
ExcludedFromBuild="TRUE">
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"/>
|
||||
</FileConfiguration>
|
||||
</File>
|
||||
</Filter>
|
||||
</Files>
|
||||
<Globals>
|
||||
</Globals>
|
||||
</VisualStudioProject>
|
||||
@@ -1,20 +0,0 @@
|
||||
This directory contains the winip library.
|
||||
Copyright (C) 2000 Andy Lutomirski
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License, version 2.1, as published by the Free Software
|
||||
Foundation, with the exception that if this copy of the library
|
||||
is distributed under the Lesser GNU Public License (as opposed
|
||||
to the ordinary GPL), you may ignore section 6b, and that all
|
||||
copies distributed without exercising section 3 must retain this
|
||||
paragraph in its entirety.
|
||||
|
||||
This library 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
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
@@ -1,161 +0,0 @@
|
||||
/*
|
||||
|
||||
rawrecv.c: implements a (very small) subset of libpcap over raw sockets
|
||||
Copyright (C) 2000 Andy Lutomirski
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License, version 2.1, as published by the Free Software
|
||||
Foundation, with the exception that if this copy of the library
|
||||
is distributed under the Lesser GNU Public License (as opposed
|
||||
to the ordinary GPL), you may ignore section 6b, and that all
|
||||
copies distributed without exercising section 3 must retain this
|
||||
paragraph in its entirety.
|
||||
|
||||
This library 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
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
*/
|
||||
|
||||
#include "..\tcpip.h"
|
||||
#include "winip.h"
|
||||
#include "..\..\NmapOps.h"
|
||||
#undef socket
|
||||
|
||||
#ifndef SIO_RCVALL
|
||||
#define IOC_VENDOR 0x18000000
|
||||
#define SIO_RCVALL _WSAIOW(IOC_VENDOR, 1)
|
||||
#endif
|
||||
|
||||
extern NmapOps o;
|
||||
|
||||
static int nullfilter(const char *packet, unsigned int len)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SOCKET s = INVALID_SOCKET;
|
||||
static PFILTERFN filter;
|
||||
|
||||
static char buf[4096];
|
||||
|
||||
pcap_t *rawrecv_open(const char *dev)
|
||||
{
|
||||
DWORD one = 1;
|
||||
u_long bufsz = 1<<20;
|
||||
DWORD bytesret;
|
||||
struct sockaddr_in sin;
|
||||
|
||||
if(o.debugging > 1)
|
||||
printf("Trying to open %s for rawsock receive\n", dev);
|
||||
|
||||
ZeroMemory(&sin, sizeof(sin));
|
||||
sin.sin_family = AF_INET;
|
||||
if(0 != devname2ipaddr((char*)dev, &sin.sin_addr))
|
||||
fatal("rawrecv_open: failed to find an IP for device %s\n", dev);
|
||||
|
||||
if(s != INVALID_SOCKET)
|
||||
fatal("rawrecv: I can't handle more than one open connection\n");
|
||||
|
||||
s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
|
||||
if(s == INVALID_SOCKET)
|
||||
fatal("rawrecv: cannot open raw socket\n");
|
||||
|
||||
if(bind(s, (struct sockaddr*)&sin, sizeof(sin)))
|
||||
fatal("rawrecv_open: failed to bind to %s (%d)\n", inet_ntoa(sin.sin_addr), WSAGetLastError());
|
||||
|
||||
if(setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char*)&bufsz, sizeof(bufsz)))
|
||||
fatal("rawrecv_open: failed to set buffer size\n");
|
||||
|
||||
if(WSAIoctl(s, SIO_RCVALL, &one, sizeof(one), NULL, 0,
|
||||
&bytesret, NULL, NULL))
|
||||
fatal("rawrecv_open: SIO_RCVALL failed (%lu) on device %s\n", WSAGetLastError(), dev);
|
||||
|
||||
filter = nullfilter;
|
||||
|
||||
return (pcap_t*)-2;
|
||||
}
|
||||
|
||||
void rawrecv_close(pcap_t *pd)
|
||||
{
|
||||
if(s == INVALID_SOCKET)
|
||||
fatal("rawrecv_close: nothing to do\n");
|
||||
|
||||
closesocket(s);
|
||||
s = INVALID_SOCKET;
|
||||
}
|
||||
|
||||
void rawrecv_setfilter(pcap_t *pd, PFILTERFN filterfn)
|
||||
{
|
||||
if(-2 != (long)pd)
|
||||
fatal("rawrecv_setfilter: got non-rawrecv handle\n");
|
||||
|
||||
if(filterfn) filter = filterfn;
|
||||
else filter = nullfilter;
|
||||
}
|
||||
|
||||
char *rawrecv_readip(pcap_t *pd, unsigned int *len, long to_usec, struct timeval *rcvdtime)
|
||||
{
|
||||
int rcvlen;
|
||||
DWORD time1, time2;
|
||||
fd_set fds;
|
||||
TIMEVAL tv;
|
||||
|
||||
if(-2 != (long)pd)
|
||||
fatal("rawrecv_readip: called with non-rawrecv handle\n");
|
||||
|
||||
begin:
|
||||
|
||||
// Note: I could use SO_RCVTIMEO but I don't trust it...
|
||||
time1 = GetTickCount();
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(s, &fds);
|
||||
tv.tv_usec = to_usec % 1000000;
|
||||
tv.tv_sec = to_usec / 1000000;
|
||||
if(0 == select(0, &fds, 0, 0, &tv))
|
||||
{
|
||||
if(len) *len = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
rcvlen = recv(s, buf, sizeof(buf), 0);
|
||||
time2 = GetTickCount() + 10;
|
||||
|
||||
if(rcvlen > 0)
|
||||
{
|
||||
if(rcvlen >= sizeof(struct ip) && filter(buf, rcvlen))
|
||||
{
|
||||
if (rcvdtime) {
|
||||
gettimeofday(rcvdtime, NULL);
|
||||
}
|
||||
if(len) *len = rcvlen;
|
||||
PacketTrace::trace(PacketTrace::RCVD, (u8 *) buf, rcvlen);
|
||||
return buf;
|
||||
}
|
||||
else
|
||||
{
|
||||
to_usec -= 1000 * (time2 - time1);
|
||||
if(to_usec < 0)
|
||||
{
|
||||
if(len) *len = 0;
|
||||
return 0;
|
||||
}
|
||||
goto begin;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DWORD err = WSAGetLastError();
|
||||
if(err != WSAETIMEDOUT && err != WSAEWOULDBLOCK)
|
||||
fatal("rawrecv: recv failed (%lu)\n", err);
|
||||
|
||||
if(len) *len = 0;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
WinIP -- a set of functions to allow raw IP on Windows
|
||||
|
||||
There is no documentation (yet).
|
||||
|
||||
The winip library consists of all files in this directory. It is
|
||||
a set of functions designed to implement something resembling BSD
|
||||
raw sockets on Windows using either winpcap or Win2K SOCK_RAW.
|
||||
It determines as runtime which one should be used.
|
||||
|
||||
This library was inspired by nmapNT by ryan@eeye.com. It doesn't
|
||||
contain any of his original code any more (I think). His code
|
||||
has been moved to wintcpip.c.
|
||||
|
||||
Note: functions in this library with the same name as the
|
||||
corresponding nmap function are still LGPL since they are
|
||||
not based (except for semantics) on nmap.
|
||||
|
||||
Proposed changes should be discussed on nmap-dev if nmap-related
|
||||
or you could e-mail me and maybe I'll set up a list for general
|
||||
development or use.
|
||||
|
||||
Note: snmpapi.cpp and MibAccess.* are based on sources from
|
||||
codeguru.com. They are for win95 support, and are not needed
|
||||
if snmp95.cpp is modified to do nothing.
|
||||
|
||||
You still need WinSock2 to run on Win95. Get it at: (one line)
|
||||
http://www.microsoft.com/Windows95/downloads/contents
|
||||
/WUAdminTools/S_WUNetworkingTools/W95Sockets2/Default.asp
|
||||
|
||||
Get winpcap from http://netgroup-serv.polito.it/winpcap
|
||||
|
||||
|
||||
My PGP key is:
|
||||
|
||||
-----BEGIN PGP PUBLIC KEY BLOCK-----
|
||||
Version: PGPfreeware 6.5.8 for non-commercial use <http://www.pgp.com>
|
||||
|
||||
mQGiBDnOgrERBADxtQfwz3gj76DTXGPvie4ZkD3OVuQw4CP0etMsiqPtipGVuetu
|
||||
A3+4XLG2CljxN3c3/KRuG1AF5M0T81IB96wGHJYP/LLQ9sp6TguQgFsTXdIRVXGF
|
||||
57+Uw2Bz1twsYWsb3vVcn5K+W7XhyEq5gVzvBbRA4tieUvwXdntYDhEP+wCg/9sR
|
||||
clVmF3kx4DfrJpsWIyv4bJsEALup/as6kW1X7I0wS0fPM1zHBaTg6/bP8mI90asX
|
||||
5xEDgsmHvc6SsQbAk4YAKMggLBtkXNA6AdBLnnh2ef5vOnrHAUbrcejR5YXxihQ9
|
||||
YKTxQ9oEnlL0sdVokEQJ9KGJofl2BmDTzPtUhxdKtGfeNz9AbrXawwxOsfOGPIB0
|
||||
cgkxA/9hdMU80ktpoKBw8o1xgX5DDaD6XjfqvmV2NwJQRXmyC596woMHUaG3WNHI
|
||||
/famgszy0SG9i9oQH0XFYEmqF7MuAfwK61i5Yzb5lKq2XHIiXbpz4pWso9sbZyDU
|
||||
9YQXRQxFMaEiQs5o2Ky61U64Fy6/n7DdeJDx4PFiNafYVE/Q9LQmQW5keSBMdXRv
|
||||
bWlyc2tpIDxMdXRvQG1haWxhbmRuZXdzLmNvbT6JAE4EEBECAA4FAjnOgrEECwMC
|
||||
AQIZAQAKCRAxdZqcg8510+X7AKCXnBYDFqwZ4r2OqgcEzTFtpjK66QCg2tEgIyg8
|
||||
cFgFJhNC6h+k0fjgisK5Aw0EOc6CsRAMAMwdd1ckOErixPDojhNnl06SE2H22+sl
|
||||
Dhf99pj3yHx5sHIdOHX79sFzxIMRJitDYMPj6NYK/aEoJguuqa6zZQ+iAFMBoHzW
|
||||
q6MSHvoPKs4fdIRPyvMX86RA6dfSd7ZCLQI2wSbLaF6dfJgJCo1+Le3kXXn11JJP
|
||||
mxiO/CqnS3wy9kJXtwh/CBdyorrWqULzBej5UxE5T7bxbrlLOCDaAadWoxTpj0BV
|
||||
89AHxstDqZSt90xkhkn4DIO9ZekX1KHTUPj1WV/cdlJPPT2N286Z4VeSWc39uK50
|
||||
T8X8dryDxUcwYc58yWb/Ffm7/ZFexwGq01uejaClcjrUGvC/RgBYK+X0iP1YTknb
|
||||
zSC0neSRBzZrM2w4DUUdD3yIsxx8Wy2O9vPJI8BD8KVbGI2Ou1WMuF040zT9fBdX
|
||||
Q6MdGGzeMyEstSr/POGxKUAYEY18hKcKctaGxAMZyAcpesqVDNmWn6vQClCbAkbT
|
||||
CD1mpF1Bn5x8vYlLIhkmuquiXsNV6UwybwACAgwAsKr5rKpGFEK+3ZR/xnoPgo+Z
|
||||
x/P19nQyBkA9ZYNelG3y+3UMKakQ0HLp08NmBOBvUFBUBbsQdqEn1RYnkEVVb/Zm
|
||||
7I2olottdoPxjSpHXoQqa0W0DYe7iFVKKUbePYyrwMSkqTm5+3WOIhPVj1pnhkhq
|
||||
MwrYUAu0yUIQ463QKuxIh/nxzShMEbx1HGdCmeT3j5ic865fQESRBYw3npxkvKGv
|
||||
K4huVO/ZC8SiXglHd9uac8N/Hv+zhnEV1rTN/sXQsIlPKPEdgfWXLPmu1aKdtWs6
|
||||
68xSdO5zeexvWoj7hcwwT1fb86U8GVRTvJb2+hD4TdNg8Id7pWGOCU9aeEjksNYX
|
||||
Q3dNjNjSUGe+SIhTDVqPcUPR2RqQ3gYZsqVSzQO/YECqaFj2Jr/SD4GHfbQwy3j/
|
||||
BrSTim1aBJi0yeF04Eh/0mbujg0ujBSSlcEn5MBm+dhRKDpiAjxwj95lJGn//W0j
|
||||
vH/52MyAJLZKak50G20FsE9MuF0p14d5B5Ybv7zliQBGBBgRAgAGBQI5zoKxAAoJ
|
||||
EDF1mpyDznXTL1QAn1Ykin2yyKCu82Je54fB97sSMhwiAKD5s4mwOmPqcfwqGe2q
|
||||
yOZTzqpgXw==
|
||||
=TKs6
|
||||
-----END PGP PUBLIC KEY BLOCK-----
|
||||
|
||||
|
||||
|
||||
Version history:
|
||||
0.1: first public release (in nmap)
|
||||
0.2: adds windows 95 support
|
||||
|
||||
|
||||
Known issues:
|
||||
|
||||
If there is a lot of traffic over any given interface unrelated
|
||||
to the client, and if the interface is using Win2K raw sockets,
|
||||
then there may be data loss. I will fix it if this becomes
|
||||
a problem.
|
||||
|
||||
It needs testing to make sure it works somewhat normally if
|
||||
iphlpapi is not present.
|
||||
|
||||
Support for forcing a given source address is somewhat sketchy.
|
||||
Support for IP over an interface that isn't bound to MS's stack
|
||||
is nonexistant, although it could feasably be added
|
||||
|
||||
Need to implement PPP over winpcap on win98 (and FDDI,
|
||||
ATM, and TokenRing on all platforms).
|
||||
|
||||
|
||||
Files contained in this library:
|
||||
winip.c
|
||||
winip.h
|
||||
rawrecv.c
|
||||
pcapsend.c
|
||||
genmod.h
|
||||
iphlpapi.txt
|
||||
iphlpapi.lib
|
||||
iphlpapi.c
|
||||
iphlpapi.def
|
||||
iphlpapi.bat
|
||||
iphlpapi.h
|
||||
snmp95.cpp
|
||||
snmpapi.cpp
|
||||
MibAccess.cpp
|
||||
MibAccess.h
|
||||
@@ -1,834 +0,0 @@
|
||||
/*
|
||||
|
||||
winip.c: non-pcap-or-rawsock-specific code for the winip library
|
||||
Copyright (C) 2000 Andy Lutomirski
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License, version 2.1, as published by the Free Software
|
||||
Foundation, with the exception that if this copy of the library
|
||||
is distributed under the Lesser GNU Public License (as opposed
|
||||
to the ordinary GPL), you may ignore section 6b, and that all
|
||||
copies distributed without exercising section 3 must retain this
|
||||
paragraph in its entirety.
|
||||
|
||||
This library 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
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
This is designed to be used by nmap but should be
|
||||
adaptable to anything.
|
||||
|
||||
This module implements the tables needed for
|
||||
routing and interface selection
|
||||
|
||||
A winif is for iphlpapi
|
||||
An ifindex is an index into iftable
|
||||
|
||||
Note: if used outside nmap in a non-GPL app, you need to reimplement
|
||||
readip_pcap_real and my_real_open_pcap_live for licensing reasons.
|
||||
If used outside nmap in a GPL'ed app, just copy them from wintcpip.c.
|
||||
|
||||
*/
|
||||
|
||||
#include "nmap.h"
|
||||
#include "..\tcpip.h"
|
||||
#include "winip.h"
|
||||
#include "..\..\NmapOps.h"
|
||||
#include "ntddndis.h"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# include <delayimp.h>
|
||||
#endif
|
||||
|
||||
#undef socket
|
||||
#undef sendto
|
||||
#undef pcap_close
|
||||
|
||||
#define IP_HDRINCL 2 /* header is included with data */
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define DLI_ERROR VcppException(ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND)
|
||||
#endif
|
||||
|
||||
extern NmapOps o;
|
||||
|
||||
int pcap_avail = 0;
|
||||
int rawsock_avail = 0;
|
||||
int winbug = 0;
|
||||
extern int iphlp_avail;
|
||||
extern int net_avail;
|
||||
|
||||
/* internal functions */
|
||||
static void winip_cleanup(void);
|
||||
static void winip_init_pcap(char *a);
|
||||
static void winip_test(int needraw);
|
||||
static void winip_list_interfaces();
|
||||
|
||||
/* delay-load hooks only for troubleshooting */
|
||||
#ifdef _MSC_VER
|
||||
static int dli_done = 0;
|
||||
static FARPROC WINAPI winip_dli_fail_hook(unsigned code, PDelayLoadInfo info);
|
||||
#endif
|
||||
|
||||
// The tables
|
||||
|
||||
typedef struct _WINIP_NAME {
|
||||
char name[16];
|
||||
int ifi;
|
||||
} WINIP_NAME;
|
||||
|
||||
PCHAR iftnames[] =
|
||||
{"net", "eth", "ppp", "loopback", "serial", "isdn", "slip"};
|
||||
// 0 1 2 3 4 5 6
|
||||
|
||||
int iftypes[] = {0,
|
||||
0, 0, 0, 0, 0, // 1-5
|
||||
1, 0, 0, 0, 0, // 6-10
|
||||
0, 0, 0, 0, 0, // 11-15
|
||||
0, 0, 0, 0, 5, // 16-20
|
||||
5, 4, 2, 3, 0, // 21-25
|
||||
1, 0, 6, 0, 0, // 26-30
|
||||
0, 0}; // 31-32
|
||||
|
||||
int iftnums[7];
|
||||
|
||||
static WINIP_IF *iftable;
|
||||
static int numifs, numips;
|
||||
static WINIP_NAME *nametable;
|
||||
|
||||
static int inited;
|
||||
static char pcaplist[4096];
|
||||
|
||||
// windows-specific options
|
||||
struct winops wo;
|
||||
|
||||
// Free this on cleanup
|
||||
static IPNODE *ipblock;
|
||||
|
||||
// For XP-friendly raw sends
|
||||
SOCKET global_raw_socket;
|
||||
|
||||
// Fix for MinGW
|
||||
// MinGW support
|
||||
#ifndef _MSC_VER
|
||||
typedef struct _OSVERSIONINFOEXA {
|
||||
DWORD dwOSVersionInfoSize;
|
||||
DWORD dwMajorVersion;
|
||||
DWORD dwMinorVersion;
|
||||
DWORD dwBuildNumber;
|
||||
DWORD dwPlatformId;
|
||||
CHAR szCSDVersion[ 128 ];
|
||||
WORD wServicePackMajor;
|
||||
WORD wServicePackMinor;
|
||||
WORD wSuiteMask;
|
||||
BYTE wProductType;
|
||||
BYTE wReserved;
|
||||
} OSVERSIONINFOEXA, *POSVERSIONINFOEXA, *LPOSVERSIONINFOEXA, OSVERSIONINFOEX, *POSVERSIONINFOEX;
|
||||
#endif // _MSC_VER
|
||||
|
||||
void winip_barf(const char *msg)
|
||||
{
|
||||
if(inited != 3) fatal("%s", msg ? msg : "You need raw support for this.\n"
|
||||
" run \"nmap --win_list_interfaces --win_trace\" to troubleshoot\n");
|
||||
if(msg) printf("%s\n\n", msg);
|
||||
printf("\nYour system doesn't have iphlpapi.dll\n\nIf you have Win95, "
|
||||
"maybe you could grab it from a Win98 system\n"
|
||||
"If you have NT4, you need service pack 4 or higher\n"
|
||||
"If you have NT3.51, try grabbing it from an NT4 system\n"
|
||||
"Otherwise, your system has problems ;-)\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void winip_init()
|
||||
{
|
||||
if(inited != 0) return;
|
||||
inited = 1;
|
||||
|
||||
ZeroMemory(&wo, sizeof(wo));
|
||||
}
|
||||
|
||||
void winip_postopt_init()
|
||||
{
|
||||
// variables
|
||||
DWORD cb = 0;
|
||||
PMIB_IFTABLE pTable = (PMIB_IFTABLE)&cb;
|
||||
DWORD nRes;
|
||||
OSVERSIONINFOEX ver;
|
||||
PMIB_IPADDRTABLE pIp = 0;
|
||||
int i;
|
||||
IPNODE *nextip;
|
||||
int numipsleft;
|
||||
WORD werd;
|
||||
WSADATA data;
|
||||
|
||||
if(inited != 1)
|
||||
return;
|
||||
inited = 2;
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#if _MSC_VER >= 1300
|
||||
__pfnDliFailureHook2 = winip_dli_fail_hook;
|
||||
#else
|
||||
__pfnDliFailureHook = winip_dli_fail_hook;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
werd = MAKEWORD( 2, 2 );
|
||||
if( (WSAStartup(werd, &data)) !=0 )
|
||||
fatal("failed to start winsock.\n");
|
||||
|
||||
ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
|
||||
if(!GetVersionEx((LPOSVERSIONINFO)&ver))
|
||||
{
|
||||
if(wo.trace) printf("***WinIP*** not win2k -- trying basic version info\n");
|
||||
ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
|
||||
if(!GetVersionEx((LPOSVERSIONINFO)&ver))
|
||||
fatal("GetVersionEx failed\n");
|
||||
|
||||
ver.wServicePackMajor = 0;
|
||||
ver.wServicePackMinor = 0;
|
||||
}
|
||||
|
||||
/* // Test for win_noiphlpapi
|
||||
if(wo.noiphlpapi)
|
||||
{
|
||||
if(wo.trace) printf("***WinIP*** testing absence of iphlpapi\n");
|
||||
o.isr00t = 0;
|
||||
inited = 3;
|
||||
if(wo.listinterfaces) winip_barf(0);
|
||||
return;
|
||||
}*/
|
||||
|
||||
// Read the size
|
||||
if(wo.trace) printf("***WinIP*** initializing if tables\n");
|
||||
nRes = GetIfTableSafe(pTable, &cb, TRUE);
|
||||
|
||||
if(!net_avail)
|
||||
{
|
||||
// we have neither iphlpapi.dll nor inetmib1.dll
|
||||
o.isr00t = 0;
|
||||
inited = 3;
|
||||
if(wo.trace) printf("***WinIP*** neither iphlpapi nor inetmib1 is available\n");
|
||||
if(wo.listinterfaces) winip_barf(0);
|
||||
return;
|
||||
}
|
||||
|
||||
if(!iphlp_avail && wo.trace)
|
||||
printf("***WinIP*** no iphlpapi; using inetmib1 instead\n");
|
||||
|
||||
if(nRes != NO_ERROR && nRes != ERROR_INSUFFICIENT_BUFFER
|
||||
&& nRes != ERROR_BUFFER_OVERFLOW)
|
||||
fatal("failed to get size of interface table\n");
|
||||
|
||||
// Read the data
|
||||
pTable = (PMIB_IFTABLE)_alloca(cb + sizeof(MIB_IFROW));
|
||||
nRes = GetIfTableSafe(pTable, &cb, TRUE);
|
||||
if(nRes != NO_ERROR)
|
||||
fatal("failed to read interface table -- try again\n");
|
||||
numifs = pTable->dwNumEntries;
|
||||
|
||||
cb = 0;
|
||||
nRes = GetIpAddrTableSafe(pIp, &cb, FALSE);
|
||||
if(nRes != NO_ERROR && nRes != ERROR_INSUFFICIENT_BUFFER)
|
||||
fatal("failed to get size of IP address table\n");
|
||||
|
||||
// Read the data
|
||||
pIp = (PMIB_IPADDRTABLE)_alloca(cb + sizeof(MIB_IPADDRROW));
|
||||
nRes = GetIpAddrTableSafe(pIp, &cb, FALSE);
|
||||
if(nRes != NO_ERROR)
|
||||
fatal("failed to read IP address table\n");
|
||||
|
||||
// Allocate storage
|
||||
iftable = (WINIP_IF*)calloc(numifs, sizeof(WINIP_IF));
|
||||
nametable = (WINIP_NAME*)calloc(numifs, sizeof(WINIP_NAME));
|
||||
ipblock = (IPNODE*)calloc(pIp->dwNumEntries, sizeof(IPNODE));
|
||||
nextip = ipblock;
|
||||
numipsleft = pIp->dwNumEntries;
|
||||
numips = pIp->dwNumEntries;
|
||||
|
||||
// Fill in the table
|
||||
for(i = 0; i < numifs; i++)
|
||||
{
|
||||
int ift;
|
||||
int j;
|
||||
|
||||
iftable[i].winif = pTable->table[i].dwIndex;
|
||||
iftable[i].type = pTable->table[i].dwType;
|
||||
iftable[i].firstip = 0;
|
||||
|
||||
nametable[i].ifi = i;
|
||||
|
||||
memcpy(iftable[i].physaddr,
|
||||
pTable->table[i].bPhysAddr,
|
||||
pTable->table[i].dwPhysAddrLen);
|
||||
iftable[i].physlen = pTable->table[i].dwPhysAddrLen;
|
||||
|
||||
ift = iftypes[iftable[i].type];
|
||||
sprintf(iftable[i].name, "%s%d", iftnames[ift], iftnums[ift]++);
|
||||
strcpy(nametable[i].name, iftable[i].name);
|
||||
|
||||
// Find an IP address
|
||||
for(j = 0; j < pIp->dwNumEntries; j++)
|
||||
{
|
||||
if(pIp->table[j].dwIndex == iftable[i].winif)
|
||||
{
|
||||
if(!numipsleft)
|
||||
fatal("internal error in winip_init\n");
|
||||
numipsleft--;
|
||||
|
||||
nextip->ip = pIp->table[j].dwAddr;
|
||||
nextip->next = iftable[i].firstip;
|
||||
nextip->ifi = i;
|
||||
iftable[i].firstip = nextip;
|
||||
nextip++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(wo.trace) printf("***WinIP*** if tables complete :)\n");
|
||||
|
||||
// Try to initialize winpcap
|
||||
#ifdef _MSC_VER
|
||||
__try
|
||||
#endif
|
||||
{
|
||||
ULONG len = sizeof(pcaplist);
|
||||
|
||||
if(wo.nopcap)
|
||||
{
|
||||
if(o.debugging > 1 && wo.trace)
|
||||
printf("***WinIP*** winpcap support disabled\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
pcap_avail = 1;
|
||||
if(wo.trace) printf("***WinIP*** trying to initialize winpcap 2.1\n");
|
||||
PacketGetAdapterNames(pcaplist, &len);
|
||||
if(o.debugging || wo.trace)
|
||||
printf("***WinIP*** winpcap present, dynamic linked to: %s\n", pcap_lib_version());
|
||||
}
|
||||
}
|
||||
#ifdef _MSC_VER
|
||||
__except(GetExceptionCode() == DLI_ERROR)
|
||||
{
|
||||
pcap_avail = 0;
|
||||
printf("WARNING: Failed to locate Winpcap. Nmap may not function properly until this is installed! WinPcap is freely available from http://winpcap.polito.it.\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
// Check for a wpcap.dll (so we don't crash on old winpcap
|
||||
// But only with VC++.NET, since old versions do not
|
||||
// provide this functionality :(
|
||||
#if defined(_MSC_VER) && _MSC_VER >= 1300
|
||||
if(pcap_avail)
|
||||
{
|
||||
if(FAILED(__HrLoadAllImportsForDll("wpcap.dll")))
|
||||
{
|
||||
if(wo.trace) printf("***WinIP*** your winpcap is too old\n");
|
||||
pcap_avail = 0;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// Do we have rawsock?
|
||||
if(wo.forcerawsock ||
|
||||
(ver.dwPlatformId == VER_PLATFORM_WIN32_NT
|
||||
&& ver.dwMajorVersion >= 5 && !wo.norawsock))
|
||||
{
|
||||
SOCKET s = INVALID_SOCKET;
|
||||
// we need to bind before non-admin
|
||||
// will detect the failure
|
||||
struct sockaddr_in sin;
|
||||
ZeroMemory(&sin, sizeof(sin));
|
||||
sin.sin_family = AF_INET;
|
||||
sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
|
||||
if(wo.trace) printf("***WinIP*** testing for raw sockets\n");
|
||||
|
||||
s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
|
||||
if(s != INVALID_SOCKET
|
||||
&& !bind(s, (struct sockaddr*)&sin, sizeof(sin)))
|
||||
{
|
||||
rawsock_avail = 1;
|
||||
global_raw_socket = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
|
||||
sethdrinclude((int)global_raw_socket);
|
||||
unblock_socket(global_raw_socket);
|
||||
closesocket(s);
|
||||
if(o.debugging > 1 || wo.trace)
|
||||
printf("***WinIP*** rawsock is available\n");
|
||||
}
|
||||
else if(o.debugging > 1 || wo.trace)
|
||||
{
|
||||
if(s == INVALID_SOCKET)
|
||||
printf("***WinIP*** rawsock init failed\n");
|
||||
else printf("***WinIP*** rawsock bind failed (most likely not admin)\n");
|
||||
}
|
||||
}
|
||||
else if(o.debugging > 1 || wo.trace)
|
||||
printf("***WinIP*** didn't try rawsock\n");
|
||||
|
||||
if(rawsock_avail && o.ipprotscan
|
||||
&& ver.dwPlatformId == VER_PLATFORM_WIN32_NT
|
||||
&& ver.dwMajorVersion == 5
|
||||
&& ver.dwMajorVersion == 0
|
||||
&& ver.wServicePackMajor == 0)
|
||||
{
|
||||
// Prevent a BSOD (we're on W2K SP0)
|
||||
if(wo.trace) printf("***WinIP*** disabling rawsock to avoid BSOD due to ipprotoscan\n");
|
||||
winbug = 1;
|
||||
rawsock_avail = 0;
|
||||
}
|
||||
|
||||
if(pcap_avail)
|
||||
{
|
||||
if(wo.trace) printf("***WinIP*** reading winpcap interface list\n");
|
||||
|
||||
if(ver.dwPlatformId == VER_PLATFORM_WIN32_NT && pcaplist[1] == '\0')
|
||||
{
|
||||
// NT version or WinPcap using Unicode names
|
||||
WCHAR *a = (WCHAR*)pcaplist;
|
||||
while(*a)
|
||||
{
|
||||
if (wo.trace) printf("***WinIP*** init %S (Unicode)\n", a);
|
||||
winip_init_pcap((char*)a);
|
||||
a += wcslen(a) + 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 9x/Me version or WinPcap 3.1 using ASCII names
|
||||
char *a = pcaplist;
|
||||
while(*a)
|
||||
{
|
||||
if (wo.trace) printf("***WinIP*** init %s (ASCII)\n", a);
|
||||
winip_init_pcap(a);
|
||||
a += strlen(a) + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
o.isr00t = (pcap_avail | rawsock_avail);
|
||||
if(wo.trace) printf("***WinIP*** o.isr00t = %d\n", o.isr00t);
|
||||
|
||||
qsort(nametable, numifs, sizeof(WINIP_NAME), (int (*)(const void *, const void *)) strcmp);
|
||||
atexit(winip_cleanup);
|
||||
|
||||
if(wo.listinterfaces)
|
||||
{
|
||||
winip_list_interfaces();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
// Check for NT4 (grr...)
|
||||
if(ver.dwPlatformId == VER_PLATFORM_WIN32_NT
|
||||
&& ver.dwMajorVersion < 5) wo.nt4route = 1;
|
||||
|
||||
// Mark load as complete so that dli errors are handled
|
||||
#ifdef _MSC_VER
|
||||
dli_done = 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void winip_test(int needraw)
|
||||
{
|
||||
if(inited < 2)
|
||||
fatal("winip not initialized yet\n");
|
||||
else if(needraw && inited == 3) winip_barf(0);
|
||||
}
|
||||
|
||||
static void winip_init_pcap(char *a)
|
||||
{
|
||||
// Write the names to the cache
|
||||
PPACKET_OID_DATA OidData;
|
||||
int i;
|
||||
|
||||
// Get the physaddr from Packet32
|
||||
BYTE phys[MAXLEN_PHYSADDR];
|
||||
int len = 6; // Ethernet
|
||||
|
||||
LPADAPTER pAdap;
|
||||
|
||||
char *foobar = a[1] ? "%s" : "%S";
|
||||
if(wo.trace)
|
||||
{
|
||||
printf("pcap device: ");
|
||||
printf(foobar, a);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
OidData=(struct _PACKET_OID_DATA *) _alloca(sizeof(PACKET_OID_DATA)+MAXLEN_PHYSADDR-1);
|
||||
|
||||
// The next line needs to be changed to support non-Ethernet devices
|
||||
OidData->Oid = OID_802_3_CURRENT_ADDRESS;
|
||||
OidData->Length = len;
|
||||
|
||||
pAdap = PacketOpenAdapter(a);
|
||||
if(!pAdap)
|
||||
{
|
||||
if(wo.trace) printf(" result: failed to open\n");
|
||||
return; // unopenable
|
||||
}
|
||||
|
||||
if(PacketRequest(pAdap,FALSE,OidData))
|
||||
{
|
||||
// we have an supported device
|
||||
for(i = 0; i < numifs; i++)
|
||||
{
|
||||
if(iftable[i].physlen == 6
|
||||
&& 0 == memcmp(iftable[i].physaddr, OidData->Data, len))
|
||||
{
|
||||
if(wo.trace)
|
||||
{
|
||||
int l;
|
||||
printf(" result: physaddr (0x");
|
||||
for(l = 0; l < len; l++)
|
||||
{
|
||||
char blah[3];
|
||||
printf("%02s", _itoa(OidData->Data[l], blah, 16));
|
||||
}
|
||||
printf(") matches %s\n", iftable[i].name);
|
||||
}
|
||||
iftable[i].pcapname = a;
|
||||
break; // Out of the j-loop
|
||||
}
|
||||
}
|
||||
|
||||
// else ignore the non-Ethernet device
|
||||
if(i == numifs && wo.trace)
|
||||
{
|
||||
int l;
|
||||
printf(" result: no match (physaddr = 0x");
|
||||
for(l = 0; l < len; l++)
|
||||
{
|
||||
char blah[3];
|
||||
printf("%02s", _itoa(OidData->Data[l], blah, 16));
|
||||
}
|
||||
printf(")\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
PacketCloseAdapter(pAdap);
|
||||
}
|
||||
|
||||
static void winip_cleanup(void)
|
||||
{
|
||||
free(ipblock);
|
||||
|
||||
WSACleanup();
|
||||
}
|
||||
|
||||
// name translation
|
||||
int name2ifi(const char *name)
|
||||
{
|
||||
WINIP_NAME *n = (WINIP_NAME*)bsearch(name, nametable, numifs,
|
||||
sizeof(WINIP_NAME), (int (*)(const void *, const void *)) strcmp);
|
||||
if(!n) return -1;
|
||||
|
||||
return n->ifi;
|
||||
}
|
||||
|
||||
const char *ifi2name(int ifi)
|
||||
{
|
||||
if(ifi < 0 || ifi >= numifs) return 0;
|
||||
|
||||
return iftable[ifi].name;
|
||||
}
|
||||
|
||||
int ifi2winif(int ifi)
|
||||
{
|
||||
if(ifi < 0 || ifi >= numifs) return -1;
|
||||
|
||||
return iftable[ifi].winif;
|
||||
}
|
||||
|
||||
const WINIP_IF* ifi2ifentry(int ifi)
|
||||
{
|
||||
if(ifi < 0 || ifi >= numifs) return 0;
|
||||
|
||||
return iftable + ifi;
|
||||
}
|
||||
|
||||
static int cmp_uint(const void *e1, const void *e2)
|
||||
{
|
||||
return *(DWORD*)e1 - *(DWORD*)e2;
|
||||
}
|
||||
|
||||
int winif2ifi(int winif)
|
||||
{
|
||||
WINIP_IF *x = (WINIP_IF*)bsearch(&winif, iftable, numifs,
|
||||
sizeof(WINIP_IF), cmp_uint);
|
||||
if(!x) return -1;
|
||||
|
||||
return x - iftable;
|
||||
}
|
||||
|
||||
int ifi2ipaddr(int ifi, struct in_addr *addr)
|
||||
{
|
||||
if(ifi < 0 || ifi >= numifs) return -1;
|
||||
|
||||
if(!iftable[ifi].firstip) return -1;
|
||||
|
||||
addr->s_addr = iftable[ifi].firstip->ip;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ipaddr2ifi(DWORD ip)
|
||||
{
|
||||
// Amusing hack
|
||||
// Note: this is slow since I see no reason to make it fast
|
||||
int i;
|
||||
for(i = 0; i < numips; i++)
|
||||
{
|
||||
if(ipblock[i].ip == ip)
|
||||
return ipblock[i].ifi;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int devname2ipaddr(char *dev, struct in_addr *addr)
|
||||
{
|
||||
return ifi2ipaddr(name2ifi(dev), addr);
|
||||
}
|
||||
|
||||
int ipaddr2devname( char *dev, const struct in_addr *addr )
|
||||
{
|
||||
int ifi = ipaddr2ifi(addr->s_addr);
|
||||
if(ifi == -1) return -1;
|
||||
|
||||
strcpy(dev, iftable[ifi].name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void winip_list_interfaces()
|
||||
{
|
||||
int i;
|
||||
|
||||
if(inited == 3)
|
||||
winip_barf(0);
|
||||
|
||||
printf("Available interfaces:\n\n");
|
||||
|
||||
// 0000000000111111111122222222223333333333
|
||||
// 0123456789012345678901234567890123456789
|
||||
printf("Name Raw mode IP\n");
|
||||
|
||||
for(i = 0; i < numifs; i++)
|
||||
{
|
||||
/* char *addr = "(query failed)";
|
||||
char extra[32];
|
||||
if(iftable[i].firstip)
|
||||
addr = inet_ntoa(*(struct in_addr*)&iftable[i].firstip->ip);
|
||||
if(iftable[i].pcapname)
|
||||
strcpy(extra, rawsock_avail ? "winpcap, rawsock" : "winpcap");
|
||||
else strcpy(extra, rawsock_avail ? "rawsock" : "no raw");
|
||||
printf("%s: %s (%s)\n", iftable[i].name,
|
||||
addr, extra);
|
||||
if(o.debugging && iftable[i].pcapname)
|
||||
printf(iftable[i].pcapname[1] ? " winpcap: %s\n"
|
||||
: " winpcap: %ls\n", iftable[i].pcapname);*/
|
||||
|
||||
IPNODE *ip = iftable[i].firstip;
|
||||
|
||||
printf("%-12s%-10s", iftable[i].name,
|
||||
(iftable[i].pcapname ? "winpcap" : (rawsock_avail ? "SOCK_RAW" : "none")));
|
||||
if(!ip) printf("[none]\n");
|
||||
else while(ip)
|
||||
{
|
||||
if(ip != iftable[i].firstip) printf(" -- ");
|
||||
printf("%s\n", inet_ntoa(*(struct in_addr*)&ip->ip));
|
||||
ip = ip->next;
|
||||
}
|
||||
|
||||
if(o.debugging && iftable[i].pcapname)
|
||||
printf(iftable[i].pcapname[1] ? " winpcap: %s\n"
|
||||
: " winpcap: %ls\n", iftable[i].pcapname);
|
||||
}
|
||||
}
|
||||
|
||||
typedef DWORD (__stdcall *PGBI)(IPAddr, PDWORD);
|
||||
|
||||
// socket and sendto replacements
|
||||
int win32_sendto(int sd, const char *packet, int len,
|
||||
unsigned int flags, struct sockaddr *to, int tolen)
|
||||
{
|
||||
/* COMMENTED OUT SINCE pcapsend* is obsolete -- need to use new Nmap infrastructure
|
||||
if(sd == 501)
|
||||
return pcapsendraw(packet, len, to, tolen);
|
||||
else */
|
||||
return sendto(sd, packet, len, flags, to, tolen);
|
||||
}
|
||||
|
||||
int Sendto(char *functionname, int sd, const unsigned char *packet, int len,
|
||||
unsigned int flags, struct sockaddr *to, int tolen)
|
||||
{
|
||||
PacketTrace::trace(PacketTrace::SENT, packet, len);
|
||||
return win32_sendto(sd, (char *) packet, len, flags, to, tolen);
|
||||
}
|
||||
|
||||
int win32_socket(int af, int type, int proto)
|
||||
{
|
||||
SOCKET s;
|
||||
winip_test(0);
|
||||
|
||||
if(type == SOCK_RAW && proto == IPPROTO_RAW)
|
||||
{
|
||||
winip_test(1);
|
||||
/* no longer using this pcapsend stuff
|
||||
pcapsend_init();
|
||||
*/
|
||||
return 501;
|
||||
}
|
||||
|
||||
s = socket(af, type, proto);
|
||||
|
||||
// Do this here to save a little time
|
||||
if(type == SOCK_RAW && proto == IPPROTO_RAW) sethdrinclude(s);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
void win32_pcap_close(pcap_t *pd)
|
||||
{
|
||||
if(-2 != (long)pd) pcap_close(pd);
|
||||
else rawrecv_close(pd);
|
||||
}
|
||||
|
||||
pcap_t *my_pcap_open_live(const char *device, int snaplen, int promisc, int to_ms)
|
||||
{
|
||||
int ifi = name2ifi(device);
|
||||
if(ifi == -1)
|
||||
fatal("my_pcap_open_live: invalid device %s\n");
|
||||
|
||||
winip_test(1);
|
||||
|
||||
if(iftable[ifi].pcapname)
|
||||
return my_real_pcap_open_live(device, snaplen, promisc, to_ms);
|
||||
|
||||
else if(rawsock_avail)
|
||||
{
|
||||
if(promisc)
|
||||
fatal("promiscuous capture not available on non-pcap device %s\n", device);
|
||||
return rawrecv_open(device);
|
||||
}
|
||||
|
||||
else
|
||||
fatal(winbug ? "%s: rawsock disabled to avoid BSOD\n"
|
||||
: "%s: no raw access\n", device);
|
||||
|
||||
return 0; // to make the compiler happy
|
||||
}
|
||||
|
||||
int winip_corruption_possible()
|
||||
{
|
||||
return rawsock_avail; // for now
|
||||
}
|
||||
|
||||
void sethdrinclude(int sd)
|
||||
{
|
||||
int one = 1;
|
||||
if(sd != 501)
|
||||
{
|
||||
// error("sethdrinclude called -- this probably shouldn't happen\n");
|
||||
setsockopt(sd, IPPROTO_IP, IP_HDRINCL, (char *) &one, sizeof(one));
|
||||
}
|
||||
}
|
||||
|
||||
void set_pcap_filter(const char *device,
|
||||
pcap_t *pd, PFILTERFN filter, char *bpf, ...)
|
||||
{
|
||||
va_list ap;
|
||||
char buf[3072]; // same size as bpf ie size of filter in scan_engine.cc
|
||||
struct bpf_program fcode;
|
||||
unsigned int localnet, netmask;
|
||||
char err0r[256];
|
||||
|
||||
if(-2 == (long)pd)
|
||||
{
|
||||
rawrecv_setfilter(pd, filter);
|
||||
return;
|
||||
}
|
||||
|
||||
if (pcap_lookupnet(device, &localnet, &netmask, err0r) == -1)
|
||||
; /* fatal("Failed to lookup device subnet/netmask: %s", err0r);*/
|
||||
|
||||
va_start(ap, bpf);
|
||||
if (vsnprintf(buf, sizeof(buf), bpf, ap) < 0)
|
||||
{
|
||||
fatal("Failed to copy the filter string %s",bpf);
|
||||
}
|
||||
va_end(ap);
|
||||
|
||||
if (o.debugging)
|
||||
log_write(LOG_STDOUT, "Packet capture filter: %s\n", buf);
|
||||
|
||||
if (pcap_compile(pd, &fcode, buf, 0, netmask) < 0)
|
||||
fatal("Error compiling our pcap filter: %s\n", pcap_geterr(pd));
|
||||
if (pcap_setfilter(pd, &fcode) < 0 )
|
||||
fatal("Failed to set the pcap filter: %s\n", pcap_geterr(pd));
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
static FARPROC WINAPI winip_dli_fail_hook(unsigned code, PDelayLoadInfo info)
|
||||
{
|
||||
if(wo.trace)
|
||||
{
|
||||
printf("***WinIP*** delay load error:\n");
|
||||
switch(code)
|
||||
{
|
||||
case dliFailLoadLib:
|
||||
printf(" failed to load dll: %s\n", info->szDll);
|
||||
break;
|
||||
|
||||
case dliFailGetProc:
|
||||
printf(" failed to load ");
|
||||
if(info->dlp.fImportByName)
|
||||
printf("function %s", info->dlp.szProcName + 2);
|
||||
else printf("ordinal %d", info->dlp.dwOrdinal);
|
||||
printf(" in dll %s\n", info->szDll);
|
||||
break;
|
||||
|
||||
default:
|
||||
printf(" unknown error\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(dli_done)
|
||||
{
|
||||
printf("******* Unexpected delay-load failure *******\n");
|
||||
|
||||
switch(code)
|
||||
{
|
||||
case dliFailLoadLib:
|
||||
printf(" failed to load dll: %s\n", info->szDll);
|
||||
if(!stricmp(info->szDll, "wpcap.dll"))
|
||||
printf(" this is most likely because you have"
|
||||
" winpcap 2.0 (2.1 or later is required)\n"
|
||||
"Get it from http://netgroup-serv.polito.it/winpcap\n");
|
||||
break;
|
||||
|
||||
case dliFailGetProc:
|
||||
printf(" failed to load ");
|
||||
if(info->dlp.fImportByName)
|
||||
printf("function %s", info->dlp.szProcName + 2);
|
||||
else printf("ordinal %d", info->dlp.dwOrdinal);
|
||||
printf(" in dll %s\n", info->szDll);
|
||||
break;
|
||||
|
||||
default:
|
||||
printf(" unknown error\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif // _MSC_VER
|
||||
Reference in New Issue
Block a user