/
HashComposers.java
164 lines (144 loc) · 6.09 KB
/
HashComposers.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
158
159
160
161
162
163
164
/**
* Copyright © 2014, University of Washington
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the University of Washington nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package edu.uw.apl.stix.utils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.mitre.cybox.cybox_2.ObjectType;
import org.mitre.cybox.default_vocabularies_2.HashNameVocab10;
import org.mitre.cybox.common_2.ControlledVocabularyStringType;
import org.mitre.cybox.common_2.ObjectPropertiesType;
import org.mitre.cybox.common_2.HashListType;
import org.mitre.cybox.common_2.HashType;
import org.mitre.cybox.common_2.SimpleHashValueType;
import org.mitre.cybox.common_2.StringObjectPropertyType;
import org.mitre.cybox.cybox_2.ObservableType;
import org.mitre.cybox.cybox_2.ObservablesType;
import org.mitre.cybox.objects.FileObjectType;
import org.mitre.stix.stix_1.STIXType;
/**
* @author Stuart Maclean
*
* All things concerning hash string authoring in STIX packages, used
* to e.g. build Observables/Indicators representing md5,shaX digests
* of files of interest.
*/
public class HashComposers {
/**
*
* @param hashes a list of md5 hashes, in hexBinary, to be inserted into
* a STIX package. Looking at the insertion as an XPath problem, we will
* be creating SimpleHashValueTypes according to this path
*
* "/stix:STIX_Package/stix:Observables/cybox:Observable/cybox:Object/cybox:Properties/FileObj:Hashes/cyboxCommon:Hash/cyboxCommon:Simple_Hash_Value"
*
* @return - a new STIX package containing the supplied hashes as
* file Observables. The instance document will look something like
*
* <pre>
* {@code
<ns114:Observables cybox_major_version="2" cybox_minor_version="1">
<ns7:Observable>
<ns7:Object>
<ns7:Properties xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns31:FileObjectType">
<ns31:Hashes>
<ns3:Hash>
<ns3:Type xmlns:ns124="http://cybox.mitre.org/default_vocabularies-2" xsi:type="ns124:HashNameVocab-1.0">md5</ns3:Type>
<ns3:Simple_Hash_Value>12345678901234567890123456789012</ns3:Simple_Hash_Value>
</ns3:Hash>
</ns31:Hashes>
</ns7:Properties>
</ns7:Object>
</ns7:Observable>
</ns114:Observables>
* }
* </pre>
*/
static public STIXType composeMD5HashObservables( List<String> hashes ) {
org.mitre.stix.stix_1.ObjectFactory of =
new org.mitre.stix.stix_1.ObjectFactory();
STIXType result = of.createSTIXType();
org.mitre.cybox.cybox_2.ObjectFactory of2 =
new org.mitre.cybox.cybox_2.ObjectFactory();
ObservablesType ot = of2.createObservablesType();
ot.setCyboxMajorVersion( "2" );
ot.setCyboxMinorVersion( "1" );
result.setObservables( ot );
List<ObservableType> observables = ot.getObservable();
addMD5HashObservables( hashes, observables );
return result;
}
static public void addMD5HashObservables( List<String> hashes,
List<ObservableType> observables) {
List<FileObjectType> fos = asFileObjectHashes( hashes );
for( FileObjectType fo : fos ) {
ObservableType obs = inObservable( fo );
observables.add( obs );
}
}
static ObservableType inObservable( ObjectPropertiesType op ) {
org.mitre.cybox.cybox_2.ObjectFactory of =
new org.mitre.cybox.cybox_2.ObjectFactory();
ObjectType obj = of.createObjectType();
obj.setProperties( op );
ObservableType obs = of.createObservableType();
obs.setObject( obj );
return obs;
}
static public List<FileObjectType> asFileObjectHashes
( List<String> hashes ) {
List<FileObjectType> result = new ArrayList<FileObjectType>();
for( String hash : hashes ) {
FileObjectType fo = asFileObjectHash( hash, "md5" );
result.add( fo );
}
return result;
}
static public FileObjectType asFileObjectHash( String hash,
String algorithm ) {
org.mitre.cybox.common_2.ObjectFactory of =
new org.mitre.cybox.common_2.ObjectFactory();
SimpleHashValueType shvt = of.createSimpleHashValueType();
shvt.setValue( hash );
HashType ht = of.createHashType();
ht.setSimpleHashValue( shvt );
org.mitre.cybox.default_vocabularies_2.ObjectFactory of2 =
new org.mitre.cybox.default_vocabularies_2.ObjectFactory();
HashNameVocab10 hnv = of2.createHashNameVocab10();
hnv.setValue( algorithm );
ht.setType( hnv );
HashListType hlt = of.createHashListType();
List<HashType> hts = hlt.getHash();
hts.add( ht );
org.mitre.cybox.objects.ObjectFactory of3 =
new org.mitre.cybox.objects.ObjectFactory();
FileObjectType result = of3.createFileObjectType();
result.setHashes( hlt );
return result;
}
}
// eof