~ubuntu-branches/ubuntu/karmic/xprobe/karmic

« back to all changes in this revision

Viewing changes to src/xpmodules/os_probe/icmp_inforeq/icmp_inforeq.cc

  • Committer: Bazaar Package Importer
  • Author(s): Richard Atterer
  • Date: 2005-02-22 22:54:24 UTC
  • mfrom: (1.2.1 upstream) (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050222225424-6cqy8rr45pkna819
Tags: 0.2.2-1
New upstream version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: icmp_inforeq.cc,v 1.13 2005/02/09 18:36:46 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
 
 
23
 
 
24
#include "xprobe.h"
 
25
#include "usi++/usi++.h"
 
26
#include <signal.h>
 
27
#include <setjmp.h>
 
28
#define _XPROBE_MODULE
 
29
#include "xplib.h"
 
30
#include "xprobe_module.h"
 
31
#include "xprobe_module_hdlr.h"
 
32
#include "interface.h"
 
33
#include "target.h"
 
34
#include "icmp_inforeq.h"
 
35
 
 
36
extern Interface *ui;
 
37
 
 
38
/* initialization function */
 
39
 
 
40
int icmp_inforeq_mod_init(Xprobe_Module_Hdlr *pt, char *nm) {
 
41
 
 
42
    ICMP_Inforeq_Mod *module = new ICMP_Inforeq_Mod;
 
43
 
 
44
    module->set_name(nm);
 
45
    xprobe_mdebug(XPROBE_DEBUG_MODULES, "Initializing the ICMP Inforeq module\n");
 
46
    pt->register_module(module);
 
47
    pt->add_keyword(module->get_id(),"icmp_info_reply");
 
48
    pt->add_keyword(module->get_id(),"icmp_info_reply_ttl");
 
49
        pt->add_keyword(module->get_id(), "icmp_info_reply_ip_id");
 
50
 
 
51
return OK;
 
52
}
 
53
 
 
54
ICMP_Inforeq_Mod::ICMP_Inforeq_Mod(void):Xprobe_Module(XPROBE_MODULE_OSTEST, "fingerprint:icmp_info","ICMP Information request fingerprinting module") {
 
55
 
 
56
        ICMP_Inforeq_Reply_Check *inforep = new ICMP_Inforeq_Reply_Check;
 
57
        ICMP_Inforeq_Ip_Id_Check *infoid = new ICMP_Inforeq_Ip_Id_Check;
 
58
        ICMP_Inforeq_Ttl_Check *infottl = new ICMP_Inforeq_Ttl_Check;
 
59
 
 
60
        kwd_chk.insert(pair<string, Xprobe_Module_Param_ICMP *>("icmp_info_reply",inforep));
 
61
        kwd_chk.insert(pair<string, Xprobe_Module_Param_ICMP *>("icmp_info_reply_ttl",infottl));
 
62
        kwd_chk.insert(pair<string, Xprobe_Module_Param_ICMP *>("icmp_info_reply_ip_id",infoid));
 
63
}
 
64
 
 
65
ICMP_Inforeq_Mod::~ICMP_Inforeq_Mod(void) {
 
66
 
 
67
        // free allocated classes 
 
68
        for (s_i=kwd_chk.begin(); s_i != kwd_chk.end(); s_i++)
 
69
                delete s_i->second;
 
70
}
 
71
 
 
72
 
 
73
int ICMP_Inforeq_Mod::init(void) {
 
74
 
 
75
    xprobe_debug(XPROBE_DEBUG_MODULES, "%s module initialized\n", get_name());
 
76
    return OK;
 
77
}
 
78
 
 
79
 
 
80
int ICMP_Inforeq_Mod::exec(Target *tg, OS_Matrix *os) {
 
81
    
 
82
    xprobe_debug(XPROBE_DEBUG_MODULES, "--%s module has been executed against: %s\n", get_name(),
 
83
            inet_ntoa(tg->get_addr()));
 
84
 
 
85
    current_os = os;
 
86
    do_icmp_query(tg);
 
87
    
 
88
    return OK;
 
89
}
 
90
 
 
91
int ICMP_Inforeq_Mod::fini(void) {
 
92
    xprobe_debug(XPROBE_DEBUG_MODULES, "%s module has been deinitilized\n", get_name());
 
93
    return OK;
 
94
}
 
95
 
 
96
int ICMP_Inforeq_Mod::parse_keyword(int os_id, const char *kwd, const char *val) {
 
97
 
 
98
    xprobe_debug(XPROBE_DEBUG_SIGNATURES, "Parsing for %i : %s  = %s\n",
 
99
                                                        os_id,  kwd, val);
 
100
        if ((s_i=kwd_chk.find(kwd)) != kwd_chk.end()) {
 
101
                return s_i->second->parse_param(os_id, val);
 
102
        }
 
103
        return OK;
 
104
}
 
105
 
 
106
int ICMP_Inforeq_Mod::do_icmp_query(Target *tg) {
 
107
 
 
108
    char buf[1024];
 
109
    struct timeval tv;
 
110
    int ret;
 
111
    int done;
 
112
    unsigned short int icmpp_id;
 
113
    struct in_addr local, remote;
 
114
 
 
115
/* our lamyer randomizer ;-p */
 
116
    srand(time(NULL));
 
117
    icmpp_id = rand();
 
118
    local = tg->get_interface_addr();
 
119
        remote = tg->get_addr();
 
120
 
 
121
    ICMP icmpp(inet_ntoa(remote));
 
122
    ICMP sn(inet_ntoa(local));
 
123
    sn.init_device(tg->get_interface(), 0, 1500);
 
124
 
 
125
    tv = tg->get_rtt();
 
126
 
 
127
    icmpp.set_src(inet_ntoa(tg->get_interface_addr()));
 
128
    icmpp.set_icmpId(icmpp_id);
 
129
    icmpp.set_type(ICMP_INFO_REQUEST);
 
130
    fflush(stderr);
 
131
    ret = -1;
 
132
    
 
133
    icmpp.timeout(tv);
 
134
    sn.timeout(tv);
 
135
    ret = icmpp.sendpack("");
 
136
    done = 0;
 
137
    while (!done) {
 
138
        ret = sn.sniffpack(buf, sizeof(buf));
 
139
        /* packet response */
 
140
//        if (ret > 0 && sn.get_src() != local.s_addr 
 
141
        if (!sn.timeout() && sn.get_src() == remote.s_addr 
 
142
            && sn.get_type() == ICMP_INFO_REPLY && sn.get_icmpId() == icmpp_id) {
 
143
                        done = 1;
 
144
                        xprobe_debug(XPROBE_DEBUG_MODULES, "[%s] Received reply.\n", get_name());
 
145
                }
 
146
//        if (ret < 1) done = 1; /* timeout */    
 
147
                if (sn.timeout()) {
 
148
                        done = 1;
 
149
                        xprobe_debug(XPROBE_DEBUG_MODULES, "[%s] Timed out, no reply received.\n", get_name());
 
150
                }
 
151
    }
 
152
        for (s_i = kwd_chk.begin(); s_i != kwd_chk.end(); s_i++)
 
153
                s_i->second->check_param(&sn, &icmpp, current_os);
 
154
 
 
155
        if (tg->generate_sig())
 
156
                generate_signature(tg, &sn, &icmpp);
 
157
    return OK;
 
158
 
 
159
}
 
160
 
 
161
void ICMP_Inforeq_Mod::generate_signature(Target *tg, ICMP *pack, ICMP *orig) {
 
162
        string keyword, value;
 
163
        unsigned int ttl;
 
164
/*
 
165
#       icmp_info_reply = [ y, n]
 
166
#       icmp_info_reply_ttl = [>< decimal num] 
 
167
#       icmp_info_reply_ip_id = [0, !0, SENT]
 
168
*/
 
169
        if (!pack->timeout()) {
 
170
                tg->signature("icmp_info_reply", "y");
 
171
                keyword = "icmp_info_reply_ttl";
 
172
                ttl = pack->get_ttl() + tg->get_distance();
 
173
                value = "<";
 
174
                if (ttl <= 32)
 
175
                        value.append("32");
 
176
                else if (ttl <= 64)
 
177
                        value.append("64");
 
178
                else if (ttl <= 128)
 
179
                        value.append("128");
 
180
                else if (ttl <= 255)
 
181
                        value.append("255");
 
182
                tg->signature(keyword, value);
 
183
                keyword = "icmp_info_reply_ip_id";
 
184
                if (pack->get_id() == 0)
 
185
                        value = "0";
 
186
                else if (pack->get_id() == orig->get_id())
 
187
                        value = "SENT";
 
188
                else
 
189
                        value = "!0";
 
190
                tg->signature(keyword, value);
 
191
        } else {
 
192
                tg->signature("icmp_info_reply", "n");
 
193
                tg->signature("icmp_info_reply_ttl", "<255");
 
194
                tg->signature("icmp_info_reply_ip_id", "!0");
 
195
        }
 
196
 
 
197
}
 
198
 
 
199
int ICMP_Inforeq_Reply_Check::check_param(ICMP *ip_pkt, ICMP *orig_pkt, OS_Matrix *os) {
 
200
 
 
201
        int gotp=ip_pkt->timeout() ? 0 : 1;
 
202
        orig_pkt->timeout();    //suspend the warning
 
203
        add_param(gotp, 0, os);
 
204
        if (!gotp) {
 
205
                /* 2 keywords depend on this on
 
206
                 * so to be able to get 100% we
 
207
                 * generate 2 matches here if no
 
208
                 * reply was received
 
209
                 */
 
210
                gen_match(2, os);
 
211
        }
 
212
        return OK;
 
213
}
 
214
 
 
215
int ICMP_Inforeq_Ip_Id_Check::check_param(ICMP *ip_pkt, ICMP *orig_pkt, OS_Matrix *os) {
 
216
 
 
217
        if (!ip_pkt->timeout())
 
218
                return add_param(ip_pkt->get_id(), orig_pkt->get_id(), os);
 
219
        return OK;
 
220
}
 
221
 
 
222
int ICMP_Inforeq_Ttl_Check::check_param(ICMP *ip_pkt, ICMP *orig_pkt, OS_Matrix *os) {
 
223
 
 
224
        if (!ip_pkt->timeout())
 
225
                return add_param(ip_pkt->get_ttl(), orig_pkt->get_ttl(), os);
 
226
        return OK;
 
227
}