1 /* $Id: os_matrix.cc,v 1.8 2005/07/18 11:08:24 mederchik Exp $ */
2 /*
3 ** Copyright (C) 2001 Fyodor Yarochkin <fygrave@tigerteam.net>,
4 **                    Ofir Arkin       <ofir@sys-security.com>
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 **
12 ** This program is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ** GNU General Public License for more details.
16 **
17 ** You should have received a copy of the GNU General Public License
18 ** along with this program; if not, write to the Free Software
19 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21 
22 #include "xprobe.h"
23 #include "os_matrix.h"
24 #include "interface.h"
25 #include "xprobe_module_hdlr.h"
26 
27 extern Interface *ui;
28 
29 /*
30  * OS_Name object.
31  *****************
32  */
33 
OS_Name(void)34 OS_Name::OS_Name(void) {
35 
36     id_count = 0;
37 
38 }
39 
40 /*
41  * OS_Name::add_os()
42  *******************
43  * returns FAIL is the OS already exist. os_id otherwise.
44  */
45 
46 
add_os(string & os_name)47 int OS_Name::add_os(string &os_name) {
48 
49     if (find_os(os_name) != FAIL) return FAIL; /* exist */
50 
51     osid_name.insert(pair<int, string>(id_count, os_name));
52     return (id_count++);
53 }
54 
55 
56 /*
57  * OS_Name::find_os()
58  *******************
59  * returns FAIL is the OS does not exist. os_id otherwise.
60  */
61 
62 
find_os(string & os_name)63 int OS_Name::find_os(string &os_name) {
64     map <int, string>::iterator osid_i;
65 
66     for (osid_i = osid_name.begin();
67          osid_i != osid_name.end(); osid_i++) {
68         if ((*osid_i).second == os_name) return ((*osid_i).first); /* exist */
69     }
70     return FAIL; /* does not exist */
71 }
72 
73 
74 /*
75  * OS_Name::list_oses()
76  *******************
77  * for debugging _ONLY_
78  */
79 
80 
list_oses(void)81 void OS_Name::list_oses(void) {
82     map <int, string>::iterator osid_i;
83 
84     xprobe_mdebug(XPROBE_DEBUG_OSMATRIX,"Following systems are recognizable\n");
85     for (osid_i = osid_name.begin();
86          osid_i != osid_name.end(); osid_i++) {
87         xprobe_debug(XPROBE_DEBUG_OSMATRIX,"Id: %i\tOS: %s\n",(*osid_i).first, (*osid_i).second.c_str());
88     }
89 }
90 
91 
92 /*
93  * OS_Name::list_oses()
94  *******************
95  * for debugging _ONLY_
96  */
97 
98 
99 
osid2str(int id)100 const string OS_Name::osid2str(int id) {
101     map <int, string>::iterator osid_i = osid_name.find(id);
102     if (osid_i != osid_name.end()) return ((*osid_i).second);
103     return ("BUG, PLEASE REPORT! :-)");
104 }
105 
106 /*
107  * OS_Vector stuff:
108  */
OS_Vector(int new_os_id)109 OS_Vector::OS_Vector(int new_os_id) {
110     os_id = new_os_id;
111     total = 0;
112 	numofkwds=0;
113 }
114 
add_result(int test_id,int score)115 void OS_Vector::add_result(int test_id, int score) {
116     xprobe_debug(XPROBE_DEBUG_OSMATRIX, "added: test_id: %i score: %i\n",
117      test_id, score);
118     total += score;
119 	numofkwds++;
120 }
121 
os_vector_compare(const OS_Vector & a,const OS_Vector & b)122 bool os_vector_compare(const OS_Vector &a, const OS_Vector &b) {
123 
124     if (a.total > b.total) return true;
125     return false;
126 }
127 
128 
129 
OS_Matrix(int mods)130 OS_Matrix::OS_Matrix(int mods) {
131 
132     xprobe_mdebug(XPROBE_DEBUG_INIT, "OS matrix initialized\n");
133     xp_loaded_mods =mods;
134 
135 }
136 
~OS_Matrix(void)137 OS_Matrix::~OS_Matrix(void) {
138 
139     xprobe_mdebug(XPROBE_DEBUG_INIT, "OS matrix deinitialized\n");
140 
141 }
142 
143 
find_os_id(int os_id)144 int OS_Matrix::find_os_id(int os_id) {
145     unsigned int i;
146 
147     for (i = 0; i< osid_vec.size(); i++)
148         if (os_id == osid_vec[i].get_os_id()) return i;
149     return -1;
150 }
151 
add_result(int test_id,int os_id,int score,int times)152 void OS_Matrix::add_result(int test_id, int os_id, int score, int times) {
153     int i;
154 
155     xprobe_debug(XPROBE_DEBUG_OSMATRIX, "test_id: %i os_id: %i score: %i\n", test_id, os_id, score);
156 
157     if (find_os_id(os_id) == -1) /* if doesn't exist. we insert it
158                                       * first */
159         osid_vec.push_back(OS_Vector(os_id));
160 
161     i = find_os_id(os_id);
162 	while (times-- > 0) {
163 	    osid_vec[i].add_result(test_id, score);
164 	}
165 }
166 
get_score(int os_id)167 int OS_Matrix::get_score(int os_id) {
168 
169     if (find_os_id(os_id) == -1) return FAIL;
170 
171     return (osid_vec[find_os_id(os_id)].get_total());
172 }
173 
get_max_score(int os_id)174 int OS_Matrix::get_max_score(int os_id) {
175 	int i = find_os_id(os_id);
176 
177     //return (xp_loaded_mods * XPROBE_MATCH_YES);
178 	return (osid_vec[i].get_number_of_keywords() * XPROBE_MATCH_YES);
179 
180 }
181 
get_prcnt_score(int os_id)182 int OS_Matrix::get_prcnt_score(int os_id) {
183 
184     if (get_score(os_id) < 0) return 0;
185     return get_score(os_id) * 100/get_max_score(os_id);
186 
187 }
188 
get_top(int num)189 int OS_Matrix::get_top(int num) {
190 
191     sort(osid_vec.begin(), osid_vec.end(), os_vector_compare);
192 
193     if ((unsigned int)num < osid_vec.size())
194         return osid_vec[num].get_os_id();
195 
196     return 0; /* out of range */
197 }
198 
199