示例#1
0
  private void setStartedState() {
    mPortList.clear();

    System.getNMap().synScan(System.getCurrentTarget(), mScanReceiver, mCustomPorts).start();

    mRunning = true;
  }
    @Override
    public void run() {
      byte[] buffer = new byte[128];
      DatagramPacket packet = new DatagramPacket(buffer, buffer.length, mAddress, NETBIOS_UDP_PORT),
          query =
              new DatagramPacket(
                  NETBIOS_REQUEST, NETBIOS_REQUEST.length, mAddress, NETBIOS_UDP_PORT);
      String name, address = mAddress.getHostAddress();
      Target target;

      for (int i = 0; i < MAX_RETRIES; i++) {
        try {
          mSocket.send(query);
          mSocket.receive(packet);

          byte[] data = packet.getData();

          if (data != null && data.length >= 74) {
            String response = new String(data, "ASCII");

            // i know this is horrible, but i really need only the netbios name
            name = response.substring(57, 73).trim();

            Logger.debug(address + " was resolved to " + name);

            // update netbios cache
            mArpReader.addNetBiosName(address, name);

            // existing target
            target = System.getTargetByAddress(address);
            if (target != null) {
              target.setAlias(name);
              sendEndpointUpdateNotification();
            }

            break;
          }
        } catch (SocketTimeoutException ste) {
          // swallow timeout error
        } catch (IOException e) {
          System.errorLogging(e);
        } finally {
          try {
            // send again a query
            mSocket.send(query);
          } catch (Exception e) {
            // swallow error
          }
        }
      }

      mSocket.close();
    }
示例#3
0
  public void run() {

    int read = -1,
        size = 0,
        max =
            Integer.parseInt(
                System.getSettings().getString("PREF_HTTP_MAX_BUFFER_SIZE", "10485760"));
    byte[] chunk = new byte[CHUNK_SIZE];

    try {
      while ((read = mReader.read(chunk, 0, CHUNK_SIZE)) > 0 && size < max) {
        mBuffer.append(chunk, read);

        size += read;
      }

      if (mBuffer.isEmpty() == false) {
        // do we have html ?
        if (mBuffer.indexOf(CONTENT_TEXT_HTML) != -1) {
          // split headers and body, then apply the filter
          String data = mBuffer.toString();
          String[] split = data.split(HEAD_SEPARATOR, 2);
          String headers = split[0], body = (split.length > 1 ? split[1] : ""), patched = "";

          body = mFilter.onHtmlReceived(body);

          // remove explicit content length, just in case the body changed after filtering
          for (String header : headers.split("\n")) {
            if (header.toLowerCase().contains("content-length") == false) patched += header + "\n";
          }

          headers = patched;
          mBuffer.setData((headers + HEAD_SEPARATOR + body).getBytes());
        }

        mWriter.write(mBuffer.getData());
        mWriter.flush();
      }
    } catch (OutOfMemoryError ome) {
      Log.e(TAG, ome.toString());
    } catch (Exception e) {
      System.errorLogging(TAG, e);
    } finally {
      try {
        mWriter.flush();
        mWriter.close();
        mReader.close();
      } catch (IOException e) {
        System.errorLogging(TAG, e);
      }
    }
  }
示例#4
0
  private void setStoppedState() {
    System.getNMap().kill();
    mScanProgress.setVisibility(View.INVISIBLE);
    mRunning = false;
    mScanToggleButton.setChecked(false);

    if (mPortList.size() == 0)
      Toast.makeText(this, getString(R.string.no_open_ports), Toast.LENGTH_LONG).show();
  }
    @Override
    public void run() {
      Logger.debug("UdpProber started ...");

      mStopped = false;

      int i, nhosts = 0;
      IP4Address current = null;

      try {
        mNetwork = System.getNetwork();
        nhosts = mNetwork.getNumberOfAddresses();
      } catch (Exception e) {
        System.errorLogging(e);
      }

      while (!mStopped && mNetwork != null && nhosts > 0) {
        try {
          for (i = 1, current = IP4Address.next(mNetwork.getStartAddress());
              current != null && i <= nhosts;
              current = IP4Address.next(current), i++) {
            // rescanning the gateway could cause an issue when the gateway itself has multiple
            // interfaces ( LAN, WAN ... )
            if (!current.equals(mNetwork.getGatewayAddress())
                && !current.equals(mNetwork.getLocalAddress())) {
              InetAddress address = current.toInetAddress();

              try {
                mExecutor.execute(new SingleProber(address));
              } catch (RejectedExecutionException e) {
                // ignore since this is happening because the executor was shut down.
                break;
              } catch (OutOfMemoryError m) {
                // wait until the thread queue gets freed
                break;
              }
            }
          }

          Thread.sleep(1000);
        } catch (Exception e) {
        }
      }
    }
 public void exit() {
   mRunning = false;
   try {
     mProber.exit();
     mTargetProber.exit();
     mArpReader.exit();
   } catch (Exception e) {
     System.errorLogging(e);
   }
 }
