| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | # Copyright (C) 2008 Adriano Monteiro Marques |
|---|
| 4 | # |
|---|
| 5 | # Author: Francesco Piccinno <stack.box@gmail.com> |
|---|
| 6 | # |
|---|
| 7 | # This program is free software; you can redistribute it and/or modify |
|---|
| 8 | # it under the terms of the GNU General Public License as published by |
|---|
| 9 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 10 | # (at your option) any later version. |
|---|
| 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 | import gtk |
|---|
| 22 | |
|---|
| 23 | from PM import Backend |
|---|
| 24 | from PM.Core.I18N import _ |
|---|
| 25 | from PM.Core.Atoms import Node |
|---|
| 26 | |
|---|
| 27 | from PM.Gui.Core.App import PMApp |
|---|
| 28 | from PM.Gui.Core.Views import UmitView |
|---|
| 29 | |
|---|
| 30 | from PM.Gui.Sessions.Base import Session |
|---|
| 31 | from PM.Gui.Sessions.SniffSession import SniffSession |
|---|
| 32 | from PM.Gui.Sessions.AttackSession import AttackSession |
|---|
| 33 | from PM.Gui.Sessions.SequenceSession import SequenceSession |
|---|
| 34 | from PM.Gui.Sessions.BtSniffSession import BtSniffSession |
|---|
| 35 | |
|---|
| 36 | from PM.Manager.PreferenceManager import Prefs |
|---|
| 37 | |
|---|
| 38 | import os.path |
|---|
| 39 | from PM.Core.Const import PIXMAPS_DIR |
|---|
| 40 | |
|---|
| 41 | class IntroPage(gtk.HBox): |
|---|
| 42 | def __init__(self): |
|---|
| 43 | gtk.HBox.__init__(self, False, 2) |
|---|
| 44 | |
|---|
| 45 | pix = gtk.gdk.pixbuf_new_from_file(os.path.join(PIXMAPS_DIR, |
|---|
| 46 | 'pm-logo.png')) |
|---|
| 47 | saturate = pix.copy() |
|---|
| 48 | |
|---|
| 49 | pix.saturate_and_pixelate(pix, 0, False) |
|---|
| 50 | saturate.saturate_and_pixelate(saturate, 10, False) |
|---|
| 51 | |
|---|
| 52 | self.pixbufs = (pix, saturate) |
|---|
| 53 | |
|---|
| 54 | self.image = gtk.Image() |
|---|
| 55 | self.image.set_from_pixbuf(self.pixbufs[0]) |
|---|
| 56 | |
|---|
| 57 | ebox = gtk.EventBox() |
|---|
| 58 | ebox.add(self.image) |
|---|
| 59 | |
|---|
| 60 | alignment = gtk.Alignment(0.5, 1.0) |
|---|
| 61 | alignment.add(ebox) |
|---|
| 62 | |
|---|
| 63 | #self.pack_start(gtk.Label('Drop a packet here')) |
|---|
| 64 | self.pack_end(alignment, False, False) |
|---|
| 65 | |
|---|
| 66 | self.set_tooltip_markup('<span size="large"><b>' |
|---|
| 67 | 'Drag and drop a packet here.' |
|---|
| 68 | '</b></span>') |
|---|
| 69 | self.image.set_tooltip_markup('<tt>PM: now nopper addicted.</tt>') |
|---|
| 70 | |
|---|
| 71 | ebox.connect('enter-notify-event', self.__on_enter) |
|---|
| 72 | ebox.connect('leave-notify-event', self.__on_leave) |
|---|
| 73 | ebox.connect('realize', self.__on_realize) |
|---|
| 74 | |
|---|
| 75 | def __on_realize(self, ebox): |
|---|
| 76 | ebox.modify_bg(gtk.STATE_NORMAL, self.style.bg[gtk.STATE_NORMAL]) |
|---|
| 77 | |
|---|
| 78 | def __on_enter(self, widget, evt): |
|---|
| 79 | self.image.set_from_pixbuf(self.pixbufs[1]) |
|---|
| 80 | |
|---|
| 81 | def __on_leave(self, widget, evt): |
|---|
| 82 | self.image.set_from_pixbuf(self.pixbufs[0]) |
|---|
| 83 | |
|---|
| 84 | class SessionNotebook(gtk.Notebook): |
|---|
| 85 | def __init__(self): |
|---|
| 86 | gtk.Notebook.__init__(self) |
|---|
| 87 | |
|---|
| 88 | self.set_show_border(False) |
|---|
| 89 | self.set_scrollable(True) |
|---|
| 90 | self.set_show_tabs(False) |
|---|
| 91 | |
|---|
| 92 | self._intro_page = IntroPage() |
|---|
| 93 | self.append_page(self._intro_page) |
|---|
| 94 | |
|---|
| 95 | # We have a static page to manage the packets |
|---|
| 96 | # selected from sniff perspective |
|---|
| 97 | self.view_page = None |
|---|
| 98 | |
|---|
| 99 | self.connect('page-added', self.__check_last_page) |
|---|
| 100 | self.connect('page-removed', self.__check_last_page) |
|---|
| 101 | |
|---|
| 102 | def create_sequence_session(self, packets): |
|---|
| 103 | """ |
|---|
| 104 | Create a sequence from packets |
|---|
| 105 | |
|---|
| 106 | @param packets a Node object or a list of packets or a single packet |
|---|
| 107 | """ |
|---|
| 108 | |
|---|
| 109 | if isinstance(packets, Node): |
|---|
| 110 | ctx = Backend.SequenceContext(packets) |
|---|
| 111 | else: |
|---|
| 112 | seq = Node() |
|---|
| 113 | |
|---|
| 114 | for packet in packets: |
|---|
| 115 | seq.append_node(Node(Backend.SequencePacket(packet))) |
|---|
| 116 | |
|---|
| 117 | ctx = Backend.SequenceContext(seq) |
|---|
| 118 | |
|---|
| 119 | session = SequenceSession(ctx) |
|---|
| 120 | return self.__append_session(session) |
|---|
| 121 | |
|---|
| 122 | def create_edit_session(self, packet): |
|---|
| 123 | if isinstance(packet, basestring): |
|---|
| 124 | packet = Backend.get_proto(packet)() |
|---|
| 125 | packet = Backend.MetaPacket(packet) |
|---|
| 126 | |
|---|
| 127 | return self.create_sequence_session([packet]) |
|---|
| 128 | |
|---|
| 129 | def create_sniff_session(self, ctx): |
|---|
| 130 | session = SniffSession(ctx) |
|---|
| 131 | return self.__append_session(session) |
|---|
| 132 | |
|---|
| 133 | def create_btsniff_session(self, ctx): |
|---|
| 134 | session = BtSniffSession(ctx) |
|---|
| 135 | return self.__append_session(session) |
|---|
| 136 | |
|---|
| 137 | def create_attack_session(self, ctx): |
|---|
| 138 | session = AttackSession(ctx) |
|---|
| 139 | return self.__append_session(session) |
|---|
| 140 | |
|---|
| 141 | def load_static_session(self, fname): |
|---|
| 142 | ctx = Backend.StaticContext(fname, fname) |
|---|
| 143 | ctx.load() |
|---|
| 144 | |
|---|
| 145 | session = SniffSession(ctx) |
|---|
| 146 | return self.__append_session(session) |
|---|
| 147 | |
|---|
| 148 | def load_sniff_session(self, fname): |
|---|
| 149 | return self.load_static_session(fname) |
|---|
| 150 | |
|---|
| 151 | def load_sequence_session(self, fname): |
|---|
| 152 | ctx = Backend.SequenceContext(fname) |
|---|
| 153 | ctx.load() |
|---|
| 154 | |
|---|
| 155 | session = SequenceSession(ctx) |
|---|
| 156 | return self.__append_session(session) |
|---|
| 157 | |
|---|
| 158 | def create_empty_session(self, title): |
|---|
| 159 | session = SniffSession(title=title) |
|---|
| 160 | return self.__append_session(session) |
|---|
| 161 | |
|---|
| 162 | def bind_session(self, sessk, ctx): |
|---|
| 163 | session = sessk(ctx) |
|---|
| 164 | return self.__append_session(session) |
|---|
| 165 | |
|---|
| 166 | def create_session(self, sessk, ctxk): |
|---|
| 167 | return self.bind_session(sessk, ctxk()) |
|---|
| 168 | |
|---|
| 169 | def __append_session(self, session): |
|---|
| 170 | session.label.connect('close-clicked', self.__on_close_page, session) |
|---|
| 171 | |
|---|
| 172 | self.append_page(session, session.label) |
|---|
| 173 | self.set_tab_reorderable(session, True) |
|---|
| 174 | |
|---|
| 175 | return session |
|---|
| 176 | |
|---|
| 177 | def __remove_session(self, session): |
|---|
| 178 | tab = PMApp().main_window.get_tab("OperationsTab") |
|---|
| 179 | |
|---|
| 180 | tab.tree.remove_operation(session.context) |
|---|
| 181 | |
|---|
| 182 | idx = self.page_num(session) |
|---|
| 183 | self.remove_page(idx) |
|---|
| 184 | |
|---|
| 185 | def get_current_session(self): |
|---|
| 186 | """ |
|---|
| 187 | Get the current Session |
|---|
| 188 | |
|---|
| 189 | @return a Session instance or None |
|---|
| 190 | """ |
|---|
| 191 | |
|---|
| 192 | idx = self.get_current_page() |
|---|
| 193 | obj = self.get_nth_page(idx) |
|---|
| 194 | |
|---|
| 195 | if obj and isinstance(obj, Session): |
|---|
| 196 | return obj |
|---|
| 197 | |
|---|
| 198 | return None |
|---|
| 199 | |
|---|
| 200 | def __check_last_page(self, widget, child, pagenum): |
|---|
| 201 | if self.get_n_pages() == 1: |
|---|
| 202 | self.set_show_tabs(False) |
|---|
| 203 | self._intro_page.show() |
|---|
| 204 | else: |
|---|
| 205 | self._intro_page.hide() |
|---|
| 206 | self.set_show_tabs(True) |
|---|
| 207 | |
|---|
| 208 | def __on_close_page(self, label, session): |
|---|
| 209 | # Check if are stopped |
|---|
| 210 | |
|---|
| 211 | if isinstance(session.context, Backend.TimedContext) and \ |
|---|
| 212 | session.context.state != session.context.NOT_RUNNING: |
|---|
| 213 | |
|---|
| 214 | if Prefs()['gui.maintab.autostop'].value == True: |
|---|
| 215 | session.context.stop() |
|---|
| 216 | else: |
|---|
| 217 | dialog = gtk.MessageDialog(self.get_toplevel(), |
|---|
| 218 | gtk.DIALOG_DESTROY_WITH_PARENT, |
|---|
| 219 | gtk.MESSAGE_QUESTION, |
|---|
| 220 | gtk.BUTTONS_YES_NO, |
|---|
| 221 | _('The session is running.\n' |
|---|
| 222 | 'Do you want stop it?')) |
|---|
| 223 | id = dialog.run() |
|---|
| 224 | |
|---|
| 225 | dialog.hide() |
|---|
| 226 | dialog.destroy() |
|---|
| 227 | |
|---|
| 228 | if id == gtk.RESPONSE_YES: |
|---|
| 229 | session.context.stop() |
|---|
| 230 | |
|---|
| 231 | return |
|---|
| 232 | |
|---|
| 233 | if session.context.status == session.context.SAVED or \ |
|---|
| 234 | Prefs()['gui.maintab.askforsave'].value == False: |
|---|
| 235 | self.__remove_session(session) |
|---|
| 236 | else: |
|---|
| 237 | dialog = gtk.MessageDialog(self.get_toplevel(), |
|---|
| 238 | gtk.DIALOG_DESTROY_WITH_PARENT, |
|---|
| 239 | gtk.MESSAGE_QUESTION, |
|---|
| 240 | gtk.BUTTONS_YES_NO, |
|---|
| 241 | _('The session has unsaved changes.\n' |
|---|
| 242 | 'Do you want to save them?')) |
|---|
| 243 | |
|---|
| 244 | id = dialog.run() |
|---|
| 245 | |
|---|
| 246 | dialog.hide() |
|---|
| 247 | dialog.destroy() |
|---|
| 248 | |
|---|
| 249 | if id == gtk.RESPONSE_NO or \ |
|---|
| 250 | (id == gtk.RESPONSE_YES and session.save()): |
|---|
| 251 | |
|---|
| 252 | self.__remove_session(session) |
|---|
| 253 | |
|---|
| 254 | class MainTab(UmitView): |
|---|
| 255 | tab_position = None |
|---|
| 256 | name = 'MainTab' |
|---|
| 257 | |
|---|
| 258 | def __create_widgets(self): |
|---|
| 259 | "Create the widgets" |
|---|
| 260 | self.vbox = gtk.VBox(False, 2) |
|---|
| 261 | self.session_notebook = SessionNotebook() |
|---|
| 262 | |
|---|
| 263 | def __pack_widgets(self): |
|---|
| 264 | "Pack the widgets" |
|---|
| 265 | |
|---|
| 266 | self.vbox.pack_start(self.session_notebook) |
|---|
| 267 | |
|---|
| 268 | self.session_notebook.drag_dest_set( |
|---|
| 269 | gtk.DEST_DEFAULT_ALL, |
|---|
| 270 | [('text/plain', 0, 0)], |
|---|
| 271 | gtk.gdk.ACTION_COPY |
|---|
| 272 | ) |
|---|
| 273 | |
|---|
| 274 | self._main_widget.add(self.vbox) |
|---|
| 275 | self._main_widget.show_all() |
|---|
| 276 | |
|---|
| 277 | def __connect_signals(self): |
|---|
| 278 | self.session_notebook.connect('drag-data-received', self.__on_drag_data) |
|---|
| 279 | |
|---|
| 280 | def create_ui(self): |
|---|
| 281 | "Create the ui" |
|---|
| 282 | self.__create_widgets() |
|---|
| 283 | self.__pack_widgets() |
|---|
| 284 | self.__connect_signals() |
|---|
| 285 | |
|---|
| 286 | def get_current_session(self): |
|---|
| 287 | "@returns the current Session or None" |
|---|
| 288 | page = self.get_current_page() |
|---|
| 289 | |
|---|
| 290 | if page and isinstance(page, Session): |
|---|
| 291 | return page |
|---|
| 292 | return None |
|---|
| 293 | |
|---|
| 294 | def get_current_page(self): |
|---|
| 295 | "@return the current page in notebook or None" |
|---|
| 296 | |
|---|
| 297 | idx = self.session_notebook.get_current_page() |
|---|
| 298 | return self.session_notebook.get_nth_page(idx) |
|---|
| 299 | |
|---|
| 300 | def __on_drag_data(self, widget, ctx, x, y, data, info, time): |
|---|
| 301 | "drag-data-received callback" |
|---|
| 302 | |
|---|
| 303 | if data and data.format == 8: |
|---|
| 304 | proto = data.data |
|---|
| 305 | |
|---|
| 306 | if Backend.get_proto(proto): |
|---|
| 307 | self.session_notebook.create_edit_session(data.data) |
|---|
| 308 | ctx.finish(True, False, time) |
|---|
| 309 | return True |
|---|
| 310 | |
|---|
| 311 | ctx.finish(False, False, time) |
|---|