repack wmi_crawler

This commit is contained in:
snoop 2017-04-12 14:54:10 +09:00
commit 121e32d3b3
11 changed files with 406 additions and 0 deletions

19
.gitignore vendored Normal file
View File

@ -0,0 +1,19 @@
# Created by .ignore support plugin (hsz.mobi)
### Java template
*.class
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.ear
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
.idea/
.target/
*.iml

BIN
lib/jacob-1.18-x64.dll Normal file

Binary file not shown.

BIN
lib/jacob-1.18-x86.dll Normal file

Binary file not shown.

36
pom.xml Normal file
View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.loafle</groupId>
<artifactId>maven_parent_jar</artifactId>
<version>1.0.0-RELEASE</version>
</parent>
<groupId>com.loafle.overflow</groupId>
<artifactId>crawler_wmi</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>com.loafle.overflow.crawler_wmi</name>
<dependencies>
<dependency>
<groupId>com.loafle.overflow</groupId>
<artifactId>crawler_java</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.hynnet/jacob -->
<dependency>
<groupId>com.hynnet</groupId>
<artifactId>jacob</artifactId>
<version>1.18</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,18 @@
package com.loafle.overflow.crawler.wmi;
import com.loafle.overflow.crawler.Crawler;
import java.util.Map;
/**
* Created by root on 17. 4. 11.
*/
public class WMICrawler extends Crawler {
public Object getInternal(Map<String, Object> map) throws Exception {
return WMICrawlerOS.getInstance().processWMI(map);
}
}

View File

@ -0,0 +1,107 @@
package com.loafle.overflow.crawler.wmi;
import java.util.*;
/**
* Created by root on 17. 4. 12.
*/
public class WMICrawlerLinux extends WMICrawlerOS {
private final String DELIMITER = "||";
private final String DELIMITER_SPLIT = "\\|\\|";
public Object processWMI(Map<String, Object> params) throws Exception {
return processCommand(params);
}
public Object processCommand(Map<String, Object> params) throws Exception {
// String id = "administrator";
// String pw = "dbseogns18";
// String nameSpace = "root/cimv2";
// String query = "select * from Win32_OperatingSystem";
// String ip = "192.168.1.106";
String id = (String)params.get("id");
String pw = (String)params.get("pw");
String nameSpace = (String)params.get("nameSpace");
String query = (String)params.get("query");
String ip = (String)params.get("ip");
List<String> argList = new ArrayList<String>();
argList.add("/home/snoop/temp/wmic");
argList.add("-U");
argList.add(id + "%" + pw);
argList.add("//" + ip);
argList.add(query);
argList.add("--namespace=" + nameSpace);
argList.add("--delimiter=" + DELIMITER);
return execute(argList.toArray(new String[argList.size()]));
}
public Object execute(String... args) throws Exception {
System.out.println("");
ProcessBuilder builder = new ProcessBuilder(args);
Process process = builder.start();
byte[] msg = new byte[1024];
StringBuffer buf = new StringBuffer();
while(process.getInputStream().read(msg) > 0) {
buf.append(new String(msg));
Arrays.fill(msg, (byte)0);
}
return parseResult(buf.toString());
}
public Object parseResult(String result) throws Exception {
if(result == null || result.length() <= 0) {
return null;
}
result = result.trim();
String[] lines = result.split("\\n");
String line = "";
List<Map<String,String>> resultMapList = new ArrayList<Map<String, String>>();
List<String> columns = null;
for (int indexI = 0 ; indexI < lines.length; ++indexI) {
line = lines[indexI].trim();
if (line.length() <= 0) continue;
if(indexI == 0) {
int idx = line.indexOf("CLASS:");
if (idx >= 0 ) {
continue;
}
}
line = line.trim();
if (columns == null) {
String[] datas = line.split(DELIMITER_SPLIT);
columns = Arrays.asList(datas);
continue;
}
String[] datas = line.split(DELIMITER_SPLIT);
Map<String, String> data = new HashMap<String, String>();
for(int indexJ = 0; indexJ < columns.size(); ++indexJ) {
data.put(columns.get(indexJ), datas[indexJ]);
}
resultMapList.add(data);
}
return resultMapList;
}
}

View File

@ -0,0 +1,30 @@
package com.loafle.overflow.crawler.wmi;
import java.util.Map;
/**
* Created by root on 17. 4. 12.
*/
public abstract class WMICrawlerOS {
private static WMICrawlerOS wmiCrawlerOS = null;
public static WMICrawlerOS getInstance() {
if(WMICrawlerOS.wmiCrawlerOS == null) {
String os = System.getProperty("os.name").toLowerCase();
if(os.indexOf("linux") >= 0) {
WMICrawlerOS.wmiCrawlerOS = new WMICrawlerLinux();
} else if(os.indexOf("") >= 0) {
WMICrawlerOS.wmiCrawlerOS = new WMICrawlerWindows();
}
}
return WMICrawlerOS.wmiCrawlerOS;
}
public abstract Object processWMI(Map<String, Object> params) throws Exception;
}

