├── .gitignore ├── src ├── test │ └── java │ │ ├── test │ │ └── constants │ │ │ └── IConstants.java │ │ └── org │ │ └── nmap4j │ │ ├── core │ │ ├── valid │ │ │ └── ValidationTest.java │ │ ├── nmap │ │ │ ├── ExecutionResultsTest.java │ │ │ ├── NMapPropertiesTest.java │ │ │ └── NMapExecutorTest.java │ │ ├── scans │ │ │ └── BaseScanTest.java │ │ └── flags │ │ │ └── ArgumentPropertiesTest.java │ │ ├── Nmap4jTest.java │ │ ├── parsers │ │ ├── Nmap4jUtil.java │ │ ├── NMapXmlHandlerTest.java │ │ └── OnePassParserTest.java │ │ └── data │ │ └── host │ │ └── CpeTests.java └── main │ └── java │ └── org │ └── nmap4j │ ├── model │ ├── Host.java │ └── Port.java │ ├── INmap4j.java │ ├── valid │ └── HostsInputValidator.java │ ├── data │ ├── host │ │ ├── trace │ │ │ ├── Hop.java │ │ │ └── Trace.java │ │ ├── ports │ │ │ ├── ExtraPorts.java │ │ │ └── Port.java │ │ ├── IpIdSequence.java │ │ ├── Distance.java │ │ ├── Status.java │ │ ├── TcpTsSequence.java │ │ ├── Hostnames.java │ │ ├── Uptime.java │ │ ├── Ports.java │ │ ├── Cpe.java │ │ ├── Times.java │ │ ├── os │ │ │ ├── OsMatch.java │ │ │ ├── PortUsed.java │ │ │ └── OsClass.java │ │ ├── Address.java │ │ ├── TcpSequence.java │ │ └── Os.java │ ├── nmaprun │ │ ├── Verbose.java │ │ ├── Debugging.java │ │ ├── host │ │ │ └── ports │ │ │ │ ├── extraports │ │ │ │ └── ExtraReasons.java │ │ │ │ └── port │ │ │ │ ├── State.java │ │ │ │ └── Service.java │ │ ├── hostnames │ │ │ └── Hostname.java │ │ ├── RunStats.java │ │ ├── runstats │ │ │ ├── Hosts.java │ │ │ └── Finished.java │ │ ├── ScanInfo.java │ │ └── Host.java │ └── NMapRun.java │ ├── parser │ ├── events │ │ ├── NMap4JParserEventListener.java │ │ └── ParserEvent.java │ ├── util │ │ ├── BogusIPGenerator.java │ │ ├── UidGenerator.java │ │ └── NMapXmlObsfucator.java │ ├── OnePassParser.java │ └── INMapRunHandler.java │ ├── core │ ├── scans │ │ ├── IScanCallback.java │ │ ├── IScanValidator.java │ │ ├── HostDiscovery.java │ │ ├── ParameterValidationFailureException.java │ │ ├── NMapExecutorThread.java │ │ ├── ServiceDiscovery.java │ │ └── IScan.java │ └── nmap │ │ ├── NMapExecutionException.java │ │ ├── NMapInitializationException.java │ │ ├── ExecutionResults.java │ │ ├── NMapProperties.java │ │ └── NMapExecutor.java │ └── Nmap4j.java ├── .travis.yml ├── license.txt ├── README.md └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | bin 2 | build 3 | dist 4 | javadocs 5 | .settings 6 | target 7 | *.class 8 | .idea 9 | *.iml -------------------------------------------------------------------------------- /src/test/java/test/constants/IConstants.java: -------------------------------------------------------------------------------- 1 | package test.constants; 2 | 3 | public interface IConstants { 4 | 5 | public final static String XML_FILE = "nmap-xml/enchantedgrounds-08272010-2140.xml" ; 6 | 7 | } 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - openjdk8 4 | before_install: 5 | - sudo apt-get -qq update 6 | - sudo apt-get install -y nmap 7 | - sudo ln -s /usr/bin/nmap /usr/local/bin/nmap 8 | script: 9 | - sudo /usr/local/maven/bin/mvn clean install 10 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/model/Host.java: -------------------------------------------------------------------------------- 1 | package org.nmap4j.model; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class Host { 6 | 7 | 8 | private ArrayList portData = new ArrayList() ; 9 | 10 | public ArrayList getPortData() { 11 | return portData; 12 | } 13 | 14 | public void setPortData(ArrayList portData) { 15 | this.portData = portData; 16 | } 17 | 18 | public void addPortData( Port port ) { 19 | portData.add( port ) ; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/INmap4j.java: -------------------------------------------------------------------------------- 1 | package org.nmap4j; 2 | 3 | import org.nmap4j.core.nmap.ExecutionResults; 4 | import org.nmap4j.core.nmap.NMapExecutionException; 5 | import org.nmap4j.core.nmap.NMapInitializationException; 6 | import org.nmap4j.data.NMapRun; 7 | 8 | public interface INmap4j { 9 | void execute() throws NMapInitializationException, NMapExecutionException; 10 | void addFlags(String flagSet); 11 | void includeHosts(String hosts); 12 | void excludeHosts(String hosts); 13 | String getOutput(); 14 | NMapRun getResult(); 15 | boolean hasError(); 16 | ExecutionResults getExecutionResults(); 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/org/nmap4j/core/valid/ValidationTest.java: -------------------------------------------------------------------------------- 1 | package org.nmap4j.core.valid; 2 | 3 | import org.junit.Test; 4 | import org.nmap4j.valid.HostsInputValidator; 5 | 6 | import static org.junit.Assert.assertFalse; 7 | import static org.junit.Assert.assertTrue; 8 | 9 | public class ValidationTest { 10 | 11 | @Test 12 | public void validHosts() { 13 | assertTrue(new HostsInputValidator().valid("8.8.4.3")); 14 | assertTrue(new HostsInputValidator().valid("8.8.4.3/32")); 15 | assertTrue(new HostsInputValidator().valid("google.com")); 16 | } 17 | 18 | @Test 19 | public void nonValidHosts() { 20 | boolean b; 21 | assertFalse(new HostsInputValidator().valid("8.8.4")); 22 | assertFalse(new HostsInputValidator().valid("8.8.4.3/322")); 23 | assertFalse(new HostsInputValidator().valid("google.com.8.8.4.3")); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/valid/HostsInputValidator.java: -------------------------------------------------------------------------------- 1 | package org.nmap4j.valid; 2 | 3 | import com.google.common.net.InternetDomainName; 4 | 5 | import java.io.File; 6 | import java.util.regex.Pattern; 7 | 8 | /** 9 | * nmap can scan over hostname, single ip, subnet, or using a file input 10 | */ 11 | public class HostsInputValidator { 12 | private final static String IP_REGEX = "^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])"; 13 | 14 | private final static String IP_CIDR = "(\\/([0-9]|[1-2][0-9]|3[0-2]))"; 15 | 16 | public boolean valid(String host) { 17 | return InternetDomainName.isValid(host) || isIp(host) || isSubnet(host) || isFile(host); 18 | } 19 | 20 | 21 | private boolean isIp(final String ip) { 22 | final Pattern PATTERN = Pattern.compile(IP_REGEX + "$"); 23 | return PATTERN.matcher(ip).matches(); 24 | } 25 | 26 | private boolean isSubnet(final String subnet) { 27 | final Pattern PATTERN = Pattern.compile(IP_REGEX + IP_CIDR); 28 | return PATTERN.matcher(subnet).matches(); 29 | } 30 | 31 | private boolean isFile(String host) { 32 | return new File(host).exists(); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/org/nmap4j/Nmap4jTest.java: -------------------------------------------------------------------------------- 1 | package org.nmap4j; 2 | 3 | import static org.junit.Assert.fail; 4 | 5 | import org.junit.Test; 6 | import org.nmap4j.core.nmap.NMapExecutionException; 7 | import org.nmap4j.core.nmap.NMapInitializationException; 8 | import org.nmap4j.data.NMapRun; 9 | 10 | public class Nmap4jTest { 11 | 12 | 13 | @Test 14 | public void basicNmap4jUsageTest() { 15 | 16 | try { 17 | String path = "/usr/local" ; 18 | 19 | Nmap4j nmap4j = new Nmap4j( path ) ; 20 | nmap4j.addFlags( "-sV -T5 -O -oX -" ) ; 21 | nmap4j.includeHosts( "localhost" ) ; 22 | nmap4j.execute() ; 23 | if( !nmap4j.hasError() ) { 24 | NMapRun nmapRun = nmap4j.getResult() ; 25 | String output = nmap4j.getOutput() ; 26 | if( output == null ) { 27 | fail() ; 28 | } 29 | String errors = nmap4j.getExecutionResults().getErrors() ; 30 | if (errors == null ) { 31 | fail() ; 32 | } 33 | } 34 | } catch (NMapInitializationException e) { 35 | // TODO Auto-generated catch block 36 | e.printStackTrace(); 37 | fail() ; 38 | } catch (NMapExecutionException e) { 39 | // TODO Auto-generated catch block 40 | e.printStackTrace(); 41 | fail() ; 42 | } 43 | 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/host/trace/Hop.java: -------------------------------------------------------------------------------- 1 | package org.nmap4j.data.host.trace; 2 | 3 | import org.nmap4j.data.nmaprun.Host; 4 | 5 | /** 6 | * This class has hop record on tracrout result 7 | * 8 | * @author omark 9 | * 10 | */ 11 | public class Hop { 12 | public final static String HOP_TAG = "hop" ; 13 | public final static String TTL_ATTR = "ttl" ; 14 | public final static String IPADDR_ATTR= "ipaddr" ; 15 | public final static String RTT_ATTR = "rtt" ; 16 | 17 | 18 | private int ttl; 19 | private String ipaddr; 20 | private float rtt; 21 | private Host host; 22 | 23 | public int getTtl() { 24 | return ttl; 25 | } 26 | public void setTtl(int ttl) { 27 | this.ttl = ttl; 28 | } 29 | public String getIpaddr() { 30 | return ipaddr; 31 | } 32 | public void setIpaddr(String ipaddr) { 33 | this.ipaddr = ipaddr; 34 | } 35 | public float getRtt() { 36 | return rtt; 37 | } 38 | public void setRtt(float rtt) { 39 | this.rtt = rtt; 40 | } 41 | 42 | 43 | public Host getHost() { 44 | return host; 45 | } 46 | public void setHost(Host host) { 47 | this.host = host; 48 | } 49 | @Override 50 | public String toString() { 51 | return "Hop [ttl=" + ttl + ", ipaddr=" + ipaddr + ", rtt=" + rtt + "]"; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/host/trace/Trace.java: -------------------------------------------------------------------------------- 1 | package org.nmap4j.data.host.trace; 2 | 3 | import java.util.ArrayList; 4 | 5 | /** 6 | * This class catches tracerout result if was enabled on scan command 7 | * 8 | * @author omark 9 | * 10 | */ 11 | public class Trace { 12 | public final static String TRACE_TAG = "trace" ; 13 | public final static String PORT_ATTR = "port" ; 14 | public final static String PORTOCOL_ATTR = "proto" ; 15 | 16 | private long port; 17 | private String protocol; 18 | private ArrayList hops = new ArrayList<>(); 19 | 20 | public long getPort() { 21 | return port; 22 | } 23 | 24 | public void setPort(long port) { 25 | this.port = port; 26 | } 27 | 28 | public String getProtocol() { 29 | return protocol; 30 | } 31 | 32 | public void setProtocol(String protocol) { 33 | this.protocol = protocol; 34 | } 35 | 36 | public ArrayList getHops() { 37 | return hops; 38 | } 39 | 40 | public void setHops(ArrayList hops) { 41 | this.hops = hops; 42 | } 43 | 44 | public void addHop( Hop p ) { 45 | if( !hops.contains( p ) ) { 46 | hops.add( p ) ; 47 | } 48 | } 49 | 50 | public void remove( Hop p ) { 51 | if( hops.contains( p ) ) { 52 | hops.remove( p ) ; 53 | } 54 | } 55 | 56 | 57 | @Override 58 | public String toString() { 59 | return "Trace [port=" + port + ", protocol=" + protocol + ", hops=" 60 | + hops + "]"; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/test/java/org/nmap4j/core/nmap/ExecutionResultsTest.java: -------------------------------------------------------------------------------- 1 | package org.nmap4j.core.nmap; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.After; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | import org.nmap4j.core.nmap.ExecutionResults; 9 | 10 | public class ExecutionResultsTest { 11 | 12 | private ExecutionResults fixture ; 13 | 14 | @Before 15 | public void setUp() { 16 | fixture = new ExecutionResults() ; 17 | } 18 | 19 | @After 20 | public void tearDown() { 21 | fixture = null ; 22 | } 23 | 24 | @Test 25 | public void testSetErrors() { 26 | String errMsg = "A Test error string" ; 27 | fixture.setErrors( errMsg ) ; 28 | 29 | if( fixture.getErrors() == null ) { 30 | fail() ; 31 | } else { 32 | assertEquals( errMsg, fixture.getErrors() ) ; 33 | } 34 | } 35 | 36 | @Test 37 | public void testSetOutput() { 38 | String outputMsg = "A test output message" ; 39 | fixture.setOutput(outputMsg) ; 40 | 41 | if( fixture.getOutput() == null ) { 42 | fail() ; 43 | } else { 44 | assertEquals( outputMsg, fixture.getOutput() ) ; 45 | } 46 | 47 | } 48 | 49 | @Test 50 | public void testTwoStringConstructor() { 51 | String errMsg = "A test err message" ; 52 | String outputMsg = "A test output message" ; 53 | 54 | fixture = new ExecutionResults(errMsg, outputMsg ) ; 55 | 56 | assertEquals( errMsg, fixture.getErrors()) ; 57 | assertEquals( outputMsg, fixture.getOutput() ) ; 58 | 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/test/java/org/nmap4j/parsers/Nmap4jUtil.java: -------------------------------------------------------------------------------- 1 | package org.nmap4j.parsers; 2 | 3 | import org.junit.Test; 4 | import org.nmap4j.core.flags.Flag; 5 | import org.nmap4j.core.nmap.ExecutionResults; 6 | import org.nmap4j.core.scans.BaseScan; 7 | import org.nmap4j.core.scans.IScan.TimingFlag; 8 | import org.nmap4j.data.NMapRun; 9 | import org.nmap4j.parser.OnePassParser; 10 | 11 | public class Nmap4jUtil { 12 | 13 | @Test 14 | public void testOpen() throws Exception { 15 | System.out.println( "Is Port Open? " + isPortOpen( 3307, "localhost" ) ) ; 16 | } 17 | 18 | public static boolean isPortOpen( int port, String address ) throws Exception { 19 | 20 | BaseScan baseScan = new BaseScan( "/usr/local") ; 21 | 22 | baseScan.includeHost( address ) ; 23 | baseScan.addPorts(new int[]{ port } ) ; 24 | baseScan.addFlag( Flag.OS_DETECTION ) ; 25 | baseScan.setTiming( TimingFlag.AGGRESSIVE ) ; 26 | 27 | boolean open = false ; 28 | 29 | ExecutionResults results = baseScan.executeScan() ; 30 | System.out.println( results.getExecutedCommand() ) ; 31 | System.out.println( results.getOutput() ) ; 32 | if( results.hasErrors() ) { 33 | return false ; 34 | } 35 | 36 | OnePassParser opp = new OnePassParser() ; 37 | NMapRun nmapRun = opp.parse(results.getOutput(), OnePassParser.STRING_INPUT ) ; 38 | 39 | String output = nmapRun.getHosts().get(0).getPorts().getPorts().get(0).getState().getState() ; 40 | 41 | if( output != null ) { 42 | if( output.toLowerCase().equals( "open" ) ) { 43 | open = true ; 44 | } 45 | } 46 | 47 | return open ; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/test/java/org/nmap4j/core/scans/BaseScanTest.java: -------------------------------------------------------------------------------- 1 | package org.nmap4j.core.scans; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | import org.nmap4j.core.flags.Flag; 7 | import org.nmap4j.core.nmap.ExecutionResults; 8 | import org.nmap4j.core.nmap.NMapExecutionException; 9 | import org.nmap4j.core.nmap.NMapInitializationException; 10 | import org.nmap4j.core.scans.BaseScan; 11 | import org.nmap4j.core.scans.ParameterValidationFailureException; 12 | import org.nmap4j.core.scans.IScan.OutputType; 13 | import org.nmap4j.core.scans.IScan.TimingFlag; 14 | 15 | public class BaseScanTest { 16 | 17 | @Test 18 | public void testSimpleScan() { 19 | 20 | BaseScan baseScan = new BaseScan( "/usr/local") ; 21 | 22 | baseScan.includeHost( "localhost" ) ; 23 | baseScan.addPorts(new int[]{ 22,80,443,3306} ) ; 24 | baseScan.addFlag( Flag.OS_DETECTION ) ; 25 | baseScan.setTiming( TimingFlag.AGGRESSIVE ) ; 26 | 27 | System.out.println( baseScan.getArgumentProperties().getFlags() ) ; 28 | System.out.println( baseScan.getNMapProperties().getFullyFormattedCommand() ) ; 29 | 30 | try { 31 | ExecutionResults results = baseScan.executeScan() ; 32 | System.out.println( results.getExecutedCommand() ) ; 33 | System.out.println( results.getOutput() ) ; 34 | if( results.hasErrors() ) { 35 | System.out.println( "Errors: " + results.getErrors() ) ; 36 | } else { 37 | System.out.println( "Results: " + results.getOutput() ) ; 38 | } 39 | 40 | 41 | 42 | } catch (ParameterValidationFailureException e) { 43 | e.printStackTrace(); 44 | fail() ; 45 | } catch (NMapExecutionException e) { 46 | e.printStackTrace(); 47 | fail() ; 48 | } catch (NMapInitializationException e) { 49 | e.printStackTrace(); 50 | fail() ; 51 | } 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | 2 | Copyright (c) 2010, nmap4j.org 3 | 4 | All rights reserved. 5 | 6 | This license covers only the Nmap4j library. To use this library with 7 | Nmap, you must also comply with Nmap's license. Including Nmap within 8 | commercial applications or appliances generally requires the purchase 9 | of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | 11 | Redistribution and use in source and binary forms, with or without 12 | modification, are permitted provided that the following conditions are met: 13 | 14 | * Redistributions of source code must retain the above copyright notice, 15 | this list of conditions and the following disclaimer. 16 | * Redistributions in binary form must reproduce the above copyright 17 | notice, this list of conditions and the following disclaimer in the 18 | documentation and/or other materials provided with the distributioan. 19 | * Neither the name of the nmap4j.org nor the names of its contributors 20 | may be used to endorse or promote products derived from this software 21 | without specific prior written permission. 22 | 23 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | POSSIBILITY OF SUCH DAMAGE. 34 | -------------------------------------------------------------------------------- /src/test/java/org/nmap4j/data/host/CpeTests.java: -------------------------------------------------------------------------------- 1 | package org.nmap4j.data.host; 2 | 3 | import static org.junit.Assert.fail; 4 | 5 | import org.junit.Test; 6 | 7 | public class CpeTests { 8 | 9 | private String cpeSimpleLinux = "cpe:/o:linux:kernel:2.6" ; 10 | private String cpeSimpleWindows = "cpe:/o:microsoft:windows_xp::sp3" ; 11 | 12 | //:::::: 13 | 14 | @Test 15 | public void testCpeSimpleLinux() { 16 | Cpe cpe = new Cpe( cpeSimpleLinux ) ; 17 | testParsedData( cpe, "o", "linux", "kernel", "2.6", null, null, null ) ; 18 | } 19 | 20 | @Test 21 | public void testCpeSimpleWindows() { 22 | Cpe cpe = new Cpe( cpeSimpleWindows ) ; 23 | testParsedData( cpe, "o", "microsoft", "windows_xp", "", "sp3", null, null ) ; 24 | } 25 | 26 | private void testParsedData( Cpe cpe, String part, String vendor, String product, 27 | String version, String update, String edition, 28 | String language ) { 29 | 30 | if( cpe.getPart() != null && !cpe.getPart().equals( part ) ) { 31 | fail( "Part - expected: " + part + " found: " + cpe.getPart() ) ; 32 | } 33 | if( cpe.getVendor() != null && !cpe.getVendor().equals( vendor ) ) { 34 | fail( "Vendor - expected: " + vendor + " found: " + cpe.getVendor() ) ; 35 | } 36 | if( cpe.getProduct() != null && !cpe.getProduct().equals( product ) ) { 37 | fail( "Product - expected: " + product + " found: " + cpe.getProduct() ) ; 38 | } 39 | if( cpe.getVersion() != null && !cpe.getVersion().equals( version ) ) { 40 | fail("Version - expected: " + version + " found: " + cpe.getVersion() ) ; 41 | } 42 | if( cpe.getUpdate() != null && !cpe.getUpdate().equals( update ) ) { 43 | fail( "Update - expected: " + update + " found: " + cpe.getUpdate() ) ; 44 | } 45 | if( cpe.getEdition() != null && !cpe.getEdition().equals( edition ) ) { 46 | fail("Edition - expected: " + edition + " found: " + cpe.getEdition() ) ; 47 | } 48 | if( cpe.getLanguage() != null && !cpe.getLanguage().equals( language ) ) { 49 | fail( "Language - expected: " + language + " found: " + cpe.getLanguage() ) ; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Intro 2 | A Java nmap wrapper 3 | 4 | [![Build Status](https://travis-ci.org/narkisr/nmap4j.png)](https://travis-ci.org/narkisr/nmap4j) 5 | 6 | 7 | # Note 8 | 9 | This is a fork of [sourceforge](https://sourceforge.net/projects/nmap4j/?source=directory) repo, please check the original author work. 10 | 11 | # License 12 | 13 | Copyright (c) 2010, nmap4j.org 14 | 15 | All rights reserved. 16 | 17 | This license covers only the Nmap4j library. To use this library with 18 | Nmap, you must also comply with Nmap's license. Including Nmap within 19 | commercial applications or appliances generally requires the purchase 20 | of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 21 | 22 | Redistribution and use in source and binary forms, with or without 23 | modification, are permitted provided that the following conditions are met: 24 | 25 | * Redistributions of source code must retain the above copyright notice, 26 | this list of conditions and the following disclaimer. 27 | * Redistributions in binary form must reproduce the above copyright 28 | notice, this list of conditions and the following disclaimer in the 29 | documentation and/or other materials provided with the distributioan. 30 | * Neither the name of the nmap4j.org nor the names of its contributors 31 | may be used to endorse or promote products derived from this software 32 | without specific prior written permission. 33 | 34 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 35 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 36 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 37 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 38 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 39 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 40 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 41 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 42 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 43 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 44 | POSSIBILITY OF SUCH DAMAGE. 45 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/parser/events/NMap4JParserEventListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.parser.events; 36 | 37 | public interface NMap4JParserEventListener { 38 | 39 | public void parseEventNotification( ParserEvent event ) ; 40 | 41 | } 42 | 43 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/core/scans/IScanCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.core.scans; 36 | 37 | import org.nmap4j.core.nmap.ExecutionResults ; 38 | 39 | public interface IScanCallback { 40 | 41 | public void executionCompleted( ExecutionResults results ) ; 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/test/java/org/nmap4j/core/nmap/NMapPropertiesTest.java: -------------------------------------------------------------------------------- 1 | package org.nmap4j.core.nmap; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.io.File; 6 | 7 | import org.junit.After; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | import org.nmap4j.core.nmap.NMapProperties; 11 | 12 | public class NMapPropertiesTest { 13 | 14 | private NMapProperties fixture ; 15 | 16 | @Before 17 | public void setUp() { 18 | fixture = new NMapProperties() ; 19 | } 20 | 21 | @After 22 | public void tearDown() { 23 | fixture = null ; 24 | } 25 | 26 | @Test 27 | public void testSetPath() { 28 | String path = "/path/to/nmap" ; 29 | 30 | fixture.setPath( path ) ; 31 | 32 | if( fixture.getPath() == null ) { 33 | fail() ; 34 | } 35 | 36 | if( !fixture.getPath().equals( path ) ) { 37 | fail() ; 38 | } 39 | } 40 | 41 | @Test 42 | public void testGetShareDir() { 43 | String path = "/path/to/nmap" ; 44 | 45 | fixture.setPath( path ) ; 46 | 47 | if( fixture.getShareDir() == null ) { 48 | fail() ; 49 | } 50 | 51 | if( !fixture.getShareDir().startsWith( path ) ) { 52 | fail() ; 53 | } 54 | } 55 | 56 | @Test 57 | public void testGetBinDir() { 58 | String path = "/path/to/nmap" ; 59 | 60 | fixture.setPath( path ) ; 61 | 62 | if( fixture.getBinDir() == null ) { 63 | fail() ; 64 | } 65 | 66 | if( !fixture.getBinDir().startsWith( path ) ) { 67 | fail() ; 68 | } 69 | } 70 | 71 | @Test 72 | public void testFullyFormattedCommand() { 73 | String path = "/path/to/nmap" ; 74 | 75 | fixture.setPath( path ) ; 76 | 77 | if( fixture.getFullyFormattedCommand() == null ) { 78 | fail() ; 79 | } 80 | 81 | ///usr/local/bin/nmap --datadir /usr/local/share/nmap 82 | String verificationPath = path + File.separator + "bin" + File.separator + "nmap " + 83 | "--datadir " + path + File.separator + "share" + File.separator + "nmap"; 84 | assertEquals( verificationPath, fixture.getFullyFormattedCommand() ) ; 85 | } 86 | 87 | @Test 88 | public void testStringConstructor() { 89 | String path = "/path/to/nmap" ; 90 | 91 | fixture = new NMapProperties( path ) ; 92 | 93 | assertNotNull( fixture.getPath() ) ; 94 | assertEquals( path, fixture.getPath() ) ; 95 | 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/test/java/org/nmap4j/parsers/NMapXmlHandlerTest.java: -------------------------------------------------------------------------------- 1 | package org.nmap4j.parsers; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | 6 | import javax.xml.parsers.ParserConfigurationException; 7 | import javax.xml.parsers.SAXParser; 8 | import javax.xml.parsers.SAXParserFactory; 9 | 10 | import junit.framework.Assert; 11 | 12 | import org.junit.Test; 13 | import org.nmap4j.parser.INMapRunHandler; 14 | import org.nmap4j.parser.NMapRunHandlerImpl; 15 | import org.nmap4j.parser.NMapXmlHandler; 16 | import org.nmap4j.parser.events.NMap4JParserEventListener; 17 | import org.nmap4j.parser.events.ParserEvent; 18 | import org.xml.sax.SAXException; 19 | 20 | import test.constants.IConstants; 21 | 22 | public class NMapXmlHandlerTest implements IConstants { 23 | 24 | @Test 25 | public void basicTest() { 26 | 27 | String fileName = "nmap-xml/ms-vscan.xml" ; 28 | 29 | INMapRunHandler nmrh = new NMapRunHandlerImpl() ; 30 | NMapXmlHandler nmxh = new NMapXmlHandler( nmrh ) ; 31 | 32 | TestListener listener = new TestListener() ; 33 | 34 | NMapXmlHandler.addListener(listener) ; 35 | 36 | SAXParserFactory spf = SAXParserFactory.newInstance(); 37 | try { 38 | 39 | //get a new instance of parser 40 | SAXParser sp = spf.newSAXParser(); 41 | 42 | // get the ms-vscan.xml as a stream 43 | InputStream in = getClass().getClassLoader().getResourceAsStream( fileName ) ; 44 | 45 | //parse the file and also register this class for call backs 46 | sp.parse( in, nmxh ); 47 | 48 | }catch(SAXException se) { 49 | se.printStackTrace(); 50 | }catch(ParserConfigurationException pce) { 51 | pce.printStackTrace(); 52 | }catch (IOException ie) { 53 | ie.printStackTrace(); 54 | } 55 | 56 | System.out.println( "\n\n exec time: " + nmxh.getExecTime() + "ms" ) ; 57 | } 58 | 59 | 60 | private class TestListener implements NMap4JParserEventListener { 61 | 62 | // public static int count = 0 ; 63 | 64 | @Override 65 | public void parseEventNotification(ParserEvent event) { 66 | //System.out.println( "source = " + event.getEventSource() ) ; 67 | if( event.getPayload() == null ) { 68 | Assert.fail() ; 69 | } 70 | } 71 | 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/core/scans/IScanValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | * 35 | */ 36 | package org.nmap4j.core.scans; 37 | 38 | /** 39 | * Implementations of this interface allow users to validate the flags for a scan. 40 | * 41 | * @author jonsvede 42 | * 43 | */ 44 | public interface IScanValidator { 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/nmaprun/Verbose.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.nmaprun; 36 | 37 | public class Verbose { 38 | 39 | public final static String VERBOSE_TAG = "verbose" ; 40 | 41 | public final static String LEVEL_ATTR = "level" ; 42 | 43 | private long level ; 44 | 45 | public long getLevel() { 46 | return level; 47 | } 48 | 49 | public void setLevel(long level) { 50 | this.level = level; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/nmaprun/Debugging.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.nmaprun; 36 | 37 | public class Debugging { 38 | 39 | public static final String DEBUGGING_TAG = "debugging" ; 40 | 41 | public static final String LEVEL_TAG = "level" ; 42 | 43 | private long level ; 44 | 45 | public long getLevel() { 46 | return level; 47 | } 48 | 49 | public void setLevel(long level) { 50 | this.level = level; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/parser/events/ParserEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.parser.events; 36 | 37 | public class ParserEvent { 38 | 39 | private Object eventSource ; 40 | private Object payload ; 41 | 42 | public ParserEvent( Object source, Object data ) { 43 | eventSource = source ; 44 | payload = data ; 45 | } 46 | 47 | public Object getEventSource() { 48 | return eventSource; 49 | } 50 | 51 | public Object getPayload() { 52 | return payload; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/host/ports/ExtraPorts.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.host.ports; 36 | 37 | public class ExtraPorts { 38 | 39 | private String state ; 40 | private long count ; 41 | 42 | public String getState() { 43 | return state; 44 | } 45 | public void setState(String state) { 46 | this.state = state; 47 | } 48 | public long getCount() { 49 | return count; 50 | } 51 | public void setCount(long count) { 52 | this.count = count; 53 | } 54 | 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/host/IpIdSequence.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.host; 36 | 37 | public class IpIdSequence { 38 | 39 | private String aClass ; 40 | private String values ; 41 | 42 | public String getaClass() { 43 | return aClass; 44 | } 45 | public void setaClass(String aClass) { 46 | this.aClass = aClass; 47 | } 48 | public String getValues() { 49 | return values; 50 | } 51 | public void setValues(String values) { 52 | this.values = values; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/nmaprun/host/ports/extraports/ExtraReasons.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.nmaprun.host.ports.extraports; 36 | 37 | public class ExtraReasons { 38 | 39 | private String reason ; 40 | private long count ; 41 | 42 | public String getReason() { 43 | return reason; 44 | } 45 | public void setReason(String reason) { 46 | this.reason = reason; 47 | } 48 | public long getCount() { 49 | return count; 50 | } 51 | public void setCount(long count) { 52 | this.count = count; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/host/Distance.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | * 35 | */ 36 | package org.nmap4j.data.host; 37 | 38 | public class Distance { 39 | 40 | public final static String DISTANCE_TAG = "distance" ; 41 | 42 | public final static String VALUE_ATTR = "value" ; 43 | 44 | 45 | private long value ; 46 | 47 | public long getValue() { 48 | return value; 49 | } 50 | 51 | public void setValue(long value) { 52 | this.value = value; 53 | } 54 | 55 | @Override 56 | public String toString() { 57 | return "Distance [value=" + value + "]"; 58 | } 59 | 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/core/nmap/NMapExecutionException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | * 35 | */ 36 | package org.nmap4j.core.nmap; 37 | 38 | /** 39 | * Thrown when IOException occurs when calling execute() on NMapExecutor. 40 | * 41 | * @author jsvede 42 | * 43 | */ 44 | public class NMapExecutionException extends Exception { 45 | 46 | public NMapExecutionException() {} 47 | 48 | public NMapExecutionException(String message) { 49 | super( message ) ; 50 | } 51 | 52 | public NMapExecutionException(Throwable cause) { 53 | super( cause ) ; 54 | } 55 | 56 | public NMapExecutionException(String message, Throwable cause) { 57 | super( message, cause ) ; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/core/scans/HostDiscovery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.core.scans; 36 | 37 | import org.nmap4j.core.flags.Flag; 38 | 39 | /** 40 | * This is a convenience class that allows you to scan for hosts based on the 41 | * included/excluded hosts passed to the constructor. 42 | * 43 | * @author jsvede 44 | * 45 | */ 46 | public class HostDiscovery extends BaseScan { 47 | 48 | public HostDiscovery( String[] includeHosts, String[] excludeHosts ) { 49 | includeHosts( includeHosts ) ; 50 | excludeHosts( excludeHosts ) ; 51 | 52 | setTiming( IScan.TimingFlag.NORMAL ) ; 53 | 54 | getArgumentProperties().addFlag("-sn" ) ; 55 | 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/core/nmap/NMapInitializationException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | * 35 | */ 36 | package org.nmap4j.core.nmap; 37 | 38 | /** 39 | * This exception is thrown when the NMapExecutor is initialized and 40 | * the host is not one of the supported OS's. 41 | * 42 | * @author jsvede 43 | * 44 | */ 45 | public class NMapInitializationException extends Exception { 46 | 47 | public NMapInitializationException() {} 48 | 49 | public NMapInitializationException(String message) { 50 | super( message ) ; 51 | } 52 | 53 | public NMapInitializationException(Throwable cause) { 54 | super( cause ) ; 55 | } 56 | 57 | public NMapInitializationException(String message, Throwable cause) { 58 | super( message, cause ) ; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/nmaprun/hostnames/Hostname.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.nmaprun.hostnames; 36 | 37 | public class Hostname { 38 | 39 | public final static String HOSTNAME_TAG = "hostname" ; 40 | public final static String NAME_ATTR = "name" ; 41 | public final static String TYPE_ATTR = "type" ; 42 | 43 | private String name ; 44 | private String type ; 45 | 46 | public String getName() { 47 | return name; 48 | } 49 | public void setName(String name) { 50 | this.name = name; 51 | } 52 | public String getType() { 53 | return type; 54 | } 55 | public void setType(String type) { 56 | this.type = type; 57 | } 58 | @Override 59 | public String toString() { 60 | return "Hostname [name=" + name + ", type=" + type + "]"; 61 | } 62 | 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/nmaprun/RunStats.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.nmaprun; 36 | 37 | import org.nmap4j.data.nmaprun.runstats.Finished; 38 | import org.nmap4j.data.nmaprun.runstats.Hosts; 39 | 40 | public class RunStats { 41 | 42 | public final static String RUNSTATS_TAG = "runstats" ; 43 | 44 | 45 | private Hosts hosts ; 46 | private Finished finished ; 47 | 48 | public Hosts getHosts() { 49 | return hosts; 50 | } 51 | public void setHosts(Hosts hosts) { 52 | this.hosts = hosts; 53 | } 54 | public Finished getFinished() { 55 | return finished; 56 | } 57 | public void setFinished(Finished finished) { 58 | this.finished = finished; 59 | } 60 | @Override 61 | public String toString() { 62 | return "RunStats []"; 63 | } 64 | 65 | 66 | 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/host/Status.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.host; 36 | 37 | public class Status { 38 | 39 | public final static String STATUS_TAG = "status" ; 40 | public final static String STATE_ATTR = "state" ; 41 | public final static String REASON_ATTR = "reason" ; 42 | 43 | 44 | private String state ; 45 | private String reason ; 46 | 47 | public String getState() { 48 | return state; 49 | } 50 | public void setState(String state) { 51 | this.state = state; 52 | } 53 | public String getReason() { 54 | return reason; 55 | } 56 | public void setReason(String reason) { 57 | this.reason = reason; 58 | } 59 | 60 | @Override 61 | public String toString() { 62 | return "Status [reason=" + reason + ", state=" + state + "]"; 63 | } 64 | 65 | 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/core/scans/ParameterValidationFailureException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | * 35 | */ 36 | package org.nmap4j.core.scans; 37 | 38 | public class ParameterValidationFailureException extends Exception { 39 | 40 | public ParameterValidationFailureException() { 41 | super() ; 42 | // TODO Auto-generated constructor stub 43 | } 44 | 45 | public ParameterValidationFailureException(String message, Throwable cause) { 46 | super( message, cause ) ; 47 | // TODO Auto-generated constructor stub 48 | } 49 | 50 | public ParameterValidationFailureException(String message) { 51 | super( message ) ; 52 | // TODO Auto-generated constructor stub 53 | } 54 | 55 | public ParameterValidationFailureException(Throwable cause) { 56 | super( cause ) ; 57 | // TODO Auto-generated constructor stub 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/host/TcpTsSequence.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.host; 36 | 37 | public class TcpTsSequence { 38 | 39 | public final static String TCP_TS_SEQUENCE_TAG = "tcptssequence" ; 40 | 41 | public final static String CLASS_ATTR = "class" ; 42 | public final static String VALUES_ATTR = "values" ; 43 | 44 | private String aClass ; 45 | private String values ; 46 | 47 | public String getaClass() { 48 | return aClass; 49 | } 50 | public void setaClass(String aClass) { 51 | this.aClass = aClass; 52 | } 53 | public String getValues() { 54 | return values; 55 | } 56 | public void setValues(String values) { 57 | this.values = values; 58 | } 59 | @Override 60 | public String toString() { 61 | return "TcpTsSequence [class=" + aClass + ", values=" + values + "]"; 62 | } 63 | 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/host/Hostnames.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.host; 36 | 37 | import org.nmap4j.data.nmaprun.hostnames.Hostname; 38 | 39 | import java.util.ArrayList; 40 | import java.util.List; 41 | 42 | public class Hostnames { 43 | 44 | public final static String HOSTNAMES_TAG = "hostnames" ; 45 | 46 | private List hostnames = new ArrayList<>(); 47 | 48 | public List getHostnames() { 49 | return hostnames; 50 | } 51 | 52 | public void setHostnames(List hostnames) { 53 | this.hostnames = hostnames; 54 | } 55 | 56 | public void addHostname( Hostname hostname ) { 57 | if( !hostnames.contains( hostname ) ) { 58 | hostnames.add( hostname ) ; 59 | } 60 | } 61 | 62 | @Override 63 | public String toString() { 64 | return "Hostnames []"; 65 | } 66 | 67 | 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/host/Uptime.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.host; 36 | 37 | public class Uptime { 38 | 39 | public final static String UPTIME_TAG = "uptime" ; 40 | 41 | public final static String SECONDS_ATTR = "seconds" ; 42 | public final static String LASTBOOT_ATTR = "lastboot" ; 43 | 44 | 45 | private long seconds ; 46 | private String lastboot ; 47 | 48 | public long getSeconds() { 49 | return seconds; 50 | } 51 | public void setSeconds(long seconds) { 52 | this.seconds = seconds; 53 | } 54 | public String getLastboot() { 55 | return lastboot; 56 | } 57 | public void setLastboot(String lastboot) { 58 | this.lastboot = lastboot; 59 | } 60 | @Override 61 | public String toString() { 62 | return "Uptime [lastboot=" + lastboot + ", seconds=" + seconds + "]"; 63 | } 64 | 65 | 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/model/Port.java: -------------------------------------------------------------------------------- 1 | package org.nmap4j.model; 2 | 3 | /** 4 | * A class to flatten the port data structure that comes out of Nmap. 5 | * 6 | * 7 | * @author jon.svede 8 | * 9 | * 10 | * 11 | 12 | 19 | 20 | * 21 | */ 22 | public class Port { 23 | 24 | private String protocol ; 25 | private int portNumber ; 26 | private String state ; 27 | private String reason ; 28 | private String reasonTtl ; 29 | private String serviceName ; 30 | private String product ; 31 | private String ostype ; 32 | private String tunnel ; 33 | private String method ; 34 | private String conf ; 35 | 36 | public Port( String prtcl, int portNum ) { 37 | protocol = prtcl ; 38 | portNumber = portNum ; 39 | } 40 | 41 | public String getProtocol() { 42 | return protocol; 43 | } 44 | public void setProtocol(String protocol) { 45 | this.protocol = protocol; 46 | } 47 | public int getPortNumber() { 48 | return portNumber; 49 | } 50 | public void setPortNumber(int portNumber) { 51 | this.portNumber = portNumber; 52 | } 53 | public String getState() { 54 | return state; 55 | } 56 | public void setState(String state) { 57 | this.state = state; 58 | } 59 | public String getReason() { 60 | return reason; 61 | } 62 | public void setReason(String reason) { 63 | this.reason = reason; 64 | } 65 | public String getReasonTtl() { 66 | return reasonTtl; 67 | } 68 | public void setReasonTtl(String reasonTtl) { 69 | this.reasonTtl = reasonTtl; 70 | } 71 | public String getServiceName() { 72 | return serviceName; 73 | } 74 | public void setServiceName(String serviceName) { 75 | this.serviceName = serviceName; 76 | } 77 | public String getMethod() { 78 | return method; 79 | } 80 | public void setMethod(String method) { 81 | this.method = method; 82 | } 83 | public String getConf() { 84 | return conf; 85 | } 86 | public void setConf(String conf) { 87 | this.conf = conf; 88 | } 89 | public String getProduct() { 90 | return product; 91 | } 92 | public void setProduct(String product) { 93 | this.product = product; 94 | } 95 | public String getOstype() { 96 | return ostype; 97 | } 98 | public void setOstype(String ostype) { 99 | this.ostype = ostype; 100 | } 101 | public String getTunnel() { 102 | return tunnel; 103 | } 104 | public void setTunnel(String tunnel) { 105 | this.tunnel = tunnel; 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/host/Ports.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | * 35 | */ 36 | package org.nmap4j.data.host; 37 | 38 | import java.util.ArrayList; 39 | 40 | import org.nmap4j.data.host.ports.Port; 41 | 42 | 43 | public class Ports { 44 | 45 | public final static String PORTS_TAG = "ports" ; 46 | 47 | private ArrayList ports ; 48 | 49 | public Ports() { 50 | ports = new ArrayList() ; 51 | } 52 | public ArrayList getPorts() { 53 | return ports; 54 | } 55 | 56 | public void setPorts(ArrayList ports) { 57 | this.ports = ports; 58 | } 59 | 60 | public void addPort( Port p ) { 61 | if( !ports.contains( p ) ) { 62 | ports.add( p ) ; 63 | } 64 | } 65 | 66 | public void remove( Port p ) { 67 | if( ports.contains( p ) ) { 68 | ports.remove( p ) ; 69 | } 70 | } 71 | @Override 72 | public String toString() { 73 | return "Ports []"; 74 | } 75 | 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/host/Cpe.java: -------------------------------------------------------------------------------- 1 | package org.nmap4j.data.host; 2 | 3 | import java.util.StringTokenizer; 4 | 5 | /** 6 | * A representation of the Common Platform Enumeration (CPE) in Nmap output. 7 | *

8 | * The CPE data is documented here: 9 | *

10 | * http://nmap.org/book/output-formats-cpe.html 11 | *

12 | * But the important bits are that it is a colon delimited string. For example: 13 | *

14 | * <cpe>cpe:/o:apple:mac_os_x:10.7</cpe> 15 | *

16 | * This data is variable in length. This data can be a child of service or 17 | * osclass. The data is formatted as follows: 18 | * 19 | * cpe:/:::::: 20 | * 21 | * The part data is as follows: 22 | *

    23 | *
  • a - for applications, 24 | *
  • h - for hardware platforms, or 25 | *
  • o - for operating systems. 26 | *
27 | * 28 | * @author jon.svede 29 | * 30 | */ 31 | public class Cpe { 32 | 33 | public final static String CPE_ATTR = "cpe" ; 34 | 35 | private String cpeData ; 36 | 37 | private String part = null ; 38 | private String vendor = null ; 39 | private String product = null ; 40 | private String version = null ; 41 | private String update = null ; 42 | private String edition = null ; 43 | private String language = null ; 44 | 45 | public Cpe() {} 46 | 47 | public Cpe( String cpe ) { 48 | cpeData = cpe.substring( 5 ) ; 49 | parseData() ; 50 | } 51 | 52 | private void parseData() { 53 | String[] tokens = cpeData.split( ":" ) ; 54 | 55 | int tokenNumber = 0 ; 56 | 57 | //:::::: 58 | for( String token : tokens ) { 59 | switch( tokenNumber ) { 60 | case 0: 61 | part = token ; 62 | break ; 63 | case 1: 64 | vendor = token ; 65 | break ; 66 | case 2: 67 | product = token ; 68 | break ; 69 | case 3: 70 | version = token ; 71 | break ; 72 | case 4: 73 | update = token ; 74 | break ; 75 | case 5: 76 | edition = token ; 77 | break ; 78 | case 6: 79 | language = token ; 80 | break ; 81 | } 82 | tokenNumber++ ; 83 | } 84 | 85 | } 86 | public String getCpeData() { 87 | return cpeData; 88 | } 89 | 90 | public void setCpeData(String cpeData) { 91 | this.cpeData = cpeData; 92 | } 93 | 94 | public String getPart() { 95 | return part; 96 | } 97 | 98 | public String getVendor() { 99 | return vendor; 100 | } 101 | 102 | public String getProduct() { 103 | return product; 104 | } 105 | 106 | public String getVersion() { 107 | return version; 108 | } 109 | 110 | public String getUpdate() { 111 | return update; 112 | } 113 | 114 | public String getEdition() { 115 | return edition; 116 | } 117 | 118 | public String getLanguage() { 119 | return language; 120 | } 121 | 122 | 123 | 124 | } 125 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/nmaprun/runstats/Hosts.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.nmaprun.runstats; 36 | 37 | public class Hosts { 38 | 39 | public final static String HOSTS_TAG = "hosts" ; 40 | 41 | public final static String UP_ATTR = "up" ; 42 | public final static String DOWN_ATTR = "down" ; 43 | public final static String TOTAL_ATTR = "total" ; 44 | 45 | private long up ; 46 | private long down ; 47 | private long total ; 48 | 49 | public long getUp() { 50 | return up; 51 | } 52 | public void setUp(long up) { 53 | this.up = up; 54 | } 55 | public long getDown() { 56 | return down; 57 | } 58 | public void setDown(long down) { 59 | this.down = down; 60 | } 61 | public long getTotal() { 62 | return total; 63 | } 64 | public void setTotal(long total) { 65 | this.total = total; 66 | } 67 | 68 | @Override 69 | public String toString() { 70 | return "Hosts [down=" + down + ", total=" + total + ", up=" + up + "]"; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/host/Times.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.host; 36 | 37 | public class Times { 38 | 39 | public final static String TIMES_TAG = "times" ; 40 | 41 | public final static String SRTT_ATTR = "srtt" ; 42 | public final static String RTTVAR_ATTR = "rttvar" ; 43 | public final static String TO_ATTR = "to" ; 44 | 45 | private long srtt ; 46 | private long rttvar ; 47 | private long to ; 48 | 49 | public long getSrtt() { 50 | return srtt; 51 | } 52 | public void setSrtt(long srtt) { 53 | this.srtt = srtt; 54 | } 55 | public long getRttvar() { 56 | return rttvar; 57 | } 58 | public void setRttvar(long rttvar) { 59 | this.rttvar = rttvar; 60 | } 61 | public long getTo() { 62 | return to; 63 | } 64 | public void setTo(long to) { 65 | this.to = to; 66 | } 67 | @Override 68 | public String toString() { 69 | return "Times [rttvar=" + rttvar + ", srtt=" + srtt + ", to=" + to 70 | + "]"; 71 | } 72 | 73 | 74 | 75 | 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/host/os/OsMatch.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.host.os; 36 | 37 | public class OsMatch { 38 | 39 | public final static String OS_MATCH_TAG = "osmatch" ; 40 | 41 | public final static String NAME_ATTR = "name" ; 42 | public final static String ACCURACY_ATTR = "accuracy" ; 43 | public final static String LINE_ATTR = "line" ; 44 | 45 | private String name ; 46 | private String accuracy ; 47 | private String line ; 48 | 49 | public String getName() { 50 | return name; 51 | } 52 | public void setName(String name) { 53 | this.name = name; 54 | } 55 | public String getAccuracy() { 56 | return accuracy; 57 | } 58 | public void setAccuracy(String accuracy) { 59 | this.accuracy = accuracy; 60 | } 61 | public String getLine() { 62 | return line; 63 | } 64 | public void setLine(String line) { 65 | this.line = line; 66 | } 67 | @Override 68 | public String toString() { 69 | return "OsMatch [accuracy=" + accuracy + ", line=" + line + ", name=" 70 | + name + "]"; 71 | } 72 | 73 | 74 | } -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/host/os/PortUsed.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.host.os; 36 | 37 | public class PortUsed { 38 | 39 | public final static String PORT_USED_TAG = "portused" ; 40 | 41 | public final static String STATE_ATTR = "state" ; 42 | public final static String PROTO_ATTR = "proto" ; 43 | public final static String PORTID_ATTR = "portid" ; 44 | 45 | private String state ; 46 | private String proto ; 47 | private String portid ; 48 | 49 | public String getState() { 50 | return state; 51 | } 52 | public void setState(String state) { 53 | this.state = state; 54 | } 55 | public String getProto() { 56 | return proto; 57 | } 58 | public void setProto(String proto) { 59 | this.proto = proto; 60 | } 61 | public String getPortid() { 62 | return portid; 63 | } 64 | public void setPortid(String portid) { 65 | this.portid = portid; 66 | } 67 | @Override 68 | public String toString() { 69 | return "PortUsed [portid=" + portid + ", proto=" + proto + ", state=" 70 | + state + "]"; 71 | } 72 | 73 | 74 | } -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/host/Address.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.host; 36 | 37 | public class Address { 38 | 39 | public final static String ADDRESS_TAG = "address" ; 40 | 41 | public final static String ADDR_ATTR = "addr" ; 42 | public final static String ADDRTYPE_ATTR = "addrtype" ; 43 | public final static String VENDOR_ATTR = "vendor" ; 44 | 45 | private String addr ; 46 | private String addrtype ; 47 | private String vendor ; 48 | 49 | public String getAddr() { 50 | return addr; 51 | } 52 | public void setAddr(String addr) { 53 | this.addr = addr; 54 | } 55 | public String getAddrtype() { 56 | return addrtype; 57 | } 58 | public void setAddrtype(String addrtype) { 59 | this.addrtype = addrtype; 60 | } 61 | public String getVendor() { 62 | return vendor; 63 | } 64 | public void setVendor(String vendor) { 65 | this.vendor = vendor; 66 | } 67 | @Override 68 | public String toString() { 69 | return "Address [addr=" + addr + ", addrtype=" + addrtype + ", vendor=" 70 | + vendor + "]"; 71 | } 72 | 73 | 74 | 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/nmaprun/runstats/Finished.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.nmaprun.runstats; 36 | 37 | public class Finished { 38 | 39 | public final static String FINISHED_TAG = "finished" ; 40 | 41 | public final static String TIME_ATTR = "time" ; 42 | public final static String TIMESTR_ATTR = "timestr" ; 43 | public final static String ELAPSED_ATTR = "elapsed" ; 44 | 45 | private String time ; 46 | private String timestr ; 47 | private String elapsed ; 48 | public String getTime() { 49 | return time; 50 | } 51 | public void setTime(String time) { 52 | this.time = time; 53 | } 54 | public String getTimestr() { 55 | return timestr; 56 | } 57 | public void setTimestr(String timestr) { 58 | this.timestr = timestr; 59 | } 60 | public String getElapsed() { 61 | return elapsed; 62 | } 63 | public void setElapsed(String elapsed) { 64 | this.elapsed = elapsed; 65 | } 66 | @Override 67 | public String toString() { 68 | return "Finished [elapsed=" + elapsed + ", time=" + time + ", timestr=" 69 | + timestr + "]"; 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 4.0.0 5 | 6 | org.nmap4j 7 | nmap4j 8 | 1.0.4 9 | Nmap4j 10 | 11 | 12 | 1.8 13 | 1.8 14 | 4.13.1 15 | 1.3.2 16 | 32.0.0-jre 17 | 3.8.1 18 | 1.8 19 | 1.8 20 | 2.22.2 21 | 22 | 23 | 24 | 25 | 26 | junit 27 | junit 28 | ${junit.version} 29 | test 30 | 31 | 32 | 33 | 34 | org.apache.commons 35 | commons-io 36 | ${commons-io.version} 37 | 38 | 39 | 40 | 41 | com.google.guava 42 | guava 43 | ${guava.version} 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | org.apache.maven.plugins 52 | maven-compiler-plugin 53 | ${maven-compiler-plugin.version} 54 | 55 | ${maven-compiler-plugin.source} 56 | ${maven-compiler-plugin.target} 57 | 58 | 59 | 60 | 61 | 62 | org.apache.maven.plugins 63 | maven-surefire-plugin 64 | ${maven-surefire-plugin.version} 65 | 66 | 1 67 | false 68 | -Xms1024m -Xmx1024m 69 | false 70 | 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/nmaprun/host/ports/port/State.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.nmaprun.host.ports.port; 36 | 37 | public class State { 38 | 39 | public final static String STATE_TAG = "state" ; 40 | 41 | public final static String STATE_ATTR = "state" ; 42 | public final static String REASON_ATTR = "reason" ; 43 | public final static String REASON_TTL_ATTR = "reason_ttl" ; 44 | 45 | private String state ; 46 | private String reason ; 47 | private long reason_ttl ; 48 | 49 | public String getState() { 50 | return state; 51 | } 52 | public void setState(String state) { 53 | this.state = state; 54 | } 55 | public String getReason() { 56 | return reason; 57 | } 58 | public void setReason(String reason) { 59 | this.reason = reason; 60 | } 61 | public long getReason_ttl() { 62 | return reason_ttl; 63 | } 64 | public void setReason_ttl(long reasonTtl) { 65 | reason_ttl = reasonTtl; 66 | } 67 | @Override 68 | public String toString() { 69 | return "State [reason=" + reason + ", reason_ttl=" + reason_ttl 70 | + ", state=" + state + "]"; 71 | } 72 | 73 | 74 | 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/host/TcpSequence.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.host; 36 | 37 | public class TcpSequence { 38 | 39 | public final static String TCP_SEQUENCE_TAG = "tcpsequence" ; 40 | 41 | public final static String INDEX_ATTR = "index" ; 42 | public final static String DIFFICULTY_ATTR = "difficulty" ; 43 | public final static String VALUES_ATTR = "values" ; 44 | 45 | private long index ; 46 | private String difficulty ; 47 | private String values ; 48 | 49 | public long getIndex() { 50 | return index; 51 | } 52 | public void setIndex(long index) { 53 | this.index = index; 54 | } 55 | public String getDifficulty() { 56 | return difficulty; 57 | } 58 | public void setDifficulty(String difficulty) { 59 | this.difficulty = difficulty; 60 | } 61 | public String getValues() { 62 | return values; 63 | } 64 | public void setValues(String values) { 65 | this.values = values; 66 | } 67 | @Override 68 | public String toString() { 69 | return "TcpSequence [difficulty=" + difficulty + ", index=" + index 70 | + ", values=" + values + "]"; 71 | } 72 | 73 | 74 | 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/parser/util/BogusIPGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.parser.util; 36 | 37 | import java.util.Random; 38 | 39 | 40 | /** 41 | * This is a utility class that generates bogus IP addresses that are out of 42 | * range actually. The generated values have nothing to do with an input. 43 | * 44 | * @author jsvede 45 | * 46 | */ 47 | public class BogusIPGenerator { 48 | 49 | private static Random random ; 50 | 51 | private static int base = 255 ; 52 | 53 | private static String DOT = "." ; 54 | 55 | private static Random getRandom() { 56 | if( random == null ) { 57 | random = new Random() ; 58 | } 59 | return random ; 60 | } 61 | 62 | public static int genIpNode() { 63 | 64 | int nextRandom = getRandom().nextInt( 744 ) ; 65 | 66 | return nextRandom + base ; 67 | } 68 | 69 | public static String getNodeAsString( int node ) { 70 | return Integer.toString( node ) ; 71 | } 72 | 73 | public static String generateBadIpAddress() { 74 | StringBuilder addrBuilder = new StringBuilder() ; 75 | for( int x=0; x<4; x++ ) { 76 | addrBuilder.append( getNodeAsString( genIpNode() ) ) ; 77 | if( x != 3 ) { 78 | addrBuilder.append( DOT ) ; 79 | } 80 | 81 | } 82 | return addrBuilder.toString(); 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/host/ports/Port.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.host.ports; 36 | 37 | import org.nmap4j.data.nmaprun.host.ports.port.Service; 38 | import org.nmap4j.data.nmaprun.host.ports.port.State; 39 | 40 | public class Port { 41 | 42 | public final static String PORT_TAG = "port" ; 43 | 44 | public final static String PROTOCOL_ATTR = "protocol" ; 45 | public final static String PORTID_ATTR = "portid" ; 46 | 47 | 48 | private String protocol ; 49 | private long portId ; 50 | 51 | private State state ; 52 | private Service service ; 53 | 54 | public String getProtocol() { 55 | return protocol; 56 | } 57 | public void setProtocol(String protocol) { 58 | this.protocol = protocol; 59 | } 60 | public long getPortId() { 61 | return portId; 62 | } 63 | public void setPortId(long portId) { 64 | this.portId = portId; 65 | } 66 | public State getState() { 67 | return state; 68 | } 69 | public void setState(State state) { 70 | this.state = state; 71 | } 72 | public Service getService() { 73 | return service; 74 | } 75 | public void setService(Service service) { 76 | this.service = service; 77 | } 78 | @Override 79 | public String toString() { 80 | return "Port [portId=" + portId + ", protocol=" + protocol + ",service=" + service + "]"; 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /src/test/java/org/nmap4j/core/nmap/NMapExecutorTest.java: -------------------------------------------------------------------------------- 1 | package org.nmap4j.core.nmap; 2 | 3 | import static org.junit.Assert.fail; 4 | 5 | import org.junit.After; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | import org.nmap4j.core.flags.ArgumentProperties; 9 | import org.nmap4j.core.flags.Flag; 10 | import org.nmap4j.core.nmap.ExecutionResults; 11 | import org.nmap4j.core.nmap.NMapExecutionException; 12 | import org.nmap4j.core.nmap.NMapExecutor; 13 | import org.nmap4j.core.nmap.NMapInitializationException; 14 | import org.nmap4j.core.nmap.NMapProperties; 15 | 16 | public class NMapExecutorTest { 17 | 18 | private ArgumentProperties nmapArgs ; 19 | private NMapProperties nmapProps ; 20 | 21 | @Before 22 | public void setup() { 23 | nmapArgs = new ArgumentProperties() ; 24 | nmapProps = new NMapProperties() ; 25 | } 26 | 27 | @After 28 | public void teardown() { 29 | nmapArgs = null ; 30 | nmapProps = null ; 31 | } 32 | 33 | /** 34 | * This tests whether or not we fail with an exception when we try 35 | * to instantiate the object with nulls which isn't a valide use case. 36 | */ 37 | @Test 38 | public void testConstructionWithNulls() { 39 | boolean hasFailed = false ; 40 | // expect this to throw an exception 41 | try { 42 | NMapExecutor nmapExec = new NMapExecutor( null, null ) ; 43 | } catch (NMapInitializationException e) { 44 | // this is what I would expect to happen 45 | hasFailed = true ; 46 | } 47 | 48 | if( !hasFailed ) { 49 | fail() ; 50 | } 51 | } 52 | 53 | /** 54 | * This test verifies that the initialization fails due to there being 55 | * no path to nmap. 56 | */ 57 | @Test 58 | public void testConstructionWithNoPath() { 59 | boolean hasFailed = false ; 60 | // expect this to throw an exception 61 | try { 62 | NMapExecutor nmapExec = new NMapExecutor( nmapArgs, nmapProps ) ; 63 | } catch (NMapInitializationException e) { 64 | // this is what I would expect to happen 65 | hasFailed = true ; 66 | } 67 | 68 | if( !hasFailed ) { 69 | fail() ; 70 | } 71 | } 72 | 73 | @Test 74 | public void testZeroLengthNMapPath() { 75 | nmapProps.setPath( "" ) ; 76 | 77 | boolean hasFailed = false ; 78 | // expect this to throw an exception 79 | try { 80 | NMapExecutor nmapExec = new NMapExecutor( nmapArgs, nmapProps ) ; 81 | } catch (NMapInitializationException e) { 82 | // this is what I would expect to happen 83 | hasFailed = true ; 84 | } 85 | 86 | if( !hasFailed ) { 87 | fail() ; 88 | } 89 | } 90 | 91 | @Test 92 | public void testExecuteMethodWithoutSudo() { 93 | 94 | nmapProps.setPath( "/usr/local" ) ; 95 | 96 | nmapArgs.addFlag( Flag.AGGRESIVE_TIMING ) ; 97 | nmapArgs.addFlag( Flag.VERSION ) ; 98 | nmapArgs.addFlag( Flag.OS_DETECTION ) ; 99 | 100 | nmapArgs.addIncludedHost( "localhost" ) ; 101 | 102 | try { 103 | NMapExecutor nmapExec = new NMapExecutor( nmapArgs, nmapProps ) ; 104 | ExecutionResults results = nmapExec.execute() ; 105 | System.out.println( results.getOutput() ) ; 106 | } catch (NMapInitializationException e) { 107 | e.printStackTrace() ; 108 | fail() ; 109 | } catch (NMapExecutionException e) { 110 | e.printStackTrace() ; 111 | fail() ; 112 | } 113 | } 114 | 115 | } 116 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/nmaprun/ScanInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.nmaprun; 36 | 37 | public class ScanInfo { 38 | 39 | public final static String SCANINFO_TAG = "scaninfo" ; 40 | 41 | public final static String TYPE_ATTR = "type" ; 42 | public final static String PROTOCOL_ATTR = "protocol" ; 43 | public final static String NUM_SERVICES_ATTR = "numservices" ; 44 | public final static String SERVICES_ATTR = "services" ; 45 | 46 | private String type ; 47 | private String protocol ; 48 | private long numservices ; 49 | private String services ; 50 | 51 | public String getType() { 52 | return type; 53 | } 54 | public void setType(String type) { 55 | this.type = type; 56 | } 57 | public String getProtocol() { 58 | return protocol; 59 | } 60 | public void setProtocol(String protocol) { 61 | this.protocol = protocol; 62 | } 63 | public long getNumservices() { 64 | return numservices; 65 | } 66 | public void setNumservices(long numservices) { 67 | this.numservices = numservices; 68 | } 69 | public String getServices() { 70 | return services; 71 | } 72 | public void setServices(String services) { 73 | this.services = services; 74 | } 75 | @Override 76 | public String toString() { 77 | return "ScanInfo [numservices=" + numservices + ", protocol=" 78 | + protocol + ", services=" + services + ", type=" + type + "]"; 79 | } 80 | 81 | 82 | 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/parser/util/UidGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.parser.util; 36 | 37 | /** 38 | * Utility class that generates random Strings. 39 | * 40 | * @author jsvede 41 | * 42 | */ 43 | public class UidGenerator { 44 | 45 | public static String getBigId(String ident) { 46 | String rs = "" + Math.random(); 47 | rs = rs.substring(rs.indexOf(".") + 1); 48 | int end = rs.indexOf("E"); 49 | if (end == -1) { 50 | end = rs.length(); 51 | } 52 | rs = rs.substring(0, end); 53 | long last2 = (long) System.currentTimeMillis(); 54 | String s = ident + "-" 55 | + java.math.BigInteger.valueOf(last2).toString(36) 56 | + new java.math.BigInteger(rs).toString(36); 57 | 58 | return s.toUpperCase(); 59 | } 60 | 61 | public static String getSmallId(String ident) { 62 | String rs = "" + Math.random(); 63 | rs = rs.substring(rs.indexOf(".") + 1); 64 | int end = rs.indexOf("E"); 65 | 66 | if (end == -1) { 67 | end = rs.length(); 68 | } 69 | rs = rs.substring(0, end); 70 | long last2 = (long) System.currentTimeMillis(); 71 | java.math.BigInteger bi1 = java.math.BigInteger.valueOf(last2); 72 | java.math.BigInteger bi2 = new java.math.BigInteger(rs); 73 | 74 | java.math.BigInteger bi3 = bi1.divide(java.math.BigInteger 75 | .valueOf(100000l)); 76 | java.math.BigInteger bi4 = bi2.divide(java.math.BigInteger 77 | .valueOf(100000000l)); 78 | 79 | bi3.abs(); 80 | bi4.abs(); 81 | 82 | String s = ident + bi3.toString(36) + bi4.toString(36); 83 | 84 | return s.toUpperCase(); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/core/scans/NMapExecutorThread.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | * 35 | */ 36 | package org.nmap4j.core.scans; 37 | 38 | import org.nmap4j.core.flags.ArgumentProperties ; 39 | import org.nmap4j.core.nmap.ExecutionResults ; 40 | import org.nmap4j.core.nmap.NMapExecutionException ; 41 | import org.nmap4j.core.nmap.NMapExecutor ; 42 | import org.nmap4j.core.nmap.NMapInitializationException ; 43 | import org.nmap4j.core.nmap.NMapProperties ; 44 | 45 | /** 46 | * An implementation of the Runnable interface capable of running an 47 | * NMapExecutor instance. 48 | *

49 | * This facilitates running NMap asynchronously. 50 | * s 51 | * @author jsvede 52 | * 53 | */ 54 | public class NMapExecutorThread implements Runnable { 55 | 56 | private IScanCallback callback ; 57 | 58 | private ArgumentProperties nmapArguments ; 59 | private NMapProperties nmapProperties ; 60 | 61 | public NMapExecutorThread( ArgumentProperties ap, NMapProperties nmp ) { 62 | nmapArguments = ap ; 63 | nmapProperties = nmp ; 64 | } 65 | 66 | public void setCallback( IScanCallback cBack ) { 67 | callback = cBack ; 68 | } 69 | 70 | @Override 71 | public void run() { 72 | try { 73 | NMapExecutor executor = new NMapExecutor( nmapArguments, nmapProperties ) ; 74 | ExecutionResults results = executor.execute() ; 75 | callback.executionCompleted( results ) ; 76 | } catch ( NMapInitializationException e ) { 77 | // TODO Auto-generated catch block 78 | e.printStackTrace(); 79 | } catch ( NMapExecutionException e ) { 80 | // TODO Auto-generated catch block 81 | e.printStackTrace(); 82 | } 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/core/scans/ServiceDiscovery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | * 35 | */ 36 | package org.nmap4j.core.scans; 37 | 38 | import org.nmap4j.core.flags.Flag ; 39 | 40 | /** 41 | * A ServiceDiscovery instance is used to scan a host or hosts to list all the 42 | * services the host is running, using NMap's default port specification. The 43 | * default port scan specification is to scan the top 1000 ports based on NMap's 44 | * own research of what are the most common open ports. You can override this 45 | * by providing your own port specification or instead select to 46 | * --top-ports (Flag.TOP_PORTS) or the --port-ratio 47 | * (Flag.PORT_RATIO) settings. 48 | *

49 | * This class is a convenience class that simplifies setting up a scan. The 50 | * simplest way to scan a host or set of hosts is as follows: 51 | *

52 | * 53 | * String[] includedHosts = { "192.168.1.1-255" } ;
54 | * ServiceDiscovery serviceDiscovery = 55 | * new ServiceDiscovery( includedHosts, null ) ;
56 | * ExecutionResults results = serviceDiscovery.execute();
57 | *
58 | *

59 | * This example is the simplest way to scan a set of hosts. NMap will let you 60 | * specify a range of hosts either as noted above or as a space delimited list. 61 | *

62 | * 63 | */ 64 | public class ServiceDiscovery extends BaseScan { 65 | 66 | public ServiceDiscovery( String[] includeHosts, String[] excludeHosts ) { 67 | includeHosts( includeHosts ) ; 68 | excludeHosts( excludeHosts ) ; 69 | 70 | setTiming( IScan.TimingFlag.NORMAL ) ; 71 | 72 | argProps.addFlag( Flag.SERVICE_VERSION ) ; 73 | argProps.addFlag( Flag.OS_DETECTION ) ; 74 | argProps.addFlag( Flag.XML_OUTPUT, "-" ) ; 75 | } 76 | 77 | 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/host/Os.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.host; 36 | 37 | import java.util.ArrayList; 38 | 39 | import org.nmap4j.data.host.os.OsClass; 40 | import org.nmap4j.data.host.os.OsMatch; 41 | import org.nmap4j.data.host.os.PortUsed; 42 | 43 | 44 | public class Os { 45 | 46 | public final static String OS_TAG = "os" ; 47 | 48 | private ArrayList portUseds ; 49 | private ArrayList osMatches ; 50 | private ArrayList osClasses ; 51 | 52 | public Os() { 53 | portUseds = new ArrayList() ; 54 | osMatches = new ArrayList() ; 55 | osClasses = new ArrayList() ; 56 | } 57 | 58 | public ArrayList getPortUseds() { 59 | return portUseds; 60 | } 61 | public void setPortUseds(ArrayList portUseds) { 62 | this.portUseds = portUseds; 63 | } 64 | public ArrayList getOsMatches() { 65 | return osMatches; 66 | } 67 | public void setOsMatches(ArrayList osMatches) { 68 | this.osMatches = osMatches; 69 | } 70 | public boolean addOsMatch(OsMatch o) { 71 | return osMatches.add(o); 72 | } 73 | public boolean removeOsMatch(OsMatch o) { 74 | return osMatches.remove(o); 75 | } 76 | public boolean addPortUsed(PortUsed o) { 77 | return portUseds.add(o); 78 | } 79 | public boolean removePortUsed(PortUsed o) { 80 | return portUseds.remove(o); 81 | } 82 | public ArrayList getOsClasses() { 83 | return osClasses; 84 | } 85 | public void setOsClasses(ArrayList osClasses) { 86 | this.osClasses = osClasses; 87 | } 88 | public boolean addOsClass(OsClass o) { 89 | return osClasses.add(o); 90 | } 91 | public boolean removeOsClass(OsClass o) { 92 | return osClasses.remove(o); 93 | } 94 | @Override 95 | public String toString() { 96 | return "Os []"; 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/host/os/OsClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.host.os; 36 | 37 | import java.util.ArrayList; 38 | 39 | import org.nmap4j.data.host.Cpe; 40 | 41 | public class OsClass { 42 | 43 | public final static String OSCLASS_TAG = "osclass" ; 44 | 45 | public final static String TYPE_ATTR = "type" ; 46 | public final static String VENDOR_ATTR = "vendor" ; 47 | public final static String OSFAMILY_ATTR = "osfamily" ; 48 | public final static String OSGEN_ATTR = "osgen" ; 49 | public final static String ACCURACY_ATTR = "accuracy" ; 50 | 51 | private String type ; 52 | private String vendor ; 53 | private String osfamily ; 54 | private String osgen ; 55 | private String accuracy ; 56 | private ArrayList cpe ; 57 | 58 | public OsClass() { 59 | cpe = new ArrayList() ; 60 | } 61 | 62 | public String getType() { 63 | return type; 64 | } 65 | public void setType(String type) { 66 | this.type = type; 67 | } 68 | public String getVendor() { 69 | return vendor; 70 | } 71 | public void setVendor(String vendor) { 72 | this.vendor = vendor; 73 | } 74 | public String getOsfamily() { 75 | return osfamily; 76 | } 77 | public void setOsfamily(String osfamily) { 78 | this.osfamily = osfamily; 79 | } 80 | public String getOsgen() { 81 | return osgen; 82 | } 83 | public void setOsgen(String osgen) { 84 | this.osgen = osgen; 85 | } 86 | public String getAccuracy() { 87 | return accuracy; 88 | } 89 | public void setAccuracy(String accuracy) { 90 | this.accuracy = accuracy; 91 | } 92 | public ArrayList getCpe() { 93 | return cpe; 94 | } 95 | public void setCpe(ArrayList cpe) { 96 | this.cpe = cpe; 97 | } 98 | public void addCpe( Cpe cpe ) { 99 | this.cpe.add( cpe ) ; 100 | } 101 | 102 | @Override 103 | public String toString() { 104 | return "OsClass [accuracy=" + accuracy + ", osfamily=" + osfamily 105 | + ", osgen=" + osgen + ", type=" + type + ", vendor=" + vendor 106 | + "]"; 107 | } 108 | 109 | } 110 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/nmaprun/host/ports/port/Service.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.nmaprun.host.ports.port; 36 | 37 | public class Service { 38 | 39 | public final static String SERVICE_TAG = "service" ; 40 | 41 | public final static String NAME_ATTR = "name" ; 42 | public final static String PRODUCT_ATTR = "product" ; 43 | public final static String VERSION_ATTR = "version" ; 44 | public final static String METHOD_ATTR = "method" ; 45 | public final static String CONF_ATTR = "conf" ; 46 | public final static String OS_TYPE_ATTR = "ostype" ; 47 | public final static String EXTRA_INFO_ATTR = "extrainfo" ; 48 | 49 | private String name ; 50 | private String product ; 51 | private String version ; 52 | private String method ; 53 | private String conf ; 54 | private String osType ; 55 | private String extrainfo ; 56 | 57 | 58 | 59 | public String getOsType() { 60 | return osType; 61 | } 62 | public void setOsType(String osType) { 63 | this.osType = osType; 64 | } 65 | public String getExtrainfo() { 66 | return extrainfo; 67 | } 68 | public void setExtrainfo(String extrainfo) { 69 | this.extrainfo = extrainfo; 70 | } 71 | public String getName() { 72 | return name; 73 | } 74 | public void setName(String name) { 75 | this.name = name; 76 | } 77 | public String getProduct() { 78 | return product; 79 | } 80 | public void setProduct(String product) { 81 | this.product = product; 82 | } 83 | public String getVersion() { 84 | return version; 85 | } 86 | public void setVersion(String version) { 87 | this.version = version; 88 | } 89 | public String getMethod() { 90 | return method; 91 | } 92 | public void setMethod(String method) { 93 | this.method = method; 94 | } 95 | public String getConf() { 96 | return conf; 97 | } 98 | public void setConf(String conf) { 99 | this.conf = conf; 100 | } 101 | @Override 102 | public String toString() { 103 | return "Service [conf=" + conf + ", extrainfo=" + extrainfo 104 | + ", method=" + method + ", name=" + name + ", osType=" 105 | + osType + ", product=" + product + ", version=" + version 106 | + "]"; 107 | } 108 | 109 | 110 | 111 | } 112 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/core/nmap/ExecutionResults.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.core.nmap; 36 | 37 | /** 38 | * This class contains the various outputs from an execution of nmap. This 39 | * includes the stdout and stderr. 40 | *

41 | * In general it's not entirely possible for this framework to detect or handle 42 | * every failure. Therefore it's always best to check the contents of stderr 43 | * before wondering why there are no results. 44 | * 45 | * @author jonsvede 46 | * 47 | */ 48 | public class ExecutionResults { 49 | 50 | private String errors; 51 | private String output; 52 | private String executedCommand; 53 | 54 | public ExecutionResults() {} 55 | 56 | /** 57 | * Create a new ExecutionResults object from the std out and std err 58 | * from the nmap execution. 59 | * 60 | * @param err 61 | * @param out 62 | */ 63 | public ExecutionResults(String err, String out) { 64 | errors = err; 65 | output = out; 66 | } 67 | 68 | /** 69 | * Returns the error stream from the nmap execution. 70 | * @return 71 | */ 72 | public String getErrors() { 73 | return errors; 74 | } 75 | 76 | /** 77 | * Sets the value for the error stream. 78 | * 79 | * @param errors 80 | */ 81 | public void setErrors(String errors) { 82 | this.errors = errors; 83 | } 84 | 85 | /** 86 | * Returns the value from the output stream from the NMap execution. 87 | * @return 88 | */ 89 | public String getOutput() { 90 | return output; 91 | } 92 | 93 | /** 94 | * Sets the value for the std out. 95 | * @param output 96 | */ 97 | public void setOutput(String output) { 98 | this.output = output; 99 | } 100 | 101 | /** 102 | * Returns true if the errors String is not null and it's length is 103 | * greater than 0. 104 | * @return 105 | */ 106 | public boolean hasErrors() { 107 | if (errors != null && errors.length() > 0) { 108 | return true; 109 | } 110 | return false; 111 | } 112 | 113 | /** 114 | * Returns the command executed to collect these outputs. 115 | * @return 116 | */ 117 | public String getExecutedCommand() { 118 | return executedCommand; 119 | } 120 | 121 | /** 122 | * Used to set the executed command. 123 | * @param command 124 | */ 125 | public void setExecutedCommand(String command) { 126 | executedCommand = command; 127 | } 128 | 129 | } 130 | -------------------------------------------------------------------------------- /src/test/java/org/nmap4j/core/flags/ArgumentPropertiesTest.java: -------------------------------------------------------------------------------- 1 | package org.nmap4j.core.flags; 2 | 3 | import static org.junit.Assert.*; 4 | import static org.junit.Assert.assertEquals; 5 | import static org.junit.Assert.assertTrue; 6 | 7 | import java.util.LinkedHashMap; 8 | import java.util.Set; 9 | 10 | import org.junit.After; 11 | import org.junit.Before; 12 | import org.junit.Test; 13 | import org.nmap4j.core.flags.ArgumentProperties; 14 | import org.nmap4j.core.flags.Flag; 15 | 16 | public class ArgumentPropertiesTest { 17 | 18 | private ArgumentProperties fixture ; 19 | 20 | @Before 21 | public void setUp() { 22 | fixture = new ArgumentProperties() ; 23 | } 24 | 25 | @After 26 | public void tearDown() { 27 | fixture = null ; 28 | } 29 | 30 | @Test 31 | public void testAddFlagOneFlagArg() { 32 | fixture.addFlag( Flag.VERSION_ALL ) ; 33 | 34 | LinkedHashMap flags = fixture.getFlagMap() ; 35 | 36 | assertTrue( flags != null ) ; 37 | assertTrue( flags.size() == 1 ) ; 38 | assertTrue( flags.containsKey( Flag.VERSION_ALL.toString()) ) ; 39 | 40 | } 41 | 42 | @Test 43 | public void testAddFlagFlagString() { 44 | fixture.addFlag( Flag.PORT_SPEC, "80,443" ) ; 45 | 46 | LinkedHashMap flags = fixture.getFlagMap() ; 47 | 48 | assertTrue( flags != null ) ; 49 | assertTrue( flags.size() == 1 ) ; 50 | assertTrue( flags.containsKey( Flag.PORT_SPEC.toString() ) ) ; 51 | assertTrue( flags.get(Flag.PORT_SPEC.toString()).equals( "80,443" ) ) ; 52 | } 53 | 54 | @Test 55 | public void testAddFlagTwoStrings() { 56 | fixture.addFlag( Flag.PORT_SPEC.toString(), "80,443" ) ; 57 | fixture.addFlag( Flag.PORT_SPEC, "7001,8001" ) ; 58 | 59 | LinkedHashMap flags = fixture.getFlagMap() ; 60 | 61 | assertTrue( flags != null ) ; 62 | assertTrue( flags.size() == 1 ) ; 63 | 64 | assertTrue( flags.containsKey( Flag.PORT_SPEC.toString() ) ) ; 65 | assertTrue( flags.get(Flag.PORT_SPEC.toString()).equals( "80,443,7001,8001" ) ) ; 66 | 67 | } 68 | 69 | @Test 70 | public void testGetFlags() { 71 | fixture.addFlag( Flag.PORT_SPEC, "80,443" ) ; 72 | fixture.addFlag( Flag.VERBOSE ) ; 73 | fixture.addFlag( Flag.INSANE_TIMING ) ; 74 | 75 | fixture.addIncludedHost( "192.168.1.1" ) ; 76 | fixture.addExcludedHost( "192.168.1.2" ) ; 77 | 78 | String flags = fixture.getFlags() ; 79 | 80 | assertTrue( flags.contains( "-p 80,443 -v -T5" ) ) ; 81 | assertTrue( flags.contains( "--exclude 192.168.1.2" ) ) ; 82 | assertTrue( flags.contains( "192.168.1.1" ) ) ; 83 | } 84 | 85 | @Test 86 | public void testIncludeHosts() { 87 | fixture.addIncludedHost( "localhost" ) ; 88 | 89 | Set hosts = fixture.getIncludedHosts() ; 90 | 91 | assertNotNull( hosts ) ; 92 | assertTrue( hosts.contains( "localhost" ) ) ; 93 | } 94 | 95 | @Test 96 | public void testRemovingIncludeHost() { 97 | fixture.addIncludedHost( "localhost" ) ; 98 | fixture.addIncludedHost( "127.0.0.1" ) ; 99 | fixture.addIncludedHost( "192.168.1.1" ) ; 100 | 101 | Set hosts = fixture.getIncludedHosts() ; 102 | 103 | assertNotNull( hosts ) ; 104 | assertTrue( hosts.size() == 3 ) ; 105 | 106 | fixture.removeIncludedHost("localhost" ) ; 107 | 108 | Set hostsMinusOne = fixture.getIncludedHosts() ; 109 | assertNotNull( hostsMinusOne ) ; 110 | assertTrue( hostsMinusOne.size() == 2 ) ; 111 | assertTrue( hostsMinusOne.contains( "127.0.0.1") && hostsMinusOne.contains( "192.168.1.1" ) ) ; 112 | 113 | } 114 | 115 | @Test 116 | public void testReplaceFlag() { 117 | fixture.addFlag( Flag.PORT_SPEC, "80,443" ) ; 118 | 119 | LinkedHashMap flagMap = fixture.getFlagMap() ; 120 | 121 | assertNotNull( flagMap ) ; 122 | assertTrue( flagMap.size() == 1 ) ; 123 | assertEquals( "80,443", flagMap.get( Flag.PORT_SPEC.toString() ) ) ; 124 | 125 | fixture.replaceFlag(Flag.PORT_SPEC, "22,23,25" ) ; 126 | 127 | LinkedHashMap flagMapReplaced = fixture.getFlagMap() ; 128 | 129 | assertNotNull( flagMapReplaced ) ; 130 | assertTrue( flagMapReplaced.size() == 1 ) ; 131 | assertEquals( flagMapReplaced.get( Flag.PORT_SPEC.toString()), "22,23,25" ) ; 132 | } 133 | 134 | @Test 135 | public void testGetIncludedHostsAsString() { 136 | fixture.addIncludedHost( "localhost" ) ; 137 | fixture.addIncludedHost( "127.0.0.1" ) ; 138 | fixture.addIncludedHost( "192.168.1.1" ) ; 139 | 140 | String hostsAsString = fixture.getIncludedHostsAsString() ; 141 | 142 | assertNotNull( hostsAsString ) ; 143 | assertTrue( hostsAsString.contains( "localhost" ) ) ; 144 | assertTrue( hostsAsString.contains( "127.0.0.1" ) ) ; 145 | assertTrue( hostsAsString.contains( "192.168.1.1" ) ) ; 146 | 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/parser/OnePassParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.parser; 36 | 37 | import java.io.IOException; 38 | import java.io.StringReader; 39 | 40 | import javax.xml.parsers.ParserConfigurationException; 41 | import javax.xml.parsers.SAXParser; 42 | import javax.xml.parsers.SAXParserFactory; 43 | 44 | import org.nmap4j.data.NMapRun; 45 | import org.nmap4j.parser.events.NMap4JParserEventListener; 46 | import org.nmap4j.parser.events.ParserEvent; 47 | import org.xml.sax.InputSource; 48 | import org.xml.sax.SAXException; 49 | 50 | 51 | 52 | /** 53 | * The OnePassParser takes a document and parsers it all the way through and 54 | * returns a tree of Objects that represent the XML that was just parsed. 55 | *

56 | * This type of parsing is best for smaller documents where the time to parse 57 | * it is small and the amount of data is less. If you have a large XML 58 | * document, you may want to consider using the NotifyingParser which issues 59 | * an event listener model, allowing you to listen in for host objects as they 60 | * are parsed. 61 | *

62 | * To use this class, pass in a either a File object or an InputStream and call 63 | * the parse() method. You will receive a tree of objects that contain the 64 | * contents of the XML. 65 | * 66 | * @author jsvede 67 | * 68 | */ 69 | public class OnePassParser implements NMap4JParserEventListener { 70 | 71 | public static final int STRING_INPUT = 1 ; 72 | public static final int FILE_NAME_INPUT = 2 ; 73 | 74 | private NMapRun nmapRun ; 75 | 76 | private INMapRunHandler nmrh ; 77 | private NMapXmlHandler nmxh ; 78 | 79 | 80 | public OnePassParser() { 81 | nmrh = new NMapRunHandlerImpl() ; 82 | nmxh = new NMapXmlHandler( nmrh ) ; 83 | } 84 | 85 | public NMapRun parse( String input, int type ) { 86 | 87 | NMapXmlHandler.addListener( this ) ; 88 | 89 | SAXParserFactory spf = SAXParserFactory.newInstance(); 90 | try { 91 | 92 | //get a new instance of parser 93 | SAXParser sp = spf.newSAXParser(); 94 | 95 | if( type == STRING_INPUT ) { 96 | StringReader strReader = new StringReader( input ) ; 97 | InputSource source = new InputSource (strReader ) ; 98 | 99 | sp.parse( source, nmxh ); 100 | 101 | } else if( type == FILE_NAME_INPUT ) { 102 | //parse the file and also register this class for call backs 103 | sp.parse("file:" + input, nmxh ); 104 | } 105 | 106 | 107 | 108 | }catch(SAXException se) { 109 | se.printStackTrace(); 110 | }catch(ParserConfigurationException pce) { 111 | pce.printStackTrace(); 112 | }catch (IOException ie) { 113 | ie.printStackTrace(); 114 | } 115 | 116 | NMapXmlHandler.removeListener( this ) ; 117 | 118 | return nmapRun ; 119 | } 120 | 121 | public void parseEventNotification(ParserEvent event) { 122 | //System.out.println( event.getPayload() ) ; 123 | if( event.getPayload() instanceof NMapRun ) { 124 | nmapRun = (NMapRun) event.getPayload() ; 125 | } 126 | } 127 | 128 | public void addListener(NMap4JParserEventListener aListener ) { 129 | NMapXmlHandler.addListener( aListener ) ; 130 | } 131 | 132 | public void removeListener( NMap4JParserEventListener aListener ) { 133 | NMapXmlHandler.removeListener( aListener ) ; 134 | } 135 | 136 | } 137 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/Nmap4j.java: -------------------------------------------------------------------------------- 1 | package org.nmap4j; 2 | 3 | import org.nmap4j.core.flags.ArgumentProperties; 4 | import org.nmap4j.core.nmap.ExecutionResults; 5 | import org.nmap4j.core.nmap.NMapExecutionException; 6 | import org.nmap4j.core.nmap.NMapExecutor; 7 | import org.nmap4j.core.nmap.NMapInitializationException; 8 | import org.nmap4j.core.nmap.NMapProperties; 9 | import org.nmap4j.data.NMapRun; 10 | import org.nmap4j.parser.OnePassParser; 11 | import org.nmap4j.valid.HostsInputValidator; 12 | 13 | 14 | /** 15 | * This is the simplified way to execute and parse Nmap output. This is the 16 | * easiest way to run Nmap from this API. Use of this class requires a little 17 | * more knowledge of Nmap's flags in order to work. 18 | *

19 | * Here is an example of how to use this class: 20 | * 21 | * Nmap4j nmap4j = new Nmap4j( "/usr/local" ) ; 22 | * nmap4j.includeHosts( "192.168.1.1-255" ) ; 23 | * nmap4j.excludeHosts( "192.168.1.110" ) ; 24 | * nmap4j.addFlags( "-T3 -oX - -O -sV" ) ; 25 | * nmap4j.execute() ; 26 | * if( !nma4j.hasError() ) { 27 | * NMapRun nmapRun = nmap4j.getResults() ; 28 | * } else { 29 | * System.out.println( nmap4j.getExecutionResults().getErrors() ) ; 30 | * } 31 | * 32 | *

33 | * This block would need a try/catch because the execute() method throws two 34 | * different exceptions. 35 | * 36 | * @author jsvede 37 | */ 38 | public class Nmap4j implements INmap4j { 39 | 40 | private NMapProperties nmapProperties; 41 | private ArgumentProperties flags; 42 | private NMapExecutor nmapExecutor; 43 | private ExecutionResults results; 44 | private HostsInputValidator validator; 45 | 46 | /** 47 | * Constructs this object with the path specified. This path needs to be 48 | * the path to your Nmap binary. On many systems this will be something 49 | * like "/usr/local". Additionally, this will also be the path to the 50 | * data dir required by Nmap. 51 | * 52 | * @param path 53 | */ 54 | public Nmap4j(String path) { 55 | nmapProperties = new NMapProperties(path); 56 | flags = new ArgumentProperties(); 57 | validator = new HostsInputValidator(); 58 | } 59 | 60 | /** 61 | * Executes the nmap scan with the parameters set. You should have 62 | * called addFlags() with appropriate Nmap flags prior to executing the 63 | * scan. 64 | * 65 | * @throws NMapInitializationException 66 | * @throws NMapExecutionException 67 | */ 68 | public void execute() throws NMapInitializationException, NMapExecutionException { 69 | nmapExecutor = new NMapExecutor(flags, nmapProperties); 70 | results = nmapExecutor.execute(); 71 | } 72 | 73 | /** 74 | * Add the appropriate flags to your scan. Call this method with all the 75 | * flags you will want. For example, if you want to scan for hosts, OS 76 | * information and service information you would pass "-sV -O -T4". This 77 | * method will append "-oX -" if you did not supply it. 78 | * 79 | * @param flagSet 80 | */ 81 | public void addFlags(String flagSet) { 82 | StringBuilder sb = new StringBuilder(); 83 | sb.append(flagSet); 84 | if (!flagSet.contains("-oX")) { 85 | sb.append(" -oX -"); 86 | } 87 | flags.addFlag(sb.toString()); 88 | } 89 | 90 | /** 91 | * Add a list of space delimited hosts that you want to scan. This 92 | * list conforms to the requirements that Nmap sets forth. 93 | * 94 | * @param hosts 95 | */ 96 | public void includeHosts(String hosts) { 97 | if (!validator.valid(hosts)) { 98 | throw new RuntimeException("Non legal hosts parameter"); 99 | } 100 | flags.addIncludedHost(hosts); 101 | } 102 | 103 | /** 104 | * Add a list of space delimited hosts to exclude. Usually this is used 105 | * when you specify a large included host list. This allows you specify 106 | * broad ranges host addresses and exclude some hosts within that range. 107 | * 108 | * @param hosts 109 | */ 110 | public void excludeHosts(String hosts) { 111 | flags.addExcludedHost(hosts); 112 | } 113 | 114 | /** 115 | * Returns the raw output of the execution. 116 | * 117 | * @return 118 | */ 119 | public String getOutput() { 120 | return results.getOutput(); 121 | } 122 | 123 | /** 124 | * This method returns an object tree representing the XML nodes. 125 | * 126 | * @return 127 | */ 128 | public NMapRun getResult() { 129 | OnePassParser parser = new OnePassParser(); 130 | NMapRun nmapRun = parser.parse(results.getOutput(), OnePassParser.STRING_INPUT); 131 | return nmapRun; 132 | } 133 | 134 | /** 135 | * Checks the output for the word "ERROR" as Nmap will usually produce an 136 | * error message that starts with ERROR though there are other scenarios. If 137 | * the call to getResult() fails check the error output. 138 | * 139 | * @return 140 | */ 141 | public boolean hasError() { 142 | return results.getErrors().contains("ERROR"); 143 | } 144 | 145 | /** 146 | * Use this method to get the raw results of the execution. The 147 | * ExecutionResults contains the raw output, the errors and the 148 | * command that was executed. 149 | * 150 | * @return 151 | */ 152 | public ExecutionResults getExecutionResults() { 153 | return results; 154 | } 155 | 156 | } 157 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/NMapRun.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | * 35 | */ 36 | package org.nmap4j.data; 37 | 38 | import java.util.ArrayList; 39 | 40 | import org.nmap4j.data.nmaprun.Debugging; 41 | import org.nmap4j.data.nmaprun.Host; 42 | import org.nmap4j.data.nmaprun.RunStats; 43 | import org.nmap4j.data.nmaprun.ScanInfo; 44 | import org.nmap4j.data.nmaprun.Verbose; 45 | 46 | /** 47 | * This class maps to the root element of NMap's XML output. 48 | * 49 | * @author jsvede 50 | * 51 | */ 52 | public class NMapRun { 53 | 54 | public final static String NMAPRUN_TAG = "nmaprun" ; 55 | 56 | public final static String SCANNER_ATTR = "scanner" ; 57 | public final static String ARGS_ATTR = "args" ; 58 | public final static String START_ATTR = "start" ; 59 | public final static String STARTSTR_ATTR = "startstr" ; 60 | public final static String VERSION_ATTR = "version" ; 61 | public final static String XML_OUTPUT_VERSION_ATTR = "xmloutputversion" ; 62 | 63 | // attributes 64 | private String scanner ; 65 | private String args ; 66 | private String start ; 67 | private String startstr ; 68 | private String version ; 69 | private String xmloutputversion ; 70 | 71 | // child elements 72 | private ScanInfo scanInfo ; 73 | private Verbose verbose ; 74 | private Debugging debugging ; 75 | private RunStats runStats ; 76 | private ArrayList hosts ; 77 | 78 | public NMapRun() { 79 | hosts = new ArrayList() ; 80 | } 81 | 82 | public ScanInfo getScanInfo() { 83 | return scanInfo; 84 | } 85 | public void setScanInfo(ScanInfo scanInfo) { 86 | this.scanInfo = scanInfo; 87 | } 88 | public Verbose getVerbose() { 89 | return verbose; 90 | } 91 | public void setVerbose(Verbose verbose) { 92 | this.verbose = verbose; 93 | } 94 | public Debugging getDebugging() { 95 | return debugging; 96 | } 97 | public void setDebugging(Debugging debugging) { 98 | this.debugging = debugging; 99 | } 100 | public RunStats getRunStats() { 101 | return runStats; 102 | } 103 | public void setRunStats(RunStats runStats) { 104 | this.runStats = runStats; 105 | } 106 | public ArrayList getHosts() { 107 | return hosts; 108 | } 109 | public void setHosts(ArrayList hosts) { 110 | this.hosts = hosts; 111 | } 112 | public void removeHost( Host host ) { 113 | hosts.remove( host ) ; 114 | } 115 | public void addHost( Host host ) { 116 | if( !hosts.contains( host ) ) { 117 | hosts.add( host ) ; 118 | } 119 | } 120 | public String getScanner() { 121 | return scanner; 122 | } 123 | public void setScanner(String scanner) { 124 | this.scanner = scanner; 125 | } 126 | public String getArgs() { 127 | return args; 128 | } 129 | public void setArgs(String args) { 130 | this.args = args; 131 | } 132 | public String getStart() { 133 | return start; 134 | } 135 | public void setStart(String start) { 136 | this.start = start; 137 | } 138 | public String getStartstr() { 139 | return startstr; 140 | } 141 | public void setStartstr(String startstr) { 142 | this.startstr = startstr; 143 | } 144 | public String getVersion() { 145 | return version; 146 | } 147 | public void setVersion(String version) { 148 | this.version = version; 149 | } 150 | public String getXmloutputversion() { 151 | return xmloutputversion; 152 | } 153 | public void setXmloutputversion(String xmloutputversion) { 154 | this.xmloutputversion = xmloutputversion; 155 | } 156 | 157 | @Override 158 | public String toString() { 159 | return "NMapRun [args=" + args + ", scanner=" + scanner + ", start=" 160 | + start + ", startstr=" + startstr + ", version=" + version 161 | + ", xmloutputversion=" + xmloutputversion + 162 | "\n\t hosts = " + hosts.size() + 163 | 164 | "]"; 165 | } 166 | 167 | 168 | 169 | 170 | } 171 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/data/nmaprun/Host.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.data.nmaprun; 36 | 37 | import java.util.ArrayList; 38 | 39 | import org.nmap4j.data.host.Address; 40 | import org.nmap4j.data.host.Distance; 41 | import org.nmap4j.data.host.Hostnames; 42 | import org.nmap4j.data.host.IpIdSequence; 43 | import org.nmap4j.data.host.Os; 44 | import org.nmap4j.data.host.Ports; 45 | import org.nmap4j.data.host.Status; 46 | import org.nmap4j.data.host.TcpSequence; 47 | import org.nmap4j.data.host.TcpTsSequence; 48 | import org.nmap4j.data.host.Times; 49 | import org.nmap4j.data.host.Uptime; 50 | import org.nmap4j.data.host.trace.Trace; 51 | 52 | 53 | public class Host { 54 | 55 | public final static String HOST_TAG = "host" ; 56 | 57 | public final static String STARTTIME_ATTR = "starttime" ; 58 | public final static String ENDTIME_ATTR = "endtime" ; 59 | 60 | private long startTime ; 61 | private long endTime ; 62 | 63 | private Status status ; 64 | private ArrayList

addresses ; 65 | private Ports ports ; 66 | private Os os ; 67 | private Uptime uptime ; 68 | private Distance distance ; 69 | private TcpSequence tcpSequence ; 70 | private IpIdSequence ipIdSequence; 71 | private TcpTsSequence tcpTsSequence; 72 | private Times times ; 73 | private Hostnames hostnames ; 74 | 75 | private Trace trace; 76 | public Trace getTrace() { 77 | return trace; 78 | } 79 | 80 | public void setTrace(Trace trace) { 81 | this.trace = trace; 82 | } 83 | public Host() { 84 | addresses = new ArrayList
() ; 85 | } 86 | 87 | public Hostnames getHostnames() { 88 | return hostnames; 89 | } 90 | public void setHostnames(Hostnames hostnames) { 91 | this.hostnames = hostnames; 92 | } 93 | public long getStartTime() { 94 | return startTime; 95 | } 96 | public void setStartTime(long startTime) { 97 | this.startTime = startTime; 98 | } 99 | public long getEndTime() { 100 | return endTime; 101 | } 102 | public void setEndTime(long endTime) { 103 | this.endTime = endTime; 104 | } 105 | public Status getStatus() { 106 | return status; 107 | } 108 | public void setStatus(Status status) { 109 | this.status = status; 110 | } 111 | 112 | 113 | public ArrayList
getAddresses() { 114 | return addresses; 115 | } 116 | public void setAddress(ArrayList
address) { 117 | this.addresses = address; 118 | } 119 | public void addAddress( Address address ) { 120 | addresses.add( address ) ; 121 | } 122 | public Ports getPorts() { 123 | return ports; 124 | } 125 | public void setPorts(Ports ports) { 126 | this.ports = ports; 127 | } 128 | public Os getOs() { 129 | return os; 130 | } 131 | public void setOs(Os os) { 132 | this.os = os; 133 | } 134 | public Uptime getUptime() { 135 | return uptime; 136 | } 137 | public void setUptime(Uptime uptime) { 138 | this.uptime = uptime; 139 | } 140 | public Distance getDistance() { 141 | return distance; 142 | } 143 | public void setDistance(Distance distance) { 144 | this.distance = distance; 145 | } 146 | public TcpSequence getTcpSequence() { 147 | return tcpSequence; 148 | } 149 | public void setTcpSequence(TcpSequence tcpSequence) { 150 | this.tcpSequence = tcpSequence; 151 | } 152 | public IpIdSequence getIpIdSequence() { 153 | return ipIdSequence; 154 | } 155 | public void setIpIdSequence(IpIdSequence ipIdSequence) { 156 | this.ipIdSequence = ipIdSequence; 157 | } 158 | public TcpTsSequence getTcpTsSequence() { 159 | return tcpTsSequence; 160 | } 161 | public void setTcpTsSequence(TcpTsSequence tcpTsSequence) { 162 | this.tcpTsSequence = tcpTsSequence; 163 | } 164 | public Times getTimes() { 165 | return times; 166 | } 167 | public void setTimes(Times times) { 168 | this.times = times; 169 | } 170 | @Override 171 | public String toString() { 172 | return "Host [endTime=" + endTime + ", startTime=" + startTime + "]"; 173 | } 174 | 175 | 176 | 177 | 178 | } 179 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/parser/INMapRunHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.parser; 36 | 37 | import org.nmap4j.data.NMapRun; 38 | import org.nmap4j.data.host.Address; 39 | import org.nmap4j.data.host.Cpe; 40 | import org.nmap4j.data.host.Distance; 41 | import org.nmap4j.data.host.Hostnames; 42 | import org.nmap4j.data.host.IpIdSequence; 43 | import org.nmap4j.data.host.Os; 44 | import org.nmap4j.data.host.Ports; 45 | import org.nmap4j.data.host.Status; 46 | import org.nmap4j.data.host.TcpSequence; 47 | import org.nmap4j.data.host.TcpTsSequence; 48 | import org.nmap4j.data.host.Times; 49 | import org.nmap4j.data.host.Uptime; 50 | import org.nmap4j.data.host.os.OsClass; 51 | import org.nmap4j.data.host.os.OsMatch; 52 | import org.nmap4j.data.host.os.PortUsed; 53 | import org.nmap4j.data.host.ports.ExtraPorts; 54 | import org.nmap4j.data.host.ports.Port; 55 | import org.nmap4j.data.host.trace.Hop; 56 | import org.nmap4j.data.host.trace.Trace; 57 | import org.nmap4j.data.nmaprun.Debugging; 58 | import org.nmap4j.data.nmaprun.Host; 59 | import org.nmap4j.data.nmaprun.RunStats; 60 | import org.nmap4j.data.nmaprun.ScanInfo; 61 | import org.nmap4j.data.nmaprun.Verbose; 62 | import org.nmap4j.data.nmaprun.host.ports.extraports.ExtraReasons; 63 | import org.nmap4j.data.nmaprun.host.ports.port.Service; 64 | import org.nmap4j.data.nmaprun.host.ports.port.State; 65 | import org.nmap4j.data.nmaprun.hostnames.Hostname; 66 | import org.nmap4j.data.nmaprun.runstats.Finished; 67 | import org.nmap4j.data.nmaprun.runstats.Hosts; 68 | import org.xml.sax.Attributes; 69 | 70 | /** 71 | * This interface defines the functionality necessary to create the various 72 | * nmap XML objects based on the parsed data. It's primary purpose is to 73 | * allow a discrete way to handle creating data objects from the XML data. 74 | *

75 | * The methods defined here are specifically for loading the XML attributes 76 | * and not the child elements. That is handled in the DefaultHandler 77 | * implementation. In essence, this is a utility class for creating Objects 78 | * from the XML attributes. 79 | * 80 | * @author jsvede 81 | * 82 | */ 83 | public interface INMapRunHandler { 84 | 85 | public NMapRun createNMapRun( Attributes attributes ) ; 86 | 87 | public Host createHost( Attributes attributes ) ; 88 | 89 | public Distance createDistance( Attributes attributes ) ; 90 | 91 | public Address createAddress( Attributes attributes ) ; 92 | 93 | public Hostnames createHostnames( Attributes attributes ) ; 94 | 95 | public Hostname createHostname( Attributes attributes ) ; 96 | 97 | public IpIdSequence createIpIdSequence( Attributes attributes ) ; 98 | 99 | public Os createOs( Attributes attributes ) ; 100 | 101 | public Ports createPorts( Attributes attributes ) ; 102 | 103 | public Status createStatus( Attributes attributes ) ; 104 | 105 | public TcpSequence createTcpSequence( Attributes attributes ) ; 106 | 107 | public TcpTsSequence createTcpTsSequence( Attributes attributes ) ; 108 | 109 | public Times createTimes( Attributes attributes ) ; 110 | 111 | public Uptime createUptime( Attributes attributes ) ; 112 | 113 | public OsClass createOsClass( Attributes attributes ) ; 114 | 115 | public OsMatch createOsMatch( Attributes attributes ) ; 116 | 117 | public PortUsed createPortUsed( Attributes attributes ) ; 118 | 119 | public ExtraPorts createExtraPorts( Attributes attributes ) ; 120 | 121 | public Port createPort( Attributes attributes ) ; 122 | 123 | public Debugging createDebugging( Attributes attributes ) ; 124 | 125 | public RunStats createRunStats( Attributes attributes ) ; 126 | 127 | public ScanInfo createScanInfo( Attributes attributes ) ; 128 | 129 | public Verbose createVerbose( Attributes attributes ) ; 130 | 131 | public ExtraReasons createExtraReasons( Attributes attributes ) ; 132 | 133 | public Service createService( Attributes attributes ) ; 134 | 135 | public State createState( Attributes attributes ) ; 136 | 137 | public Finished createFinished( Attributes attributes ) ; 138 | 139 | public Hosts createHosts( Attributes attributes ) ; 140 | 141 | public Cpe createCpe( Attributes attributes ) ; 142 | 143 | public Trace createTrace( Attributes attributes ) ; 144 | 145 | public Hop createHop( Attributes attributes ) ; 146 | 147 | } 148 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/core/nmap/NMapProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.core.nmap; 36 | 37 | import java.io.File ; 38 | 39 | import org.nmap4j.core.flags.Flag; 40 | 41 | /** 42 | * This class is used to manage the path to nmap. This class attempts to look 43 | * for the environmental variable NMAP_HOME. If that is not set it is expected 44 | * that the user set this location via this class. 45 | *

46 | * Essentially the path needs to be to the location where the "bin" dir that 47 | * contains the nmap binary is. Usually, when dealing with most nmap installs, 48 | * this will also be location where the "share" dir is located. It's ideal 49 | * to identify both of these of dirs as this allows you to specify the right 50 | * share dir for a binary. Otherwise there is a risk that nmap will pick up 51 | * a default in the system and this may introduce inconsistencies. 52 | *

53 | * If you are planning to use the system property NMAP_HOME to set your 54 | * NMap path, use the no arg constructor. Otherwise use the constructor with 55 | * the String. If you want flexibility, set the path, use the no arg 56 | * constructor and then use the setPath(String) method. 57 | * 58 | * @author jsvede 59 | * 60 | */ 61 | public class NMapProperties { 62 | 63 | private String pathToNMap ; 64 | 65 | private final String BIN = "bin" ; 66 | private final String SHARE = "share" ; 67 | private final String COMMAND = "nmap" ; 68 | 69 | /** 70 | * Constructs an instance of NMapProperties and looks in the environment 71 | * properties for NMAP_HOME. If it is not found, the path is initialized 72 | * to null and the API assumes that you will set it manually. See setPath(). 73 | */ 74 | public NMapProperties() { 75 | String path = System.getenv().get( "NMAP_HOME" ) ; 76 | if( path != null && path.length() > 0 ) { 77 | pathToNMap = path ; 78 | } 79 | } 80 | 81 | /** 82 | * Contructs and instance of NMapProperties using the path passed. 83 | * @param path 84 | */ 85 | public NMapProperties( String path ) { 86 | pathToNMap = path ; 87 | } 88 | 89 | /** 90 | * Returns the current path. 91 | * @return 92 | */ 93 | public String getPath() { 94 | return pathToNMap ; 95 | } 96 | 97 | /** 98 | * Sets the path the bin dir where nmap can be found. This is also the path 99 | * to the share dir which contains important files for nmap. 100 | *

101 | * For example, if the nmap bin dir is in /usr/local/share/bin/nmap the 102 | * path you would set into this method is /usr/local/share . 103 | * @param pathToBinDir - /the/path/to/nmapbindir. 104 | */ 105 | public void setPath( String pathToBinDir ) { 106 | pathToNMap = pathToBinDir ; 107 | } 108 | 109 | /** 110 | * Returns the expected location of the share dir relative to the path set 111 | * or passed in at construction time. The value returned by this method is 112 | * equivalent to that path variable + filesystem dependent separator + bin . 113 | * @return 114 | */ 115 | public String getBinDir() { 116 | return pathToNMap + File.separator + BIN ; 117 | } 118 | 119 | /** 120 | * Returns the expected location of the share dir relative to the path set 121 | * or passed in at construction time. The value returned by this method is 122 | * equivalent to the path variable + filesystem dependent separator + share. 123 | * @return 124 | */ 125 | public String getShareDir() { 126 | return pathToNMap + File.separator + SHARE + File.separator + "nmap" ; 127 | } 128 | 129 | /** 130 | * Returns the system's os.name property. 131 | * @return 132 | */ 133 | private String getOS() { 134 | return System.getProperty( "os.name" ) ; 135 | } 136 | 137 | /** 138 | * This returns the full path to the nmap version to be executed. 139 | * @return 140 | */ 141 | public String getFullyFormattedCommand() { 142 | 143 | StringBuffer command = new StringBuffer() ; 144 | 145 | if ( getOS().toLowerCase().contains( "windows" ) ){ 146 | command.append( pathToNMap ) ; 147 | command.append( File.separator ) ; 148 | command.append( COMMAND ) ; 149 | command.append( ".exe" ) ; 150 | } 151 | else { //Linux or MacOsX case 152 | command.append( getBinDir() ) ; 153 | command.append( File.separator ) ; 154 | command.append( COMMAND ) ; 155 | command.append( " " ) ; 156 | command.append( Flag.DATADIR ) ; 157 | command.append( " " ) ; 158 | command.append( getShareDir() ) ; 159 | } 160 | 161 | return command.toString() ; 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /src/test/java/org/nmap4j/parsers/OnePassParserTest.java: -------------------------------------------------------------------------------- 1 | package org.nmap4j.parsers; 2 | 3 | import static org.junit.Assert.assertNotNull; 4 | import static org.junit.Assert.fail; 5 | 6 | import java.io.IOException; 7 | import java.io.InputStream; 8 | import java.util.ArrayList; 9 | 10 | import org.apache.commons.io.IOUtils; 11 | import org.junit.Test; 12 | import org.nmap4j.core.flags.Flag; 13 | import org.nmap4j.core.nmap.ExecutionResults; 14 | import org.nmap4j.core.nmap.NMapExecutionException; 15 | import org.nmap4j.core.nmap.NMapInitializationException; 16 | import org.nmap4j.core.scans.BaseScan; 17 | import org.nmap4j.core.scans.IScan.TimingFlag; 18 | import org.nmap4j.core.scans.ParameterValidationFailureException; 19 | import org.nmap4j.data.NMapRun; 20 | import org.nmap4j.data.host.Cpe; 21 | import org.nmap4j.data.host.os.OsClass; 22 | import org.nmap4j.data.nmaprun.Host; 23 | import org.nmap4j.parser.OnePassParser; 24 | import org.nmap4j.parser.events.NMap4JParserEventListener; 25 | import org.nmap4j.parser.events.ParserEvent; 26 | 27 | import test.constants.IConstants; 28 | 29 | public class OnePassParserTest implements IConstants { 30 | 31 | String fileName = "nmap-xml/ms-vscan.xml" ; 32 | int count = 0 ; 33 | 34 | 35 | public void testOnePass() { 36 | OnePassParser opp = new OnePassParser() ; 37 | 38 | NMapRun nmapRun = null ; 39 | try { 40 | InputStream is = getClass().getClassLoader().getResourceAsStream( fileName ) ; 41 | 42 | String fileAsString = IOUtils.toString( is ) ; 43 | 44 | nmapRun = opp.parse( fileAsString, OnePassParser.STRING_INPUT ); 45 | 46 | } catch (IOException e) { 47 | e.printStackTrace(); 48 | } 49 | 50 | if( nmapRun != null ) { 51 | System.out.println( "hosts count: " + nmapRun.getHosts().size() ) ; 52 | } else { 53 | System.out.println( "nmapRun is null" ) ; 54 | } 55 | } 56 | 57 | @Test 58 | public void testOnePassWithSMBOutput() { 59 | 60 | String smbFileName = "nmap-xml/SMB-os-discovery_CPE.xml" ; 61 | 62 | OnePassParser opp = new OnePassParser() ; 63 | 64 | NMapRun nmapRun = null ; 65 | try { 66 | InputStream is = getClass().getClassLoader().getResourceAsStream( smbFileName ) ; 67 | 68 | String fileAsString = IOUtils.toString( is ) ; 69 | 70 | nmapRun = opp.parse( fileAsString, OnePassParser.STRING_INPUT ); 71 | 72 | 73 | } catch (IOException e) { 74 | e.printStackTrace(); 75 | } 76 | 77 | if( nmapRun != null ) { 78 | System.out.println( "hosts count: " + nmapRun.getHosts().size() ) ; 79 | } else { 80 | System.out.println( "nmapRun is null" ) ; 81 | } 82 | } 83 | 84 | 85 | @Test 86 | public void testForPresenceOfCpeData() { 87 | 88 | System.out.println( "start") ; 89 | 90 | String smbFileName = "nmap-xml/SMB-os-discovery_CPE.xml" ; 91 | 92 | OnePassParser opp = new OnePassParser() ; 93 | 94 | NMapRun nmapRun = null ; 95 | try { 96 | InputStream is = getClass().getClassLoader().getResourceAsStream( smbFileName ) ; 97 | 98 | String fileAsString = IOUtils.toString( is ) ; 99 | 100 | nmapRun = opp.parse( fileAsString, OnePassParser.STRING_INPUT ); 101 | 102 | ArrayList hosts = nmapRun.getHosts() ; 103 | 104 | boolean foundAtLeastOneNotNullCpeObj = false ; 105 | 106 | for( Host h : hosts ) { 107 | if( h.getOs() != null ) { 108 | if( h.getOs().getOsClasses() != null ) { 109 | ArrayList osClasses = h.getOs().getOsClasses() ; 110 | for( OsClass osClass : osClasses ) { 111 | ArrayList cpeData = osClass.getCpe() ; 112 | for( Cpe cpe : cpeData ) { 113 | if( cpe != null ) { 114 | if( cpe.getCpeData() != null ) { 115 | System.out.println( cpe.getCpeData() ) ; 116 | foundAtLeastOneNotNullCpeObj = true ; 117 | } 118 | } 119 | } 120 | } 121 | } 122 | } 123 | } 124 | 125 | if( !foundAtLeastOneNotNullCpeObj ) { 126 | fail() ; 127 | } 128 | 129 | 130 | } catch (IOException e) { 131 | e.printStackTrace(); 132 | } 133 | 134 | if( nmapRun != null ) { 135 | System.out.println( "hosts count: " + nmapRun.getHosts().size() ) ; 136 | } else { 137 | System.out.println( "nmapRun is null" ) ; 138 | } 139 | } 140 | 141 | @Test 142 | public void testLocalHostScan() { 143 | BaseScan baseScan = new BaseScan( "/usr/local") ; 144 | 145 | baseScan.includeHost( "localhost" ) ; 146 | baseScan.addPorts(new int[]{ 3306} ) ; 147 | baseScan.addFlag( Flag.OS_DETECTION ) ; 148 | baseScan.setTiming( TimingFlag.AGGRESSIVE ) ; 149 | 150 | try { 151 | ExecutionResults results = baseScan.executeScan() ; 152 | System.out.println( results.getExecutedCommand() ) ; 153 | System.out.println( results.getOutput() ) ; 154 | if( results.hasErrors() ) { 155 | System.out.println( "Errors: " + results.getErrors() ) ; 156 | } else { 157 | System.out.println( "Results: " + results.getOutput() ) ; 158 | } 159 | 160 | OnePassParser opp = new OnePassParser() ; 161 | NMapRun nmapRun = opp.parse(results.getOutput(), OnePassParser.STRING_INPUT ) ; 162 | 163 | assertNotNull("Are you root?", nmapRun); 164 | String output = nmapRun.getHosts().get(0).getPorts().getPorts().get(0).getState().getState() ; 165 | 166 | System.out.println( "Port state: " + output ) ; 167 | 168 | } catch (ParameterValidationFailureException | NMapExecutionException | NMapInitializationException e) { 169 | e.printStackTrace(); 170 | fail() ; 171 | } 172 | } 173 | 174 | @Test 175 | public void testAddingListener() { 176 | System.out.println( "start") ; 177 | 178 | String smbFileName = "nmap-xml/SMB-os-discovery_CPE.xml" ; 179 | 180 | OnePassParser opp = new OnePassParser() ; 181 | 182 | NMapRun nmapRun = null ; 183 | 184 | HostListener simpleListener = new HostListener() ; 185 | 186 | try { 187 | InputStream is = getClass().getClassLoader().getResourceAsStream( smbFileName ) ; 188 | 189 | String fileAsString = IOUtils.toString( is ) ; 190 | 191 | opp.addListener( simpleListener ) ; 192 | nmapRun = opp.parse( fileAsString, OnePassParser.STRING_INPUT ); 193 | 194 | ArrayList hosts = nmapRun.getHosts() ; 195 | 196 | } catch (IOException e) { 197 | e.printStackTrace(); 198 | } 199 | 200 | if( nmapRun != null ) { 201 | if( nmapRun.getHosts().size() != simpleListener.getHostCount() ) { 202 | fail() ; 203 | } 204 | } else { 205 | fail() ; 206 | } 207 | } 208 | 209 | private class HostListener implements NMap4JParserEventListener { 210 | 211 | private int hostCount = 0 ; 212 | 213 | @Override 214 | public void parseEventNotification(ParserEvent event) { 215 | if( event.getPayload() instanceof Host ) { 216 | hostCount++ ; 217 | } 218 | } 219 | 220 | public int getHostCount() { 221 | return hostCount ; 222 | } 223 | 224 | } 225 | } 226 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/parser/util/NMapXmlObsfucator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.parser.util; 36 | 37 | import java.io.IOException; 38 | 39 | import javax.xml.parsers.ParserConfigurationException; 40 | import javax.xml.parsers.SAXParser; 41 | import javax.xml.parsers.SAXParserFactory; 42 | 43 | import org.xml.sax.Attributes; 44 | import org.xml.sax.SAXException; 45 | import org.xml.sax.helpers.DefaultHandler; 46 | 47 | /** 48 | * This class is an implementation of the SAX parser's DefaultHandler which 49 | * basically receive's notifications of nodes from the SAX parser as nodes 50 | * are encountered. 51 | *

52 | * This implementation serves the specific purpose of obsfucating the IP address 53 | * and hostname values in the XML output of NMap. 54 | * 55 | * @author jsvede 56 | * 57 | */ 58 | public class NMapXmlObsfucator extends DefaultHandler { 59 | 60 | private int indentIdx = 0 ; 61 | 62 | /* 63 | * TODO: replace the file with an agnostic reference. Also, this class's 64 | * main method needs to be removed and an examples src tree needs to be 65 | * created. 66 | * 67 | */ 68 | private static String XML_FILE = "/Users/jsvede/work/loquatic/workspaces/nmap4j/org.nmap4j.parser/nmap-webapp-20090831-123159.xml" ; 69 | 70 | 71 | public static void main(String[] args) { 72 | NMapXmlObsfucator nmxo = new NMapXmlObsfucator() ; 73 | 74 | SAXParserFactory spf = SAXParserFactory.newInstance(); 75 | try { 76 | 77 | //get a new instance of parser 78 | SAXParser sp = spf.newSAXParser(); 79 | 80 | if( args != null && args.length > 0 ) { 81 | XML_FILE = args[0] ; 82 | } 83 | // String fileName = args[/0] ; 84 | 85 | //parse the file and also register this class for call backs 86 | sp.parse("file:" + XML_FILE, nmxo ); 87 | 88 | }catch(SAXException se) { 89 | se.printStackTrace(); 90 | }catch(ParserConfigurationException pce) { 91 | pce.printStackTrace(); 92 | }catch (IOException ie) { 93 | ie.printStackTrace(); 94 | } 95 | 96 | 97 | } 98 | 99 | @Override 100 | public void endDocument() throws SAXException { 101 | // TODO Auto-generated method stub 102 | super.endDocument(); 103 | } 104 | 105 | @Override 106 | public void endElement(String uri, String localName, String qName) 107 | throws SAXException { 108 | // TODO Auto-generated method stub 109 | super.endElement(uri, localName, qName); 110 | 111 | indentIdx-- ; 112 | 113 | StringBuffer indent = new StringBuffer() ; 114 | for( int x=0; x" ) ; 119 | 120 | } 121 | 122 | @Override 123 | public void startDocument() throws SAXException { 124 | // TODO Auto-generated method stub 125 | super.startDocument(); 126 | } 127 | 128 | @Override 129 | public void startElement(String uri, String localName, String qName, 130 | Attributes attributes) throws SAXException { 131 | // TODO Auto-generated method stub 132 | super.startElement(uri, localName, qName, attributes); 133 | 134 | // System.out.println( "uri = " + uri ) ; 135 | // System.out.println( "localName = " + localName ) ; 136 | StringBuffer indent = new StringBuffer() ; 137 | for( int x=0; x\n" ) ; 170 | indentIdx++ ; 171 | } 172 | 173 | private String generateRandomHostName( String defaultDomainName ) { 174 | String myDefaultDomainName = "dummydomain.com" ; 175 | String uid = UidGenerator.getBigId( "host" ) ; 176 | 177 | StringBuffer nameBuffer = new StringBuffer() ; 178 | nameBuffer.append( uid ) ; 179 | nameBuffer.append( "." ) ; 180 | if( defaultDomainName != null ) { 181 | nameBuffer.append( defaultDomainName ) ; 182 | } else { 183 | nameBuffer.append( myDefaultDomainName ) ; 184 | } 185 | return nameBuffer.toString() ; 186 | } 187 | 188 | } 189 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/core/nmap/NMapExecutor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | * 35 | */ 36 | package org.nmap4j.core.nmap ; 37 | 38 | import java.io.BufferedReader ; 39 | import java.io.IOException ; 40 | import java.io.InputStream ; 41 | import java.io.InputStreamReader ; 42 | import java.io.OutputStream ; 43 | import java.util.concurrent.CompletableFuture; 44 | import java.util.concurrent.ExecutionException; 45 | 46 | import org.nmap4j.core.flags.ArgumentProperties ; 47 | 48 | /** 49 | * A simple class that encapsulates executing NMap. 50 | *

51 | * This class executes nmap synchronously. 52 | * 53 | * @author jsvede 54 | * 55 | */ 56 | public class NMapExecutor { 57 | 58 | private ArgumentProperties nmapArguments ; 59 | private NMapProperties nmapProperties ; 60 | 61 | /** 62 | * Constructs an instance based on the ArgumentProperties and the 63 | * NMapProperties passed in. These become the basis for the subsequent 64 | * execution of NMap. 65 | * 66 | * @param argProps 67 | * @param nmapProps 68 | * @throws NMapInitializationException 69 | */ 70 | public NMapExecutor( ArgumentProperties argProps, NMapProperties nmapProps ) 71 | throws NMapInitializationException { 72 | nmapArguments = argProps ; 73 | nmapProperties = nmapProps ; 74 | if ( nmapArguments == null || nmapProperties == null ) { 75 | throw new NMapInitializationException( 76 | "You cannot instantiate " 77 | + "an NMapExecutor with nulls in either argument. Please " 78 | + "refer to the documentation if you aren't sure how to proceed." ) ; 79 | } 80 | if ( nmapProps.getPath() == null 81 | || ( nmapProps.getPath() != null && nmapProps.getPath() 82 | .length() <= 0 ) ) { 83 | throw new NMapInitializationException( 84 | "the NMAP_HOME variable is not set " 85 | + "or you did not set this path." ) ; 86 | } 87 | } 88 | 89 | /** 90 | * Returns the system's os.name property. 91 | * 92 | * @return 93 | */ 94 | private String getOS() { 95 | return System.getProperty( "os.name" ) ; 96 | } 97 | 98 | /** 99 | * Get the nmap command as a StringBuffer. 100 | * 101 | * @return 102 | */ 103 | private StringBuffer getCommand() { 104 | 105 | StringBuffer fullCommand = new StringBuffer() ; 106 | fullCommand.append( nmapProperties.getFullyFormattedCommand() ) ; 107 | fullCommand.append( " " ) ; 108 | fullCommand.append( nmapArguments.getFlags() ) ; 109 | 110 | return fullCommand ; 111 | } 112 | 113 | /** 114 | * This method attempts to execute NMap using the properties supplied when 115 | * this object was constructed. 116 | *

117 | * This method can throw an NMapExecutionException which will be a wrapper 118 | * around an IO Exception. 119 | * 120 | * @return 121 | * @throws NMapExecutionException 122 | */ 123 | public ExecutionResults execute() throws NMapExecutionException { 124 | StringBuffer command = getCommand() ; 125 | ExecutionResults results = new ExecutionResults() ; 126 | 127 | try { 128 | results.setExecutedCommand( command.toString() ) ; 129 | Process process = Runtime.getRuntime().exec( command.toString() ) ; 130 | 131 | CompletableFuture errorPromise = convertStream( process.getErrorStream() ) ; 132 | CompletableFuture outputPromise = convertStream( process.getInputStream() ); 133 | 134 | results.setOutput(outputPromise.get()) ; 135 | results.setErrors(errorPromise.get()) ; 136 | 137 | } catch ( IOException | InterruptedException | ExecutionException e ) { 138 | throw new NMapExecutionException( e.getMessage(), e ) ; 139 | } 140 | 141 | return results ; 142 | } 143 | 144 | /** 145 | * Converts the given InputStream to a String. This is how the streams from 146 | * executing NMap are converted and later stored in the ExecutionResults. 147 | * 148 | * @param is 149 | * @return promise of converted String 150 | * @throws IOException 151 | */ 152 | private CompletableFuture convertStream( InputStream is ) throws IOException { 153 | CompletableFuture promise = CompletableFuture.supplyAsync(() -> { 154 | String output ; 155 | StringBuffer outputBuffer = new StringBuffer() ; 156 | BufferedReader streamReader = new BufferedReader( 157 | new InputStreamReader( is ) ) ; 158 | try { 159 | while ( ( output = streamReader.readLine() ) != null ) { 160 | outputBuffer.append( output ) ; 161 | outputBuffer.append( "\n" ) ; 162 | } 163 | } catch (IOException e) { 164 | e.printStackTrace(); 165 | } 166 | 167 | return outputBuffer.toString(); 168 | }); 169 | 170 | return promise; 171 | } 172 | 173 | public String toString() { 174 | return getCommand().toString() ; 175 | } 176 | 177 | } 178 | -------------------------------------------------------------------------------- /src/main/java/org/nmap4j/core/scans/IScan.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, nmap4j.org 3 | * 4 | * All rights reserved. 5 | * 6 | * This license covers only the Nmap4j library. To use this library with 7 | * Nmap, you must also comply with Nmap's license. Including Nmap within 8 | * commercial applications or appliances generally requires the purchase 9 | * of a commercial Nmap license (see http://nmap.org/book/man-legal.html). 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions are met: 13 | * 14 | * * Redistributions of source code must retain the above copyright notice, 15 | * this list of conditions and the following disclaimer. 16 | * * Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the nmap4j.org nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | * POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package org.nmap4j.core.scans; 36 | 37 | import org.nmap4j.core.flags.ArgumentProperties; 38 | import org.nmap4j.core.flags.Flag; 39 | import org.nmap4j.core.nmap.ExecutionResults; 40 | import org.nmap4j.core.nmap.NMapExecutionException; 41 | import org.nmap4j.core.nmap.NMapInitializationException; 42 | import org.nmap4j.core.nmap.NMapProperties; 43 | 44 | /** 45 | * Parent interface of all Scan implentations. Allows you to customize the 46 | * scan behavior if for any reason the BaseScan doesn't meet your needs. 47 | * 48 | * @author jsvede 49 | * 50 | */ 51 | public interface IScan { 52 | 53 | public enum TimingFlag { PARANOID, SNEAKY, POLITE, NORMAL, AGGRESSIVE, INSANE } ; 54 | 55 | public enum OutputType { XML, GREPPABLE, SCRIPT_KIDDIE, NORMAL } ; 56 | 57 | /** 58 | * Sets the scan timing flag. 59 | * 60 | * @param tf 61 | */ 62 | public void setTiming( TimingFlag tf ) ; 63 | 64 | /** 65 | * Adds a single port to the scan spec; adds the implied -p flag. You can 66 | * call this method repetitively to add ports. 67 | * 68 | * @param port 69 | */ 70 | public void addPort( int port ) ; 71 | 72 | /** 73 | * Allows you to add ports in a block; this can also be called repetitively. 74 | * @param ports 75 | */ 76 | public void addPorts( int[] ports ) ; 77 | 78 | /** 79 | * Add a single host to the list of hosts. Additive. 80 | * 81 | * @param host 82 | */ 83 | public void includeHost( String host ) ; 84 | 85 | /** 86 | * Add the the hosts from the array. Additive. 87 | * 88 | * @param hosts 89 | */ 90 | public void includeHosts( String[] hosts ) ; 91 | 92 | /** 93 | * Add a single host to the list of excluded hosts. Additive. 94 | * 95 | * @param host 96 | */ 97 | public void excludeHost( String host ) ; 98 | 99 | /** 100 | * Add the array of hosts to the list of hosts that are excluded. Additive. 101 | * 102 | * @param hosts 103 | */ 104 | public void excludeHosts( String[] hosts ) ; 105 | 106 | /** 107 | * Removes the specified host from the list of included hosts. 108 | * 109 | * @param host 110 | */ 111 | public void removeIncludeHost( String host ) ; 112 | 113 | /** 114 | * Removes all the hosts in the array from the list of included hosts. 115 | * Should not throw any exceptions if a host in the array is not in the 116 | * current list. 117 | * 118 | * @param hosts 119 | */ 120 | public void removeIncludeHosts( String[] hosts ) ; 121 | 122 | /** 123 | * Remove one host from the list of excluded hosts. 124 | * 125 | * @param host 126 | */ 127 | public void removeExcludeHost( String host ) ; 128 | 129 | /** 130 | * Remove the list of hosts from the list of hosts that should be excluded 131 | * from a scan. 132 | * 133 | * @param hosts 134 | */ 135 | public void removeExcludeHosts( String[] hosts ) ; 136 | 137 | 138 | /** 139 | * Sets the output type flag accordingly and also sets the filename. This 140 | * method should be used to override the default behavior, writing XML to the 141 | * std out. 142 | * 143 | * @param ot 144 | */ 145 | public void setOutputType( OutputType ot, String fileName ) ; 146 | 147 | /** 148 | * Executes a scan and blocks while scan runs. If your scan is long 149 | * running consider using the executeAsynchronousExecute() method. 150 | * 151 | * @return 152 | * @throws NMapInitializationException 153 | */ 154 | public ExecutionResults executeScan() 155 | throws ParameterValidationFailureException, 156 | NMapExecutionException, NMapInitializationException ; 157 | 158 | /** 159 | * Execute a scan asynchronously; you must pass in a callback in order 160 | * for this to work. 161 | * 162 | * @param isc 163 | */ 164 | public void executeAsynchronousScan( IScanCallback isc ) 165 | throws ParameterValidationFailureException, 166 | NMapExecutionException ; 167 | 168 | /** 169 | * Allows for a controlled way to vet a scan configuration. Allows API users 170 | * to verify the right combination of flags, the right values for things like 171 | * ports or hosts (for example, allows users to prevent hosts from being 172 | * scanned, etc). 173 | * 174 | * @param isv 175 | * @throws ParameterValidationFailureException 176 | */ 177 | public void setScanValidator( IScanValidator isv ) ; 178 | 179 | /** 180 | * Specifies the path in which to look for the nmap binaries and share 181 | * directory. If this is not set, the API will try to use the environmental 182 | * variable NMAP_HOME. 183 | * @param path 184 | */ 185 | public void setNMapPath( String path ) ; 186 | 187 | /** 188 | * Adds the specified flag to the underlying ArgumentProperties object. 189 | */ 190 | public void addFlag( Flag flag ) ; 191 | 192 | /** 193 | * Removes the specified flag from the underlying ArgumentProperties object. 194 | */ 195 | public void removeFlag( Flag flag ) ; 196 | 197 | /** 198 | * This provides access to the member variable for the ArgumentProperties. 199 | * @return 200 | */ 201 | public ArgumentProperties getArgumentProperties() ; 202 | 203 | /** 204 | * This provides access to the member variable for the NMapProperties. 205 | * @return 206 | */ 207 | public NMapProperties getNMapProperties() ; 208 | } 209 | --------------------------------------------------------------------------------