1 
2 /***************************************************************************
3  * MACLookup.cc -- This relatively simple system handles looking up the    *
4  * vendor registered to a MAC address using the nmap-mac-prefixes          *
5  * database.                                                               *
6  *                                                                         *
7  ***********************IMPORTANT NMAP LICENSE TERMS************************
8  *                                                                         *
9  * The Nmap Security Scanner is (C) 1996-2020 Insecure.Com LLC ("The Nmap  *
10  * Project"). Nmap is also a registered trademark of the Nmap Project.     *
11  *                                                                         *
12  * This program is distributed under the terms of the Nmap Public Source   *
13  * License (NPSL). The exact license text applying to a particular Nmap    *
14  * release or source code control revision is contained in the LICENSE     *
15  * file distributed with that version of Nmap or source code control       *
16  * revision. More Nmap copyright/legal information is available from       *
17  * https://nmap.org/book/man-legal.html, and further information on the    *
18  * NPSL license itself can be found at https://nmap.org/npsl. This header  *
19  * summarizes some key points from the Nmap license, but is no substitute  *
20  * for the actual license text.                                            *
21  *                                                                         *
22  * Nmap is generally free for end users to download and use themselves,    *
23  * including commercial use. It is available from https://nmap.org.        *
24  *                                                                         *
25  * The Nmap license generally prohibits companies from using and           *
26  * redistributing Nmap in commercial products, but we sell a special Nmap  *
27  * OEM Edition with a more permissive license and special features for     *
28  * this purpose. See https://nmap.org/oem                                  *
29  *                                                                         *
30  * If you have received a written Nmap license agreement or contract       *
31  * stating terms other than these (such as an Nmap OEM license), you may   *
32  * choose to use and redistribute Nmap under those terms instead.          *
33  *                                                                         *
34  * The official Nmap Windows builds include the Npcap software             *
35  * (https://npcap.org) for packet capture and transmission. It is under    *
36  * separate license terms which forbid redistribution without special      *
37  * permission. So the official Nmap Windows builds may not be              *
38  * redistributed without special permission (such as an Nmap OEM           *
39  * license).                                                               *
40  *                                                                         *
41  * Source is provided to this software because we believe users have a     *
42  * right to know exactly what a program is going to do before they run it. *
43  * This also allows you to audit the software for security holes.          *
44  *                                                                         *
45  * Source code also allows you to port Nmap to new platforms, fix bugs,    *
46  * and add new features.  You are highly encouraged to submit your         *
47  * changes as a Github PR or by email to the dev@nmap.org mailing list     *
48  * for possible incorporation into the main distribution. Unless you       *
49  * specify otherwise, it is understood that you are offering us very       *
50  * broad rights to use your submissions as described in the Nmap Public    *
51  * Source License Contributor Agreement. This is important because we      *
52  * fund the project by selling licenses with various terms, and also       *
53  * because the inability to relicense code has caused devastating          *
54  * problems for other Free Software projects (such as KDE and NASM).       *
55  *                                                                         *
56  * The free version of Nmap is distributed in the hope that it will be     *
57  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of  *
58  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties,        *
59  * indemnification and commercial support are all available through the    *
60  * Npcap OEM program--see https://nmap.org/oem.                            *
61  *                                                                         *
62  ***************************************************************************/
63 
64 /* $Id: MACLookup.cc 38078 2020-10-02 16:12:22Z dmiller $ */
65 
66 #include "nmap.h"
67 
68 #include <map>
69 
70 /* Character pool memory allocation */
71 #include "MACLookup.h"
72 #include "NmapOps.h"
73 #include "nmap_error.h"
74 #include "charpool.h"
75 
76 extern NmapOps o;
77 
78 std::map<int, char *> MacTable;
79 
MacCharPrefix2Key(const u8 * prefix)80 static inline int MacCharPrefix2Key(const u8 *prefix) {
81   return (prefix[0] << 16) + (prefix[1] << 8) + prefix[2];
82 }
83 
mac_prefix_init()84 static void mac_prefix_init() {
85   static int initialized = 0;
86   if (initialized) return;
87   initialized = 1;
88   char filename[256];
89   FILE *fp;
90   char line[128];
91   int pfx;
92   char *endptr, *vendor;
93   int lineno = 0;
94 
95   /* Now it is time to read in all of the entries ... */
96   if (nmap_fetchfile(filename, sizeof(filename), "nmap-mac-prefixes") != 1){
97     error("Cannot find nmap-mac-prefixes: Ethernet vendor correlation will not be performed");
98     return;
99   }
100 
101   fp = fopen(filename, "r");
102   if (!fp) {
103     error("Unable to open %s.  Ethernet vendor correlation will not be performed ", filename);
104     return;
105   }
106   /* Record where this data file was found. */
107   o.loaded_data_files["nmap-mac-prefixes"] = filename;
108 
109   while(fgets(line, sizeof(line), fp)) {
110     lineno++;
111     if (*line == '#') continue;
112     if (!isxdigit((int) (unsigned char) *line)) {
113       error("Parse error on line #%d of %s. Giving up parsing.", lineno, filename);
114       break;
115     }
116     /* First grab the prefix */
117     pfx = strtol(line, &endptr, 16);
118     if (!endptr || !isspace((int) (unsigned char) *endptr)) {
119       error("Parse error on line #%d of %s. Giving up parsing.", lineno, filename);
120       break;
121     }
122     /* Now grab the vendor */
123     while(*endptr && isspace((int) (unsigned char) *endptr)) endptr++;
124     assert(*endptr);
125     vendor = endptr;
126     while(*endptr && *endptr != '\n' && *endptr != '\r') endptr++;
127     *endptr = '\0';
128 
129     if (MacTable.find(pfx) == MacTable.end()) {
130       MacTable[pfx] = cp_strdup(vendor);
131     } else {
132       if (o.debugging > 1)
133         error("MAC prefix %06X is duplicated in %s; ignoring duplicates.", pfx, filename);
134     }
135 
136   }
137 
138   fclose(fp);
139   return;
140 }
141 
142 
findMACEntry(int prefix)143 static const char *findMACEntry(int prefix) {
144   std::map<int, char *>::iterator i;
145 
146   i = MacTable.find(prefix);
147   if (i == MacTable.end())
148     return NULL;
149 
150   return i->second;
151 }
152 
153 /* Takes a three byte MAC address prefix (passing the whole MAC is OK
154    too) and returns the company which has registered the prefix.
155    NULL is returned if no vendor is found for the given prefix or if there
156    is some other error. */
MACPrefix2Corp(const u8 * prefix)157 const char *MACPrefix2Corp(const u8 *prefix) {
158   if (!prefix) fatal("%s called with a NULL prefix", __func__);
159   mac_prefix_init();
160 
161   return findMACEntry(MacCharPrefix2Key(prefix));
162 }
163 
164 /* Takes a string and looks through the table for a vendor name which
165    contains that string.  Sets the first three bytes in mac_data and
166    returns true for the first matching entry found.  If no entries
167    match, leaves mac_data untouched and returns false.  Note that this
168    is not particularly efficient and so should be rewritten if it is
169    called often */
MACCorp2Prefix(const char * vendorstr,u8 * mac_data)170 bool MACCorp2Prefix(const char *vendorstr, u8 *mac_data) {
171   std::map<int, char *>::iterator i;
172 
173   if (!vendorstr) fatal("%s: vendorstr is NULL", __func__);
174   if (!mac_data) fatal("%s: mac_data is NULL", __func__);
175   mac_prefix_init();
176 
177   for (i = MacTable.begin(); i != MacTable.end(); i++) {
178     if (strcasestr(i->second, vendorstr)) {
179       mac_data[0] = i->first >> 16;
180       mac_data[1] = (i->first >> 8) & 0xFF;
181       mac_data[2] = i->first & 0xFF;
182       return true;
183     }
184   }
185   return false;
186 }
187