-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathStixParsers.java
157 lines (136 loc) · 6.6 KB
/
StixParsers.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package io.digitalstate.stix.json;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.guava.GuavaModule;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import io.digitalstate.stix.bundle.Bundle;
import io.digitalstate.stix.bundle.BundleObject;
import io.digitalstate.stix.bundle.BundleableObject;
import io.digitalstate.stix.common.Stix;
import io.digitalstate.stix.common.StixBoolean;
import io.digitalstate.stix.common.StixInstant;
import io.digitalstate.stix.coo.extension.types.*;
import io.digitalstate.stix.coo.objects.*;
import io.digitalstate.stix.coo.objects.Process;
import io.digitalstate.stix.datamarkings.MarkingDefinition;
import io.digitalstate.stix.datamarkings.objects.Statement;
import io.digitalstate.stix.datamarkings.objects.Tlp;
import io.digitalstate.stix.sdo.objects.*;
import io.digitalstate.stix.sro.objects.Relationship;
import io.digitalstate.stix.sro.objects.Sighting;
import javax.validation.ValidationException;
import java.io.IOException;
/**
* Default JSON Mapper is configured with JsonMapperBase configs + StixSubTypesModule + StixInstantModule
*/
public class StixParsers {
private static ObjectMapper jsonMapper = new ObjectMapper()
.registerModule(new ParameterNamesModule())
.registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule())
.registerModule(new GuavaModule())
.registerModule(generateStixSubTypesModule())
.registerModule(generateStixInstantModule())
.registerModule(generateStixBooleanModule());
/**
* Generates a Base Object Mapper with some generic modules.
*
* @return
*/
public static ObjectMapper generateJsonMapperBase() {
return new ObjectMapper()
.registerModule(new ParameterNamesModule())
.registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule())
.registerModule(new GuavaModule());
}
public static ObjectMapper getJsonMapper() {
return jsonMapper;
}
/**
* Override for setting a custom configured ObjectMapper
*
* @param objectMapper
*/
public static void setJsonMapper(ObjectMapper objectMapper) {
jsonMapper = objectMapper;
}
/**
* Generate a Jackson module for all STIX objects (SDOs, SROs, Markings, bundle, observables, and observable extensions)
*
* @return
*/
public static SimpleModule generateStixSubTypesModule() {
SimpleModule module = new SimpleModule();
Class<?>[] sdoClasses = {AttackPattern.class, Campaign.class, CourseOfAction.class,
Identity.class, Indicator.class, IntrusionSet.class, Malware.class, ObservedData.class,
Report.class, ThreatActor.class, Tool.class, Vulnerability.class};
Class<?>[] sroClasses = {Relationship.class, Sighting.class};
Class<?>[] dataMarkingClasses = {MarkingDefinition.class, Statement.class, Tlp.class};
Class<?>[] bundleClasses = {Bundle.class};
Class<?>[] cyberObservableClasses = {Artifact.class, AutonomousSystem.class, Directory.class,
DomainName.class, EmailAddress.class, EmailMessage.class, File.class, Ipv4Address.class, Ipv6Address.class,
MacAddress.class, Mutex.class, NetworkTraffic.class, Process.class, Software.class, Url.class,
UserAccount.class, WindowsRegistryKey.class, X509Certificate.class};
Class<?>[] cyberObservableExtensionClasses = {ArchiveFileExtension.class, HttpRequestExtension.class, IcmpExtension.class,
NetworkSocketExtension.class, NtfsFileExtenstion.class, PdfFileExtension.class, RasterImageFileExtension.class,
TcpExtension.class, UnixAccountExtension.class, WindowsPeBinaryFileExtension.class, WindowsProcessExtension.class,
WindowsServiceExtension.class};
module.registerSubtypes(sdoClasses);
module.registerSubtypes(sroClasses);
module.registerSubtypes(dataMarkingClasses);
module.registerSubtypes(bundleClasses);
module.registerSubtypes(cyberObservableClasses);
module.registerSubtypes(cyberObservableExtensionClasses);
return module;
}
public static SimpleModule generateStixInstantModule() {
SimpleModule module = new SimpleModule();
module.addSerializer(StixInstant.class, new StixInstantSerializer());
module.addDeserializer(StixInstant.class, new StixInstantDeserializer());
return module;
}
public static SimpleModule generateStixBooleanModule() {
SimpleModule module = new SimpleModule();
module.addSerializer(StixBoolean.class, new StixBooleanSerializer());
module.addDeserializer(StixBoolean.class, new StixBooleanDeserializer());
return module;
}
public static BundleObject parseBundle(String bundleJsonString) throws IOException, StixParserValidationException {
try {
return getJsonMapper().readValue(bundleJsonString, BundleObject.class);
} catch (IOException ex) {
if (ValidationException.class.isAssignableFrom(ex.getCause().getClass())) {
throw new StixParserValidationException((ValidationException) ex.getCause());
} else {
throw ex;
}
}
}
public static BundleableObject parseObject(String objectJsonString) throws IOException, StixParserValidationException {
try {
return getJsonMapper().readValue(objectJsonString, BundleableObject.class);
} catch (IOException ex) {
if (ValidationException.class.isAssignableFrom(ex.getCause().getClass())) {
throw new StixParserValidationException((ValidationException) ex.getCause());
} else {
throw ex;
}
}
}
public static <T extends Stix> T parse(String bundleJsonString, Class<T> stixClass) throws IOException, StixParserValidationException {
try {
return getJsonMapper().readValue(bundleJsonString, stixClass);
} catch (IOException ex) {
if (ValidationException.class.isAssignableFrom(ex.getCause().getClass())) {
throw new StixParserValidationException((ValidationException) ex.getCause());
} else {
throw ex;
}
}
}
}