示例#7
0
 /* keep grabbing commands, acquiring locks, until everything is executed */
 public void run() {
   try {
     while (true) {
       while (mSleep) Thread.sleep(200);
       String next = grabCommand();
       if (next != null) {
         processCommand(next);
         Thread.sleep(50);
       } else Thread.sleep(200);
     }
   } catch (InterruptedException e) {
     Logger.warning("interrupted");
   } catch (TimeoutException e) {
     Logger.error("Session timed out: " + e.getMessage());
   } catch (RPCClient.MSFException e) {
     System.errorLogging(e);
   } catch (IOException e) {
     System.errorLogging(e);
   } finally {
     stopSession();
   }
 }
    @Override
    public void run() {
      while (!mStopped) {
        try {
          for (Target t : System.getTargets()) {

            if (t.getAddress() == null) continue;

            DatagramSocket socket = new DatagramSocket();
            DatagramPacket packet =
                new DatagramPacket(
                    NETBIOS_REQUEST, NETBIOS_REQUEST.length, t.getAddress(), NETBIOS_UDP_PORT);

            socket.setSoTimeout(200);
            socket.send(packet);

            socket.close();
          }
          sleep(5000);
        } catch (Exception e) {
          if (!mStopped) System.errorLogging(e);
        }
      }
    }
  @Override
  public void run() {
    Logger.debug("Network monitor started ...");

    mRunning = true;

    try {
      mProber.start();
      mArpReader.start();
      mTargetProber.start();

      mProber.join();
      mArpReader.join();
      mTargetProber.join();

      Logger.debug("Network monitor stopped.");

      mRunning = false;
    } catch (Exception e) {
      System.errorLogging(e);
    }
  }
示例#10
0
    @Override
    public void run() {
      Logger.debug("ArpReader started ...");

      mNetBiosMap.clear();
      mStopped = false;
      String iface = "";
      ArrayList<Target> foundTargets = new ArrayList<Target>();

      try {
        iface = System.getNetwork().getInterface().getDisplayName();
      } catch (Exception e) {
        System.errorLogging(e);
      }

      while (!mStopped) {
        try {
          BufferedReader reader = new BufferedReader(new FileReader(ARP_TABLE_FILE));
          String line = null, name = null;
          Matcher matcher = null;
          Endpoint endpoint = null;
          Target target = null;
          Network network = System.getNetwork();

          foundTargets.clear();

          while ((line = reader.readLine()) != null) {
            if ((matcher = ARP_TABLE_PARSER.matcher(line)) != null && matcher.find()) {
              String address = matcher.group(1),
                  // hwtype  = matcher.group( 2 ),
                  flags = matcher.group(3),
                  hwaddr = matcher.group(4),
                  // mask	   = matcher.group( 5 ),
                  device = matcher.group(6);

              if (device.equals(iface)
                  && !hwaddr.equals("00:00:00:00:00:00")
                  && flags.contains("2")) {
                endpoint = new Endpoint(address, hwaddr);
                target = new Target(endpoint);
                foundTargets.add(target);
                // rescanning the gateway could cause an issue when the gateway itself has multiple
                // interfaces ( LAN, WAN ... )
                if (!endpoint.getAddress().equals(network.getGatewayAddress())
                    && !endpoint.getAddress().equals(network.getLocalAddress())) {
                  synchronized (mNetBiosMap) {
                    name = mNetBiosMap.get(address);
                  }

                  if (name == null) {
                    try {
                      mExecutor.execute(new NBResolver(address));
                    } catch (RejectedExecutionException e) {
                      // ignore since this is happening because the executor was shut down.
                    } catch (OutOfMemoryError m) {
                      // wait until the thread queue gets freed
                      break;
                    }

                    if (!target.isRouter()) {
                      // attempt DNS resolution
                      name = endpoint.getAddress().getHostName();

                      if (!name.equals(address)) {
                        Logger.debug(address + " was DNS resolved to " + name);

                        synchronized (mNetBiosMap) {
                          mNetBiosMap.put(address, name);
                        }
                      } else name = null;
                    }
                  }

                  if (!System.hasTarget(target)) sendNewEndpointNotification(endpoint, name);
                  else if (name != null) {
                    target = System.getTargetByAddress(address);
                    if (target != null && !target.hasAlias()) {
                      target.setAlias(name);
                      sendEndpointUpdateNotification();
                    }
                  }
                }
              }
            }
          }

          reader.close();

          boolean update = false;
          boolean found;
          int i;
          Target[] currentTargets =
              System.getTargets().toArray(new Target[System.getTargets().size()]);

          for (Target t : currentTargets) {

            endpoint = t.getEndpoint();

            if (endpoint == null) continue;
            for (found = false, i = 0; i < foundTargets.size() && !found; i++) {
              if (endpoint.equals(foundTargets.get(i).getEndpoint())) found = true;
            }

            if (t.isConnected() != found
                && !t.isRouter()
                && !t.getAddress().equals(network.getLocalAddress())) {
              t.setConneced(found);
              update = true;
            }
          }

          if (update) sendEndpointUpdateNotification();
          Thread.sleep(500);
        } catch (Exception e) {
          System.errorLogging(e);
          String msg = e.getMessage();
          if (msg != null && msg.contains("EMFILE")) {
            try {
              Shell.exec(
                  "lsof | grep " + android.os.Process.myPid(),
                  new Shell.OutputReceiver() {
                    @Override
                    public void onStart(String command) {}

                    @Override
                    public void onNewLine(String line) {
                      Logger.debug(line);
                    }

                    @Override
                    public void onEnd(int exitCode) {}
                  });
            } catch (Exception e1) {
              System.errorLogging(e1);
            }
          }
        }
      }
    }
