ruby_stix 0.0.2-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,169 @@
1
+ require 'stringio'
2
+
3
+ module StixRuby::Marshall
4
+
5
+ def self.included(parent)
6
+ parent.send(:include, StixRuby::Marshall::InstanceMethods)
7
+ parent.send(:extend, StixRuby::Marshall::ClassMethods)
8
+ end
9
+
10
+ module InstanceMethods
11
+ def write(io, args = {})
12
+ clean_namespaces = args[:clean_namespaces] != false
13
+
14
+ if clean_namespaces
15
+ document = self.class.new_dom
16
+ self.class.marshaller.marshal(self, document)
17
+
18
+ namespaces = {}
19
+ namespaces[StixRuby::NAMESPACE_MAPPINGS[document.document_element.namespace_uri]] = true
20
+ for i in 0...document.document_element.child_nodes.length
21
+ collect_namespaces(document.document_element.child_nodes.item(i), namespaces, )
22
+ end
23
+
24
+ to_delete = []
25
+
26
+ for i in 0...document.document_element.attributes.length
27
+ attribute = document.document_element.attributes.item(i)
28
+ if attribute.name =~ /xmlns/
29
+ if !(namespaces[attribute.name.split(':').last] || namespaces[attribute.value])
30
+ to_delete.push(attribute.name)
31
+ end
32
+ end
33
+ end
34
+
35
+ to_delete.each {|i| document.document_element.remove_attribute(i)}
36
+ io = to_java_io(io)
37
+
38
+ self.class.dom_writer.set_output_property(javax.xml.transform.OutputKeys::INDENT, args[:no_formatting] == true ? 'no' : 'yes');
39
+ self.class.dom_writer.transform(javax.xml.transform.dom.DOMSource.new(document), javax.xml.transform.stream.StreamResult.new(io))
40
+ else
41
+ io = to_java_io(io)
42
+ self.class.marshaller.set_property(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, !(args[:no_formatting] == true))
43
+ self.class.marshaller.marshal(self, io)
44
+ end
45
+
46
+ return io
47
+ end
48
+
49
+ def to_java_io(io)
50
+ if io.kind_of?(StringIO) || io.kind_of?(File)
51
+ io.to_outputstream
52
+ elsif io.kind_of?(String)
53
+ sio = StringIO.new(io)
54
+ sio.to_outputstream
55
+ else
56
+ io
57
+ end
58
+ end
59
+
60
+ def to_xml
61
+ io = StringIO.new
62
+ write(io)
63
+ io.string
64
+ end
65
+
66
+ private
67
+
68
+ def collect_namespaces(node, coll)
69
+ coll[node.namespace_uri] = true
70
+ for i in 0...node.child_nodes.length
71
+ collect_namespaces(node.child_nodes.item(i), coll)
72
+ end
73
+
74
+ attributes = node.get_attributes || []
75
+ for i in 0...attributes.length
76
+ attribute = node.get_attributes.item(i)
77
+ coll[attribute.namespace_uri] = true
78
+
79
+ if attribute.namespace_uri == 'http://www.w3.org/2001/XMLSchema-instance' && attribute.value =~ /:/
80
+ coll[attribute.value.split(':').first] = true
81
+ elsif attribute.local_name == 'id' || attribute.local_name == 'idref'
82
+ coll[attribute.value.split(':').first] = true
83
+ end
84
+ end
85
+ coll
86
+ end
87
+ end
88
+
89
+ module ClassMethods
90
+
91
+ def from_xml(input)
92
+ unmarshaller.unmarshal(to_java_inio(input))
93
+ end
94
+
95
+ def to_java_inio(io)
96
+ if io.kind_of?(StringIO) || io.kind_of?(File)
97
+ io.to_inputstream
98
+ elsif io.kind_of?(String)
99
+ sio = StringIO.new(io)
100
+ sio.to_inputstream
101
+ else
102
+ io
103
+ end
104
+ end
105
+
106
+ def dom_writer
107
+ if @transformer.nil?
108
+ factory = javax.xml.transform.TransformerFactory.new_instance
109
+ @transformer = factory.new_transformer
110
+ @transformer.set_output_property("{http://xml.apache.org/xslt}indent-amount", "4")
111
+ end
112
+ return @transformer
113
+ end
114
+
115
+ def jaxb_context
116
+ @context ||= javax.xml.bind.JAXBContext.new_instance(org.mitre.stix.core.STIXType.java_class)
117
+ end
118
+
119
+ def marshaller
120
+ if @marshaller.nil?
121
+ @marshaller = jaxb_context.create_marshaller
122
+ @marshaller.set_property("com.sun.xml.internal.bind.namespacePrefixMapper", StixRuby::Marshall::StixNamespaceMapper.new);
123
+ end
124
+
125
+ return @marshaller
126
+ end
127
+
128
+ def unmarshaller
129
+ if @unmarshaller.nil?
130
+ @unmarshaller = jaxb_context.create_unmarshaller
131
+ end
132
+
133
+ return @unmarshaller
134
+ end
135
+
136
+ def new_dom
137
+ if @document_builder.nil?
138
+ dbf = javax.xml.parsers.DocumentBuilderFactory.new_instance
139
+ dbf.namespace_aware = true
140
+ @document_builder = dbf.new_document_builder
141
+ end
142
+ @document_builder.new_document
143
+ end
144
+ end
145
+
146
+ class StixNamespaceMapper < com.sun.xml.internal.bind.marshaller.NamespacePrefixMapper
147
+ def initialize(mappings = {})
148
+ super()
149
+ @mappings = mappings
150
+ end
151
+
152
+ def getPreferredPrefix(uri, suggestion, require_prefix)
153
+ if @mappings[uri]
154
+ @mappings[uri]
155
+ elsif uri == StixRuby.id_namespace_uri
156
+ return StixRuby.id_namespace_prefix
157
+ elsif StixRuby::NAMESPACE_MAPPINGS[uri]
158
+ return StixRuby::NAMESPACE_MAPPINGS[uri]
159
+ else
160
+ return suggestion
161
+ end
162
+ end
163
+
164
+ def getPreDeclaredNamespaceUris
165
+ StixRuby::NAMESPACE_MAPPINGS.keys + [StixRuby.id_namespace_uri, ''].compact + @mappings.keys
166
+ end
167
+
168
+ end
169
+ end
File without changes
@@ -0,0 +1,3 @@
1
+ module StixRuby
2
+ VERSION = "0.0.2"
3
+ end
data/lib/ruby_stix.rb ADDED
@@ -0,0 +1,191 @@
1
+ java_import 'javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter'
2
+ require "ruby_stix/version"
3
+ require "cybox_bindings.jar"
4
+ require "stix_bindings.jar"
5
+ require "securerandom"
6
+
7
+ module StixRuby
8
+
9
+ module Aliases
10
+ STIXPackage =org.mitre.stix.core.STIXType
11
+ Campaign =org.mitre.stix.campaign.CampaignType
12
+ CourseOfAction =org.mitre.stix.coa.CourseOfActionType
13
+ ExploitTarget =org.mitre.stix.et.ExploitTargetType
14
+ Incident =org.mitre.stix.incident.IncidentType
15
+ Indicator =org.mitre.stix.indicator.IndicatorType
16
+ ThreatActor =org.mitre.stix.ta.ThreatActorType
17
+ TTP =org.mitre.stix.ttp.TTPType
18
+
19
+ Observables =org.mitre.cybox.core.ObservablesType
20
+ Observable =org.mitre.cybox.core.ObservableType
21
+
22
+ module Stix
23
+ Core =org.mitre.stix.core
24
+ Common =org.mitre.stix.common
25
+ Vocabs =org.mitre.stix.vocabularies
26
+ end
27
+
28
+ module Cybox
29
+ Core =org.mitre.cybox.core
30
+ Common =org.mitre.cybox.common
31
+ Vocabs =org.mitre.cybox.vocabularies
32
+ end
33
+ end
34
+
35
+ @id_namespace = nil
36
+ def self.set_id_namespace(namespace, prefix)
37
+ @id_namespace = {:uri => namespace, :prefix => prefix}
38
+ end
39
+
40
+ def self.id_namespace_uri
41
+ if @id_namespace
42
+ @id_namespace[:uri]
43
+ else
44
+ nil
45
+ end
46
+ end
47
+
48
+ def self.id_namespace_prefix
49
+ if @id_namespace
50
+ @id_namespace[:prefix]
51
+ else
52
+ nil
53
+ end
54
+ end
55
+
56
+ def self.generate_id(obj_type)
57
+ if @id_namespace
58
+ javax.xml.namespace.QName.new(@id_namespace[:uri], "#{obj_type}-#{SecureRandom.uuid}", @id_namespace[:prefix])
59
+ else
60
+ javax.xml.namespace.QName.new("#{obj_type}-#{SecureRandom.uuid}")
61
+ end
62
+ end
63
+
64
+ def self.qname(localpart, uri = nil, prefix = nil)
65
+ if prefix
66
+ javax.xml.namespace.QName.new(uri, localpart, prefix)
67
+ elsif uri
68
+ javax.xml.namespace.QName.new(uri, localpart)
69
+ elsif @id_namespace
70
+ javax.xml.namespace.QName.new(@id_namespace[:uri], localpart, @id_namespace[:prefix])
71
+ else
72
+ javax.xml.namespace.QName.new(localpart)
73
+ end
74
+ end
75
+
76
+ IRREGULARS = {
77
+ Regexp.new("tt_ps") => "ttps"
78
+ }
79
+
80
+ NAMESPACE_MAPPINGS = {
81
+ "http://cybox.mitre.org/common-2"=>"cyboxCommon",
82
+ "http://cybox.mitre.org/cybox-2"=>"cybox",
83
+ "http://cybox.mitre.org/default_vocabularies-2"=>"cyboxVocabs",
84
+ "http://cybox.mitre.org/objects#AccountObject-2"=>"AccountObj",
85
+ "http://cybox.mitre.org/objects#AddressObject-2"=>"AddressObj",
86
+ "http://cybox.mitre.org/objects#APIObject-2"=>"APIObj",
87
+ "http://cybox.mitre.org/objects#ArtifactObject-2"=>"ArtifactObj",
88
+ "http://cybox.mitre.org/objects#CodeObject-2"=>"CodeObj",
89
+ "http://cybox.mitre.org/objects#CustomObject-1"=>"CustomObj",
90
+ "http://cybox.mitre.org/objects#DeviceObject-2"=>"DeviceObj",
91
+ "http://cybox.mitre.org/objects#DiskObject-2"=>"DiskObj",
92
+ "http://cybox.mitre.org/objects#DiskPartitionObject-2"=>"DiskPartitionObj",
93
+ "http://cybox.mitre.org/objects#DNSCacheObject-2"=>"DNSCacheObj",
94
+ "http://cybox.mitre.org/objects#DNSRecordObject-2"=>"DNSRecordObj",
95
+ "http://cybox.mitre.org/objects#URIObject-2"=>"URIObj",
96
+ "http://cybox.mitre.org/objects#DNSQueryObject-2"=>"DNSQueryObj",
97
+ "http://cybox.mitre.org/objects#EmailMessageObject-2"=>"EmailMessageObj",
98
+ "http://cybox.mitre.org/objects#FileObject-2"=>"FileObj",
99
+ "http://cybox.mitre.org/objects#GUIDialogboxObject-2"=>"GUIDialogBoxObj",
100
+ "http://cybox.mitre.org/objects#GUIObject-2"=>"GUIObj",
101
+ "http://cybox.mitre.org/objects#GUIWindowObject-2"=>"GUIWindowObj",
102
+ "http://cybox.mitre.org/objects#HTTPSessionObject-2"=>"HTTPSessionObj",
103
+ "http://cybox.mitre.org/objects#PortObject-2"=>"PortObj",
104
+ "http://cybox.mitre.org/objects#LibraryObject-2"=>"LibraryObj",
105
+ "http://cybox.mitre.org/objects#LinkObject-1"=>"LinkObj",
106
+ "http://cybox.mitre.org/objects#LinuxPackageObject-2"=>"LinuxPackageObj",
107
+ "http://cybox.mitre.org/objects#MemoryObject-2"=>"MemoryObj",
108
+ "http://cybox.mitre.org/objects#MutexObject-2"=>"MutexObj",
109
+ "http://cybox.mitre.org/objects#NetworkConnectionObject-2"=>"NetworkConnectionObj",
110
+ "http://cybox.mitre.org/objects#SocketAddressObject-1"=>"SocketAddressObj",
111
+ "http://cybox.mitre.org/objects#NetworkFlowObject-2"=>"NetFlowObj",
112
+ "http://cybox.mitre.org/objects#PacketObject-2"=>"PacketObj",
113
+ "http://cybox.mitre.org/objects#NetworkRouteEntryObject-2"=>"NetworkRouteEntryObj",
114
+ "http://cybox.mitre.org/objects#NetworkRouteObject-2"=>"NetworkRouteObj",
115
+ "http://cybox.mitre.org/objects#NetworkSocketObject-2"=>"NetworkSocketObj",
116
+ "http://cybox.mitre.org/objects#NetworkSubnetObject-2"=>"NetworkSubnetObj",
117
+ "http://cybox.mitre.org/objects#PDFFileObject-1"=>"PDFFileObj",
118
+ "http://cybox.mitre.org/objects#PipeObject-2"=>"PipeObj",
119
+ "http://cybox.mitre.org/objects#ProcessObject-2"=>"ProcessObj",
120
+ "http://cybox.mitre.org/objects#ProductObject-2"=>"ProductObj",
121
+ "http://cybox.mitre.org/objects#SemaphoreObject-2"=>"SemaphoreObj",
122
+ "http://cybox.mitre.org/objects#SystemObject-2"=>"SystemObj",
123
+ "http://cybox.mitre.org/objects#UnixFileObject-2"=>"UnixFileObj",
124
+ "http://cybox.mitre.org/objects#UnixNetworkRouteEntryObject-2"=>"UnixNetworkRouteEntryObj",
125
+ "http://cybox.mitre.org/objects#UnixPipeObject-2"=>"UnixPipeObj",
126
+ "http://cybox.mitre.org/objects#UnixProcessObject-2"=>"UnixProcessObj",
127
+ "http://cybox.mitre.org/objects#UnixUserAccountObject-2"=>"UnixUserAccountObj",
128
+ "http://cybox.mitre.org/objects#UserAccountObject-2"=>"UserAccountObj",
129
+ "http://cybox.mitre.org/objects#UnixVolumeObject-2"=>"UnixVolumeObj",
130
+ "http://cybox.mitre.org/objects#VolumeObject-2"=>"VolumeObj",
131
+ "http://cybox.mitre.org/objects#UserSessionObject-2"=>"UserSessionObj",
132
+ "http://cybox.mitre.org/objects#WhoisObject-2"=>"WhoisObj",
133
+ "http://cybox.mitre.org/objects#WinComputerAccountObject-2"=>"WinComputerAccountObj",
134
+ "http://cybox.mitre.org/objects#WinCriticalSectionObject-2"=>"WinCriticalSectionObj",
135
+ "http://cybox.mitre.org/objects#WinDriverObject-2"=>"WinDriverObj",
136
+ "http://cybox.mitre.org/objects#WinEventLogObject-2"=>"WinEventLogObj",
137
+ "http://cybox.mitre.org/objects#WinEventObject-2"=>"WinEventObj",
138
+ "http://cybox.mitre.org/objects#WinHandleObject-2"=>"WinHandleObj",
139
+ "http://cybox.mitre.org/objects#WinExecutableFileObject-2"=>"WinExecutableFileObj",
140
+ "http://cybox.mitre.org/objects#WinFileObject-2"=>"WinFileObj",
141
+ "http://cybox.mitre.org/objects#WinKernelHookObject-2"=>"WinKernelHookObj",
142
+ "http://cybox.mitre.org/objects#WinKernelObject-2"=>"WinKernelObj",
143
+ "http://cybox.mitre.org/objects#WinMailslotObject-2"=>"WinMailslotObj",
144
+ "http://cybox.mitre.org/objects#WinMemoryPageRegionObject-2"=>"WinMemoryPageRegionObj",
145
+ "http://cybox.mitre.org/objects#WinMutexObject-2"=>"WinMutexObj",
146
+ "http://cybox.mitre.org/objects#WinNetworkRouteEntryObject-2"=>"WinNetworkRouteEntryObj",
147
+ "http://cybox.mitre.org/objects#WinNetworkShareObject-2"=>"WinNetworkShareObj",
148
+ "http://cybox.mitre.org/objects#WinPipeObject-2"=>"WinPipeObj",
149
+ "http://cybox.mitre.org/objects#WinPrefetchObject-2"=>"WinPrefetchObj",
150
+ "http://cybox.mitre.org/objects#WinVolumeObject-2"=>"WinVolumeObj",
151
+ "http://cybox.mitre.org/objects#WinProcessObject-2"=>"WinProcessObj",
152
+ "http://cybox.mitre.org/objects#WinRegistryKeyObject-2"=>"WinRegistryKeyObj",
153
+ "http://cybox.mitre.org/objects#WinSemaphoreObject-2"=>"WinSemaphoreObj",
154
+ "http://cybox.mitre.org/objects#WinServiceObject-2"=>"WinServiceObj",
155
+ "http://cybox.mitre.org/objects#WinSystemObject-2"=>"WinSystemObj",
156
+ "http://cybox.mitre.org/objects#WinSystemRestoreObject-2"=>"WinSystemRestoreObj",
157
+ "http://cybox.mitre.org/objects#WinTaskObject-2"=>"WinTaskObj",
158
+ "http://cybox.mitre.org/objects#WinThreadObject-2"=>"WinThreadObj",
159
+ "http://cybox.mitre.org/objects#WinUserAccountObject-2"=>"WinUserAccountObj",
160
+ "http://cybox.mitre.org/objects#WinWaitableTimerObject-2"=>"WinWaitableTimerObj",
161
+ "http://cybox.mitre.org/objects#X509CertificateObject-2"=>"X509CertificateObj",
162
+ "http://stix.mitre.org/Campaign-1"=>"campaign",
163
+ "http://stix.mitre.org/common-1"=>"stixCommon",
164
+ "http://data-marking.mitre.org/Marking-1"=>"marking",
165
+ "http://stix.mitre.org/CourseOfAction-1"=>"coa",
166
+ "http://stix.mitre.org/ExploitTarget-1"=>"et",
167
+ "http://stix.mitre.org/Incident-1"=>"incident",
168
+ "http://stix.mitre.org/Indicator-2"=>"indicator",
169
+ "http://stix.mitre.org/stix-1"=>"stix",
170
+ "http://stix.mitre.org/default_vocabularies-1"=>"stixVocabs",
171
+ "http://stix.mitre.org/ThreatActor-1"=>"ta",
172
+ "http://stix.mitre.org/TTP-1"=>"ttp",
173
+ "http://stix.mitre.org/extensions/Address#CIQAddress3.0-1"=>"stixCiqAddress",
174
+ "http://stix.mitre.org/extensions/AP#CAPEC2.6-1"=>"stixCapec",
175
+ "http://stix.mitre.org/extensions/Identity#CIQIdentity3.0-1"=>"stixCiqIdentity",
176
+ "http://stix.mitre.org/extensions/Malware#MAEC4.0-1"=>"stixMaec",
177
+ "http://data-marking.mitre.org/extensions/MarkingStructure#Simple-1"=>"simpleMarking",
178
+ "http://data-marking.mitre.org/extensions/MarkingStructure#TLP-1"=>"tlpMarking",
179
+ "http://stix.mitre.org/extensions/StructuredCOA#Generic-1"=>"genericStructuredCOA",
180
+ "http://stix.mitre.org/extensions/TestMechanism#Generic-1"=>"genericTM",
181
+ "http://stix.mitre.org/extensions/TestMechanism#OpenIOC2010-1"=>"openiocTM",
182
+ "http://stix.mitre.org/extensions/TestMechanism#OVAL5.10-1"=>"ovalTM",
183
+ "http://stix.mitre.org/extensions/TestMechanism#Snort-1"=>"snortTM",
184
+ "http://stix.mitre.org/extensions/TestMechanism#YARA-1"=>"yaraTM",
185
+ "http://stix.mitre.org/extensions/Vulnerability#CVRF-1"=>"stixCvrf",
186
+ "urn:oasis:names:tc:ciq:xpil:3"=>"xpil",
187
+
188
+ 'http://www.w3.org/2001/XMLSchema-instance'=>'xsi',
189
+ 'http://www.w3.org/2001/XMLSchema'=>'xsd'
190
+ }
191
+ end
Binary file
data/ruby_stix.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ruby_stix/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "ruby_stix"
8
+ gem.version = StixRuby::VERSION
9
+ gem.authors = ["John Wunder"]
10
+ gem.email = ["jwunder@mitre.org"]
11
+ gem.description = %q{Bindings and APIs for STIX and CybOX}
12
+ gem.summary = %q{Bindings and APIs for STIX and CybOX}
13
+ gem.homepage = ""
14
+ gem.platform = "java"
15
+
16
+ gem.add_development_dependency 'nokogiri'
17
+ gem.add_dependency 'activesupport', '>=4.0.0'
18
+
19
+ gem.files = `git ls-files`.split($/)
20
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
21
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
22
+ gem.require_paths = ["lib"]
23
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'ruby_stix'
5
+ require 'ruby_stix/api'
6
+
7
+ RSpec.configure do |config|
8
+ # nothing to configure really...
9
+ end