def makeDialog(self, event): cmd = event.getActionCommand() isModal = (cmd == 'Modal') dialog = JDialog( self.frame, # Try None as owner... # None, # owner title='%d: %s' % (self.boxNum, cmd), modal=isModal, size=(200, 100), locationRelativeTo=self.frame) self.boxNum += 1 dialog.add(JLabel(cmd)) dialog.setVisible(1)
def valueChanged(self, event): if not event.getValueIsAdjusting(): table = self.table row = table.getSelectedRow() if row > -1: method = table.getModel().getValueAt(row, 0) title = '%s.help( "%s" )' % (self.objName, method) text = self.WASobj.help(method) text = JTextArea(text, 20, 80, font=monoFont) dialog = JDialog( None, # owner title, # title 1, # modal = true layout=BorderLayout(), locationRelativeTo=None) dialog.add(JScrollPane(text), BorderLayout.CENTER) dialog.pack() dialog.setVisible(1)
class AutopsyImageClassificationModuleWithUISettingsPanel( IngestModuleIngestJobSettingsPanel): _logger = Logger.getLogger( AutopsyImageClassificationModuleFactory.moduleName) def log(self, level, msg): self._logger.logp(level, self.__class__.__name__, inspect.stack()[1][3], msg) def __init__(self, settings): self.local_settings = settings self.config_location = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'configs.json') self.init_components() self.customize_components() self.check_server_connection(None) self.classes_of_interest_changes_list = list() self.classes_of_interest_checkboxes = list() # Return the settings used def getSettings(self): if not os.path.isfile(self.config_location): self.log( Level.INFO, "Configuration file not found, loading the default configuration" ) self.local_settings.setServerHost(DEFAULT_HOST) self.local_settings.setServerPort(DEFAULT_PORT) self.local_settings.setImageFormats(DEFAULT_IMAGES_FORMAT) self.local_settings.setMinFileSize(DEFAULT_MIN_FILE_SIZE) self.local_settings.setMinProbability(DEFAULT_MIN_PROBABILITY) self.local_settings.setClassesOfInterest( json.loads(DEFAULT_CLASSES_OF_INTEREST)) # self.saveSettings(None) return self.local_settings else: if not os.access(self.config_location, os.R_OK): err_string = "Cannot access configuration file, please review the file permissions" raise IngestModuleException(err_string) with io.open(self.config_location, 'r', encoding='utf-8') as f: self.log(Level.INFO, "Configuration file read") json_configs = json.load(f) self.local_settings.setServerHost(json_configs['server']['host']) self.local_settings.setServerPort(json_configs['server']['port']) image_formats = json_configs['imageFormats'] if not isinstance(image_formats, list) or len(image_formats) == 0: err_string = "Invalid list of image formats given" raise IngestModuleException(err_string) self.local_settings.setImageFormats(image_formats) self.local_settings.setMinFileSize(json_configs['minFileSize']) self.local_settings.setMinProbability( json_configs['minProbability']) self.local_settings.setClassesOfInterest( json_configs['classesOfInterest']) return self.local_settings def check_server_connection(self, e): message_string = "Testing connection with server..." self.log(Level.INFO, message_string) self.message.setText(message_string) self.error_message.setText("") new_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) new_socket.settimeout(1) try: new_socket.connect( (self.host_TF.getText(), int(self.port_TF.getText()))) self.local_settings.setIsServerOnline(True) message_string = "Server is up!" self.log(Level.INFO, message_string) self.message.setText(message_string) except (socket.timeout, socket.error) as e: self.local_settings.setIsServerOnline(False) err_string = "Server is down" self.error_message.setText(err_string) self.message.setText("") self.log(Level.INFO, err_string) finally: new_socket.close() def save_settings(self, e): self.message.setText("") self.log(Level.INFO, "Settings save button clicked!") host = self.host_TF.getText() if not host.strip(): err_string = "Invalid host" self.error_message.setText(err_string) return # raise IngestModuleException(err_string) port = self.port_TF.getText() if not host.strip(): err_string = "Invalid port number" self.error_message.setText(err_string) return # raise IngestModuleException(err_string) image_formats_array = self.image_formats_TF.getText().strip().split( ';') if len(image_formats_array) == 0 or not image_formats_array[0]: err_string = "Invalid image formats" self.error_message.setText(err_string) return # raise IngestModuleException(err_string) min_probability_string = self.min_probability_TF.getText().strip() if not min_probability_string: err_string = "Invalid minimum confidence value" self.error_message.setText(err_string) return # raise IngestModuleException(err_string) min_probability = int(float(min_probability_string)) min_file_size_string = self.min_file_size_TF.getText().strip() if not min_file_size_string: err_string = "Invalid minimum file size" self.error_message.setText(err_string) return # raise IngestModuleException(err_string) min_file_size = int(float(min_file_size_string)) configs = { 'server': { 'host': host, 'port': port }, 'imageFormats': image_formats_array, 'minProbability': min_probability, 'minFileSize': min_file_size, 'classesOfInterest': self.local_settings.getClassesOfInterest() } with io.open(self.config_location, 'w', encoding='utf-8') as f: f.write(json.dumps(configs, ensure_ascii=False)) self.error_message.setText("") message = "Settings saved " self.message.setText(message) self.log(Level.INFO, message + " in " + self.config_location) def open_text_editor(self, e): self.log(Level.INFO, "Lauching external text editor ") if sys.platform == "win32": subprocess.call(["notepad", self.config_location]) else: opener = "open" if sys.platform == "darwin" else "xdg-open" subprocess.call([opener, self.config_location]) def on_save_classes_of_interest_click(self, e): self.detectable_obejcts_dialog.setVisible(False) for item in self.classes_of_interest_changes_list: for class_oi in self.local_settings.getClassesOfInterest(): if item.getText() == class_oi['name']: self.log( Level.INFO, "match found " + class_oi['name'] + " and is selected " + str(item.isSelected())) class_oi['enabled'] = item.isSelected() break self.classes_of_interest_checkboxes = list() def on_cancel_classes_of_interest_click(self, e): self.detectable_obejcts_dialog.setVisible(False) self.classes_of_interest_changes_list = list() self.classes_of_interest_checkboxes = list() def on_class_checkbox_clicked(self, e): self.classes_of_interest_changes_list.append(e.getSource()) def on_select_all_clicked(self, e): for checkbox in self.classes_of_interest_checkboxes: checkbox.setSelected(True) def on_deselect_all_clicked(self, e): for checkbox in self.classes_of_interest_checkboxes: checkbox.setSelected(False) def show_detectable_objects_dialog(self, e): parentComponent = SwingUtilities.windowForComponent(self.panel0) self.detectable_obejcts_dialog = JDialog( parentComponent, "List of Objects to Detect", ModalityType.APPLICATION_MODAL) panel = JPanel() self.detectable_obejcts_dialog.add(panel) gbPanel = GridBagLayout() gbcPanel = GridBagConstraints() panel.setLayout(gbPanel) y = 0 x = 0 for line in self.local_settings.getClassesOfInterest(): if y > 15: y = 0 x = x + 1 class_check_box = JCheckBox(line['name']) self.classes_of_interest_checkboxes.append(class_check_box) class_check_box.setEnabled(True) class_check_box.setSelected(line['enabled']) class_check_box.addItemListener(self.on_class_checkbox_clicked) gbcPanel.gridx = x gbcPanel.gridy = y gbcPanel.gridwidth = 1 gbcPanel.gridheight = 1 gbcPanel.fill = GridBagConstraints.BOTH gbcPanel.weightx = 1 gbcPanel.weighty = 1 gbcPanel.anchor = GridBagConstraints.NORTH gbPanel.setConstraints(class_check_box, gbcPanel) panel.add(class_check_box) y = y + 1 blank_1_L = JLabel(" ") blank_1_L.setEnabled(True) gbcPanel.gridx = 0 gbcPanel.gridy = y + 1 gbcPanel.gridwidth = 1 gbcPanel.gridheight = 1 gbcPanel.fill = GridBagConstraints.BOTH gbcPanel.weightx = 1 gbcPanel.weighty = 0 gbcPanel.anchor = GridBagConstraints.NORTH gbPanel.setConstraints(blank_1_L, gbcPanel) panel.add(blank_1_L) deselect_all_button = JButton("Deselect all") deselect_all_button.setEnabled(True) deselect_all_button.addActionListener(self.on_deselect_all_clicked) gbcPanel.gridx = 1 gbcPanel.gridy = y + 2 gbcPanel.gridwidth = 1 gbcPanel.gridheight = 1 gbcPanel.fill = GridBagConstraints.BOTH gbcPanel.weightx = 2 gbcPanel.weighty = 1 gbcPanel.anchor = GridBagConstraints.NORTH gbPanel.setConstraints(deselect_all_button, gbcPanel) panel.add(deselect_all_button) select_all_button = JButton("Select all") select_all_button.setEnabled(True) select_all_button.addActionListener(self.on_select_all_clicked) gbcPanel.gridx = 3 gbcPanel.gridy = y + 2 gbcPanel.gridwidth = 1 gbcPanel.gridheight = 1 gbcPanel.fill = GridBagConstraints.BOTH gbcPanel.weightx = 2 gbcPanel.weighty = 1 gbcPanel.anchor = GridBagConstraints.NORTH gbPanel.setConstraints(select_all_button, gbcPanel) panel.add(select_all_button) blank_2_L = JLabel(" ") blank_2_L.setEnabled(True) gbcPanel.gridx = 0 gbcPanel.gridy = y + 3 gbcPanel.gridwidth = 1 gbcPanel.gridheight = 1 gbcPanel.fill = GridBagConstraints.BOTH gbcPanel.weightx = 1 gbcPanel.weighty = 0 gbcPanel.anchor = GridBagConstraints.NORTH gbPanel.setConstraints(blank_2_L, gbcPanel) panel.add(blank_2_L) cancel_button = JButton("Cancel") cancel_button.setEnabled(True) cancel_button.addActionListener( self.on_cancel_classes_of_interest_click) gbcPanel.gridx = 1 gbcPanel.gridy = y + 4 gbcPanel.gridwidth = 1 gbcPanel.gridheight = 1 gbcPanel.fill = GridBagConstraints.BOTH gbcPanel.weightx = 2 gbcPanel.weighty = 1 gbcPanel.anchor = GridBagConstraints.NORTH gbPanel.setConstraints(cancel_button, gbcPanel) panel.add(cancel_button) save_button = JButton("Save") save_button.setEnabled(True) save_button.addActionListener(self.on_save_classes_of_interest_click) gbcPanel.gridx = 3 gbcPanel.gridy = y + 4 gbcPanel.gridwidth = 1 gbcPanel.gridheight = 1 gbcPanel.fill = GridBagConstraints.BOTH gbcPanel.weightx = 2 gbcPanel.weighty = 1 gbcPanel.anchor = GridBagConstraints.NORTH gbPanel.setConstraints(save_button, gbcPanel) panel.add(save_button) blank_3_L = JLabel(" ") blank_3_L.setEnabled(True) gbcPanel.gridx = 0 gbcPanel.gridy = y + 5 gbcPanel.gridwidth = 1 gbcPanel.gridheight = 1 gbcPanel.fill = GridBagConstraints.BOTH gbcPanel.weightx = 1 gbcPanel.weighty = 0 gbcPanel.anchor = GridBagConstraints.NORTH gbPanel.setConstraints(blank_3_L, gbcPanel) panel.add(blank_3_L) self.detectable_obejcts_dialog.pack() screenSize = Toolkit.getDefaultToolkit().getScreenSize() self.detectable_obejcts_dialog.setLocation( int((screenSize.getWidth() / 2) - (self.detectable_obejcts_dialog.getWidth() / 2)), int((screenSize.getHeight() / 2) - (self.detectable_obejcts_dialog.getHeight() / 2))) self.detectable_obejcts_dialog.setVisible(True) def customize_components(self): settings = self.getSettings() self.host_TF.setText(settings.getServerHost()) self.port_TF.setText(str(settings.getServerPort())) self.log(Level.INFO, "[customizeComponents]") self.log(Level.INFO, settings.getImageFormats()[0]) self.image_formats_TF.setText(';'.join(settings.getImageFormats())) self.min_probability_TF.setText(str(settings.getMinProbability())) self.min_file_size_TF.setText(str(settings.getMinFileSize())) def init_components(self): self.panel0 = JPanel() self.rbgPanel0 = ButtonGroup() self.gbPanel0 = GridBagLayout() self.gbcPanel0 = GridBagConstraints() self.panel0.setLayout(self.gbPanel0) self.host_L = JLabel("Host:") self.host_L.setEnabled(True) self.gbcPanel0.gridx = 0 self.gbcPanel0.gridy = 1 self.gbcPanel0.gridwidth = 1 self.gbcPanel0.gridheight = 1 self.gbcPanel0.fill = GridBagConstraints.BOTH self.gbcPanel0.weightx = 1 self.gbcPanel0.weighty = 0 self.gbcPanel0.anchor = GridBagConstraints.NORTH self.gbPanel0.setConstraints(self.host_L, self.gbcPanel0) self.panel0.add(self.host_L) self.port_L = JLabel("Port:") self.port_L.setEnabled(True) self.gbcPanel0.gridx = 1 self.gbcPanel0.gridy = 1 self.gbcPanel0.gridwidth = 2 self.gbcPanel0.gridheight = 1 self.gbcPanel0.fill = GridBagConstraints.BOTH self.gbcPanel0.weightx = 1 self.gbcPanel0.weighty = 0 self.gbcPanel0.anchor = GridBagConstraints.NORTH self.gbPanel0.setConstraints(self.port_L, self.gbcPanel0) self.panel0.add(self.port_L) self.host_TF = JTextField(10) self.gbcPanel0.gridx = 0 self.gbcPanel0.gridy = 2 self.gbcPanel0.gridwidth = 1 self.gbcPanel0.gridheight = 1 self.gbcPanel0.fill = GridBagConstraints.BOTH self.gbcPanel0.weightx = 1 self.gbcPanel0.weighty = 0 self.gbcPanel0.anchor = GridBagConstraints.NORTH self.gbPanel0.setConstraints(self.host_TF, self.gbcPanel0) self.panel0.add(self.host_TF) format = NumberFormat.getInstance() format.setGroupingUsed(False) port_formatter = NumberFormatter(format) port_formatter.setValueClass(Integer) port_formatter.setAllowsInvalid(False) port_formatter.setMinimum(Integer(0)) port_formatter.setMaximum(Integer(65535)) self.port_TF = JFormattedTextField(port_formatter) self.gbcPanel0.gridx = 1 self.gbcPanel0.gridy = 2 self.gbcPanel0.gridwidth = 1 self.gbcPanel0.gridheight = 1 self.gbcPanel0.fill = GridBagConstraints.BOTH self.gbcPanel0.weightx = 1 self.gbcPanel0.weighty = 0 self.gbcPanel0.anchor = GridBagConstraints.NORTH self.gbPanel0.setConstraints(self.port_TF, self.gbcPanel0) self.panel0.add(self.port_TF) self.blank_1_L = JLabel(" ") self.blank_1_L.setEnabled(True) self.gbcPanel0.gridx = 2 self.gbcPanel0.gridy = 3 self.gbcPanel0.gridwidth = 1 self.gbcPanel0.gridheight = 1 self.gbcPanel0.fill = GridBagConstraints.BOTH self.gbcPanel0.weightx = 1 self.gbcPanel0.weighty = 0 self.gbcPanel0.anchor = GridBagConstraints.NORTH self.gbPanel0.setConstraints(self.blank_1_L, self.gbcPanel0) self.panel0.add(self.blank_1_L) self.image_formats_L = JLabel("Format of images (separator \";\"):") self.port_L.setEnabled(True) self.gbcPanel0.gridx = 0 self.gbcPanel0.gridy = 4 self.gbcPanel0.gridwidth = 1 self.gbcPanel0.gridheight = 1 self.gbcPanel0.fill = GridBagConstraints.BOTH self.gbcPanel0.weightx = 1 self.gbcPanel0.weighty = 0 self.gbcPanel0.anchor = GridBagConstraints.NORTH self.gbPanel0.setConstraints(self.image_formats_L, self.gbcPanel0) self.panel0.add(self.image_formats_L) self.image_formats_TF = JTextField(10) self.gbcPanel0.gridx = 0 self.gbcPanel0.gridy = 5 self.gbcPanel0.gridwidth = 1 self.gbcPanel0.gridheight = 1 self.gbcPanel0.fill = GridBagConstraints.BOTH self.gbcPanel0.weightx = 1 self.gbcPanel0.weighty = 0 self.gbcPanel0.anchor = GridBagConstraints.NORTH self.gbPanel0.setConstraints(self.image_formats_TF, self.gbcPanel0) self.panel0.add(self.image_formats_TF) self.blank_2_L = JLabel(" ") self.blank_2_L.setEnabled(True) self.gbcPanel0.gridx = 2 self.gbcPanel0.gridy = 6 self.gbcPanel0.gridwidth = 1 self.gbcPanel0.gridheight = 1 self.gbcPanel0.fill = GridBagConstraints.BOTH self.gbcPanel0.weightx = 1 self.gbcPanel0.weighty = 0 self.gbcPanel0.anchor = GridBagConstraints.NORTH self.gbPanel0.setConstraints(self.blank_2_L, self.gbcPanel0) self.panel0.add(self.blank_2_L) self.min_probability_L = JLabel("Confidence minimum (%):") self.port_L.setEnabled(True) self.gbcPanel0.gridx = 0 self.gbcPanel0.gridy = 7 self.gbcPanel0.gridwidth = 1 self.gbcPanel0.gridheight = 1 self.gbcPanel0.fill = GridBagConstraints.BOTH self.gbcPanel0.weightx = 1 self.gbcPanel0.weighty = 0 self.gbcPanel0.anchor = GridBagConstraints.NORTH self.gbPanel0.setConstraints(self.min_probability_L, self.gbcPanel0) self.panel0.add(self.min_probability_L) min_probabilty_formatter = NumberFormatter(format) min_probabilty_formatter.setValueClass(Integer) min_probabilty_formatter.setAllowsInvalid(False) min_probabilty_formatter.setMinimum(Integer(0)) min_probabilty_formatter.setMaximum(Integer(100)) self.min_probability_TF = JFormattedTextField(min_probabilty_formatter) self.gbcPanel0.gridx = 0 self.gbcPanel0.gridy = 8 self.gbcPanel0.gridwidth = 1 self.gbcPanel0.gridheight = 1 self.gbcPanel0.fill = GridBagConstraints.BOTH self.gbcPanel0.weightx = 1 self.gbcPanel0.weighty = 0 self.gbcPanel0.anchor = GridBagConstraints.NORTH self.gbPanel0.setConstraints(self.min_probability_TF, self.gbcPanel0) self.panel0.add(self.min_probability_TF) self.blank_3_L = JLabel(" ") self.blank_3_L.setEnabled(True) self.gbcPanel0.gridx = 2 self.gbcPanel0.gridy = 9 self.gbcPanel0.gridwidth = 1 self.gbcPanel0.gridheight = 1 self.gbcPanel0.fill = GridBagConstraints.BOTH self.gbcPanel0.weightx = 1 self.gbcPanel0.weighty = 0 self.gbcPanel0.anchor = GridBagConstraints.NORTH self.gbPanel0.setConstraints(self.blank_3_L, self.gbcPanel0) self.panel0.add(self.blank_3_L) self.min_file_size_L = JLabel("Minimum file size (KB):") self.min_file_size_L.setEnabled(True) self.gbcPanel0.gridx = 0 self.gbcPanel0.gridy = 10 self.gbcPanel0.gridwidth = 1 self.gbcPanel0.gridheight = 1 self.gbcPanel0.fill = GridBagConstraints.BOTH self.gbcPanel0.weightx = 1 self.gbcPanel0.weighty = 0 self.gbcPanel0.anchor = GridBagConstraints.NORTH self.gbPanel0.setConstraints(self.min_file_size_L, self.gbcPanel0) self.panel0.add(self.min_file_size_L) min_file_size_formatter = NumberFormatter(format) min_file_size_formatter.setValueClass(Integer) min_file_size_formatter.setAllowsInvalid(False) min_file_size_formatter.setMinimum(Integer(0)) self.min_file_size_TF = JFormattedTextField(min_file_size_formatter) self.gbcPanel0.gridx = 0 self.gbcPanel0.gridy = 11 self.gbcPanel0.gridwidth = 1 self.gbcPanel0.gridheight = 1 self.gbcPanel0.fill = GridBagConstraints.BOTH self.gbcPanel0.weightx = 1 self.gbcPanel0.weighty = 0 self.gbcPanel0.anchor = GridBagConstraints.NORTH self.gbPanel0.setConstraints(self.min_file_size_TF, self.gbcPanel0) self.panel0.add(self.min_file_size_TF) self.blank_4_L = JLabel(" ") self.blank_4_L.setEnabled(True) self.gbcPanel0.gridx = 2 self.gbcPanel0.gridy = 12 self.gbcPanel0.gridwidth = 1 self.gbcPanel0.gridheight = 1 self.gbcPanel0.fill = GridBagConstraints.BOTH self.gbcPanel0.weightx = 1 self.gbcPanel0.weighty = 0 self.gbcPanel0.anchor = GridBagConstraints.NORTH self.gbPanel0.setConstraints(self.blank_4_L, self.gbcPanel0) self.panel0.add(self.blank_4_L) self.detectable_obejcts_BTN = \ JButton("List of Objects to Detect", actionPerformed=self.show_detectable_objects_dialog) # self.save_Settings_BTN.setPreferredSize(Dimension(1, 20)) self.rbgPanel0.add(self.detectable_obejcts_BTN) self.gbcPanel0.gridx = 0 self.gbcPanel0.gridy = 13 self.gbPanel0.setConstraints(self.detectable_obejcts_BTN, self.gbcPanel0) self.panel0.add(self.detectable_obejcts_BTN) self.blank_5_L = JLabel(" ") self.blank_5_L.setEnabled(True) self.gbcPanel0.gridx = 2 self.gbcPanel0.gridy = 14 self.gbcPanel0.gridwidth = 1 self.gbcPanel0.gridheight = 1 self.gbcPanel0.fill = GridBagConstraints.BOTH self.gbcPanel0.weightx = 1 self.gbcPanel0.weighty = 0 self.gbcPanel0.anchor = GridBagConstraints.NORTH self.gbPanel0.setConstraints(self.blank_5_L, self.gbcPanel0) self.panel0.add(self.blank_5_L) self.error_message = JLabel("", JLabel.CENTER) self.error_message.setForeground(Color.red) self.error_message.setEnabled(True) self.gbcPanel0.gridx = 0 self.gbcPanel0.gridy = 15 self.gbcPanel0.gridwidth = 1 self.gbcPanel0.gridheight = 1 self.gbcPanel0.fill = GridBagConstraints.BOTH self.gbcPanel0.weightx = 1 self.gbcPanel0.weighty = 0 self.gbcPanel0.anchor = GridBagConstraints.NORTH self.gbPanel0.setConstraints(self.error_message, self.gbcPanel0) self.panel0.add(self.error_message) self.message = JLabel("", JLabel.CENTER) self.message.setEnabled(True) self.gbcPanel0.gridx = 0 self.gbcPanel0.gridy = 16 self.gbcPanel0.gridwidth = 1 self.gbcPanel0.gridheight = 1 self.gbcPanel0.fill = GridBagConstraints.BOTH self.gbcPanel0.weightx = 1 self.gbcPanel0.weighty = 0 self.gbcPanel0.anchor = GridBagConstraints.NORTH self.gbPanel0.setConstraints(self.message, self.gbcPanel0) self.panel0.add(self.message) self.save_settings_BTN = JButton("Save Settings", actionPerformed=self.save_settings) # self.save_Settings_BTN.setPreferredSize(Dimension(1, 20)) self.rbgPanel0.add(self.save_settings_BTN) self.gbcPanel0.gridx = 0 self.gbcPanel0.gridy = 17 self.gbPanel0.setConstraints(self.save_settings_BTN, self.gbcPanel0) self.panel0.add(self.save_settings_BTN) self.blank_6_L = JLabel(" ") self.blank_6_L.setEnabled(True) self.gbcPanel0.gridx = 2 self.gbcPanel0.gridy = 18 self.gbcPanel0.gridwidth = 1 self.gbcPanel0.gridheight = 1 self.gbcPanel0.fill = GridBagConstraints.BOTH self.gbcPanel0.weightx = 1 self.gbcPanel0.weighty = 0 self.gbcPanel0.anchor = GridBagConstraints.NORTH self.gbPanel0.setConstraints(self.blank_6_L, self.gbcPanel0) self.panel0.add(self.blank_6_L) self.blank_7_L = JLabel(" ") self.blank_7_L.setEnabled(True) self.gbcPanel0.gridx = 2 self.gbcPanel0.gridy = 19 self.gbcPanel0.gridwidth = 1 self.gbcPanel0.gridheight = 1 self.gbcPanel0.fill = GridBagConstraints.BOTH self.gbcPanel0.weightx = 1 self.gbcPanel0.weighty = 0 self.gbcPanel0.anchor = GridBagConstraints.NORTH self.gbPanel0.setConstraints(self.blank_7_L, self.gbcPanel0) self.panel0.add(self.blank_7_L) self.check_server_connection_BTN = \ JButton("Check server connection", actionPerformed=self.check_server_connection) # self.save_Settings_BTN.setPreferredSize(Dimension(1, 20)) self.rbgPanel0.add(self.check_server_connection_BTN) self.gbcPanel0.gridx = 0 self.gbcPanel0.gridy = 20 self.gbPanel0.setConstraints(self.check_server_connection_BTN, self.gbcPanel0) self.panel0.add(self.check_server_connection_BTN) self.blank_8_L = JLabel(" ") self.blank_8_L.setEnabled(True) self.gbcPanel0.gridx = 2 self.gbcPanel0.gridy = 21 self.gbcPanel0.gridwidth = 1 self.gbcPanel0.gridheight = 1 self.gbcPanel0.fill = GridBagConstraints.BOTH self.gbcPanel0.weightx = 1 self.gbcPanel0.weighty = 0 self.gbcPanel0.anchor = GridBagConstraints.NORTH self.gbPanel0.setConstraints(self.blank_8_L, self.gbcPanel0) self.panel0.add(self.blank_8_L) self.text_editor_BTN = \ JButton("Open config file", actionPerformed=self.open_text_editor) # self.save_Settings_BTN.setPreferredSize(Dimension(1, 20)) self.rbgPanel0.add(self.text_editor_BTN) self.gbcPanel0.gridx = 0 self.gbcPanel0.gridy = 22 self.gbPanel0.setConstraints(self.text_editor_BTN, self.gbcPanel0) self.panel0.add(self.text_editor_BTN) self.add(self.panel0)
class Plugin(IHttpListener): MUTATE_ID_COLUMN_INDEX = 4 ORIG_ID_COLUMN_INDEX = 5 def __init__(self, callbacks): self.callbacks = callbacks self.helpers = self.callbacks.getHelpers() self.origMessageEditorController = MessageEditorController() self.mutatedMessageEditorController = MessageEditorController() self.origSearchString = replacements.origSearchString self.replacements = replacements.replacements self.requestResponseCache = {} def start(self): self.frame = JDialog() #self.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) self.frame.setLocation(0, 1500) self.frame.setSize(1000, 200) self.tableDataModel = DefaultTableModel([], [ "URL", "Code", "Content-Length", "Location", "Mutated Id", "Orig Id" ]) self.jtable = JTable(self.tableDataModel) scrollPane = JScrollPane(self.jtable) self.jtable.setFillsViewportHeight(True) messageEditorOrig = self.callbacks.createMessageEditor(None, False) messageEditorModified = self.callbacks.createMessageEditor( None, False) self.editorSplitPane = JSplitPane(JSplitPane.HORIZONTAL_SPLIT, messageEditorOrig.getComponent(), messageEditorModified.getComponent()) self.editorSplitPane.setResizeWeight(0.5) splitPane = JSplitPane(JSplitPane.VERTICAL_SPLIT, scrollPane, self.editorSplitPane) splitPane.setResizeWeight(0.5) class TableSelector(ListSelectionListener): def __init__(self, plugin): self.plugin = plugin def valueChanged(self, event): if not event.getValueIsAdjusting(): selectedRowIndex = self.plugin.jtable.getSelectedRows()[0] self.plugin._rowSelected(selectedRowIndex) self.jtable.getSelectionModel().addListSelectionListener( TableSelector(self)) self.frame.add(splitPane) self.frame.setVisible(True) self.callbacks.registerHttpListener(self) self.callbacks.setExtensionName("Custom Plugin") return def stop(self): print("Closing!") self.callbacks.removeHttpListener(self) self.frame.dispose() self.jrame = None return def _rowSelected(self, index): #self.splitPane.setLeftComponent( #self.callbacks.createMessageEditor( origId = self.tableDataModel.getValueAt( index, self.ORIG_ID_COLUMN_INDEX).encode('ascii', 'ignore') mutateId = self.tableDataModel.getValueAt( index, self.MUTATE_ID_COLUMN_INDEX).encode('ascii', 'ignore') self.origMessageEditorController.setRequestResponse( self.requestResponseCache[origId]) messageEditorOrig = self.callbacks.createMessageEditor( self.origMessageEditorController, False) messageEditorOrig.setMessage( self.requestResponseCache[origId].getResponse(), False) self.editorSplitPane.setLeftComponent(messageEditorOrig.getComponent()) self.mutatedMessageEditorController.setRequestResponse( self.requestResponseCache[mutateId]) messageEditorMutated = self.callbacks.createMessageEditor( self.mutatedMessageEditorController, False) messageEditorMutated.setMessage( self.requestResponseCache[mutateId].getResponse(), False) self.editorSplitPane.setRightComponent( messageEditorMutated.getComponent()) print(mutateId) print("Row selected") print(str(index)) def _buildResponseHeadersDictionary(self, headers): """Creates key/value lookup from list of headers. Header names are converted to lowercase. If header is returned multiple time, last header has precedence.""" d = {} #Skip first "header", it's the response code line. for i in range(1, len(headers)): (name, value) = headers[i].split(":", 1) d[name.lower()] = value return d def _getDictValueOrEmptyStr(self, d, key): if key in d: return d[key] else: return "" def handleReceivedResponseForModifiedRequest(self, requestResponse): #Get original HTTP Request requestData = StringUtil.fromBytes(requestResponse.getRequest()) requestId = re.search(b"^X-REQUEST-ID: ([^\r]*)", requestData, flags=re.MULTILINE).group(1).encode('ascii') origRequestId = re.search(b"^X-REQUEST-ORIG-ID: ([^\r]*)", requestData, flags=re.MULTILINE).group(1).encode('ascii') print("Keys") print(requestId) print(origRequestId) print(self.requestResponseCache.keys()) self.requestResponseCache[requestId] = requestResponse origRequestResponse = self.requestResponseCache[origRequestId] analyzedOrigResponse = self.helpers.analyzeResponse( origRequestResponse.getResponse()) analayzeMutatedResponse = self.helpers.analyzeResponse( requestResponse.getResponse()) origResponseHeaders = self._buildResponseHeadersDictionary( analyzedOrigResponse.getHeaders()) mutatedResponseHeaders = self._buildResponseHeadersDictionary( analayzeMutatedResponse.getHeaders()) mutatedRequestInfo = self.helpers.analyzeRequest( requestResponse.getHttpService(), requestResponse.getRequest()) model = self.jtable.getModel() model.addRow([ str(mutatedRequestInfo.getUrl()), str(analayzeMutatedResponse.getStatusCode()), self._getDictValueOrEmptyStr(mutatedResponseHeaders, "content-length"), self._getDictValueOrEmptyStr(mutatedResponseHeaders, "location"), requestId, origRequestId ]) print("Modified Request Found: %s %s" % (requestId, origRequestId)) #Get original request and response object from lookup #Get request from lookup def processHttpMessage(self, toolFlag, messageIsRequest, requestResponse): if not messageIsRequest: requestData = StringUtil.fromBytes(requestResponse.getRequest()) #We generated the request, process it if requestData.find(b"X-REQUEST-ID") != -1: self.handleReceivedResponseForModifiedRequest(requestResponse) #Response received for non-mutated request. #Mutate request and send it. else: origRequestResponseUUID = str(uuid.uuid4()) reload(replacements) print("Looking for replacements") for replacement in self.replacements: newRequestData = re.sub(self.origSearchString, replacement, requestData) #If no replacemnets made, don't send any requests if newRequestData != requestData: newRequestUUID = str(uuid.uuid4()) newRequestData = re.sub( b"Host", b"X-REQUEST-ID: " + newRequestUUID + "\r\nHost", requestData) newRequestData = re.sub( b"Host", b"X-REQUEST-ORIG-ID: " + origRequestResponseUUID + "\r\nHost", newRequestData) print("Sending Mutated Request") print(newRequestData) self.requestResponseCache[ origRequestResponseUUID] = requestResponse httpService = requestResponse.getHttpService() self.callbacks.makeHttpRequest(httpService, newRequestData) print("Got here")
jB2 = JButton("Exit", actionPerformed=exitme) lowerPanel.add(jB2); topPanel.setLayout(BorderLayout()); # comments jScrollPane1 = JScrollPane(); jScrollPane1.getViewport().add( jDescription ); topPanel.add(jScrollPane1,BorderLayout.CENTER); jDescription.setLineWrap(1); jDescription.setWrapStyleWord(1); jDescription.setCaretColor(Color.red); frame.add( topPanel, BorderLayout.CENTER ); frame.add( lowerPanel, BorderLayout.SOUTH ); bounds = view.getBounds() ww = bounds.width hh= bounds.height xx = bounds.x yy = bounds.y frame.setLocation(xx+(int)(0.4*ww), yy+(int)(0.1*hh)) frame.pack() frame.setSize( (int)(0.5*ww),(int)(0.8*hh) ); frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE) frame.setVisible(1)