示例#11
0
 public ShellSession(Integer id, Map<String, Object> map) throws UnknownHostException {
   super(id, map);
   mRpcClient = System.getMsfRpc();
   mReceiver = null;
   start();
 }
示例#12
0
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mScanToggleButton = (ToggleButton) findViewById(R.id.scanToggleButton);
    mScanProgress = (ProgressBar) findViewById(R.id.scanActivity);

    mScanToggleButton.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View v) {
            if (mRunning) {
              setStoppedState();
            } else {
              setStartedState();
            }
          }
        });

    ListView mScanList = (ListView) findViewById(R.id.scanListView);

    for (Port port : System.getCurrentTarget().getOpenPorts()) {
      mPortList.add(port.number + " ( " + port.protocol.toString().toLowerCase() + " )");
    }

    mListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mPortList);
    mScanList.setAdapter(mListAdapter);
    mScanList.setOnItemLongClickListener(
        new OnItemLongClickListener() {
          @Override
          public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            int portNumber = System.getCurrentTarget().getOpenPorts().get(position).number;
            String url = "";

            if (portNumber == 80)
              url = "http://" + System.getCurrentTarget().getCommandLineRepresentation() + "/";
            else if (portNumber == 443)
              url = "https://" + System.getCurrentTarget().getCommandLineRepresentation() + "/";
            else if (portNumber == 21)
              url = "ftp://" + System.getCurrentTarget().getCommandLineRepresentation();
            else if (portNumber == 22)
              url = "ssh://" + System.getCurrentTarget().getCommandLineRepresentation();
            else
              url =
                  "telnet://"
                      + System.getCurrentTarget().getCommandLineRepresentation()
                      + ":"
                      + portNumber;

            final String furl = url;

            new ConfirmDialog(
                    "Open",
                    "Open " + url + " ?",
                    PortScanner.this,
                    new ConfirmDialogListener() {
                      @Override
                      public void onConfirm() {
                        try {
                          Intent browser = new Intent(Intent.ACTION_VIEW, Uri.parse(furl));

                          PortScanner.this.startActivity(browser);
                        } catch (ActivityNotFoundException e) {
                          System.errorLogging(e);

                          new ErrorDialog(
                                  getString(R.string.error),
                                  getString(R.string.no_activities_for_url),
                                  PortScanner.this)
                              .show();
                        }
                      }

                      @Override
                      public void onCancel() {}
                    })
                .show();

            return false;
          }
        });
  }