View File

@ -0,0 +1,93 @@
package com.loafle.overflow.crawler.wmi;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by snoop on 2017-04-11.
*/
public class WMICrawlerWindows extends WMICrawlerOS {
public Object processWMI(Map<String, Object> params) {
// String id = "administrator";
// String pw = "!@#$qwer1234";
// String nameSpace = "root/cimv2";
// String query = "select * from Win32_OperatingSystem";
// String ip = "192.168.1.1";
String id = (String)params.get("id");
String pw = (String)params.get("pw");
String nameSpace = (String)params.get("nameSpace");
String query = (String)params.get("query");
String ip = (String)params.get("ip");
ActiveXComponent wmi = null;
wmi = new ActiveXComponent("WbemScripting.SWbemLocator");
Variant conRet = wmi.invoke("ConnectServer", new Variant(ip), new Variant(nameSpace), new Variant(id), new Variant(pw));
ActiveXComponent wmiconnect = new ActiveXComponent(conRet.toDispatch());
Variant vCollection = wmiconnect.invoke("ExecQuery", new Variant(query));
EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
Dispatch item = null;
List<String> columns = null;
Map<String, String> resultMap = null;
List<Map<String, String>> resultMapList = new ArrayList<Map<String, String>>();
while (enumVariant.hasMoreElements()) {
item = enumVariant.nextElement().toDispatch();
if (columns == null) {
columns = getColumns(item);
}
resultMap = new HashMap<String, String>();
String name = null;
String value = null;
for(int indexI = 0 ; indexI < columns.size(); ++indexI) {
name = columns.get(indexI);
value = Dispatch.call(item, name).toString();
resultMap.put(name, value);
}
resultMapList.add(resultMap);
}
return resultMapList;
}
protected List<String> getColumns(Dispatch item) {
List<String> columns = new ArrayList<String>();
Variant propertyesa = Dispatch.call(item, "Properties_");
Variant newEnum = Dispatch.call(propertyesa.toDispatch(), "_NewEnum");
EnumVariant enumv = newEnum.toEnumVariant();
while (enumv.hasMoreElements()) {
Dispatch vv = enumv.nextElement().toDispatch();
String vs = Dispatch.call(vv, "Name").toString();
columns.add(vs);
}
return columns;
}
}

0
src/main/resources/_ Normal file
View File

View File

@ -0,0 +1,86 @@
package com.loafle.overflow.crawler.wmi;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by root on 17. 4. 11.
*/
public class WMICrawlerTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Ignore
@Test
public void testLinuxExe() throws Exception {
String command = "/home/snoop/temp/wmic";
ProcessBuilder builder = new ProcessBuilder(command, "-U","administrator%!@#$qwer1234", "//192.168.1.1", "select * from Win32_OperatingSystem", "--namespace=root/cimv2", "--delimiter=||");
Process process = builder.start();
byte[] msg = new byte[1024];
StringBuffer buf = new StringBuffer();
while(process.getInputStream().read(msg) > 0) {
buf.append(new String(msg));
Arrays.fill(msg, (byte)0);
}
System.out.println(buf.toString());
}
@Test
public void testWMI() throws Exception {
WMICrawler wmiCrawler = new WMICrawler();
Map<String, Object> map = new HashMap<String, Object>();
String id = "administrator";
String pw = "!@#$qwer1234";
String nameSpace = "root/cimv2";
String query = "select * from Win32_OperatingSystem";
String ip = "192.168.1.1";
map.put("id", "administrator");
map.put("pw", "!@#$qwer1234");
map.put("nameSpace", "root/cimv2");
map.put("query", "select * from Win32_OperatingSystem");
map.put("ip", "192.168.1.1");
List<Map<String, String>> resultList = (List<Map<String, String>>)wmiCrawler.getInternal(map);
for( Map<String,String> rMap : resultList) {
for( String key : rMap.keySet() ){
System.out.println( String.format("key : %s ||| value : %s", key, rMap.get(key)) );
}
}
}
@Test
public void testOS() {
System.out.println(System.getProperty("os.name"));
}
}

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="3 seconds">
<contextName>crawler_wmi</contextName>
<!-- TRACE > DEBUG > INFO > WARN > ERROR -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{32} - %msg%n
</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
<logger name="com.loafle.overflow" level="ALL" />
</configuration>