<s> import base64 <EOL> import bz2 <EOL> import zlib <EOL> from mixbox import entities <EOL> from mixbox import fields <EOL> from mixbox . vendor import six <EOL> import cybox . bindings . artifact_object as artifact_binding <EOL> from cybox . common import ObjectProperties , String <EOL> from cybox . compat import xor <EOL> class RawArtifact ( String ) : <EOL> _binding_class = artifact_binding . RawArtifactType <EOL> _namespace = '<STR_LIT>' <EOL> byte_order = fields . TypedField ( "<STR_LIT>" ) <EOL> class Artifact ( ObjectProperties ) : <EOL> _binding = artifact_binding <EOL> _namespace = '<STR_LIT>' <EOL> _XSI_NS = "<STR_LIT>" <EOL> _XSI_TYPE = "<STR_LIT>" <EOL> TYPE_FILE = "<STR_LIT>" <EOL> TYPE_MEMORY = "<STR_LIT>" <EOL> TYPE_FILE_SYSTEM = "<STR_LIT>" <EOL> TYPE_NETWORK = "<STR_LIT>" <EOL> TYPE_GENERIC = "<STR_LIT>" <EOL> def __init__ ( self , data = None , type_ = None ) : <EOL> super ( Artifact , self ) . __init__ ( ) <EOL> self . type_ = type_ <EOL> self . packaging = [ ] <EOL> self . _packed_data = None <EOL> self . data = data <EOL> @ property <EOL> def data ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _data : <EOL> return self . _data <EOL> elif self . _packed_data : <EOL> tmp_data = self . _packed_data . encode ( '<STR_LIT:ascii>' ) <EOL> for p in reversed ( self . packaging ) : <EOL> tmp_data = p . unpack ( tmp_data ) <EOL> return tmp_data <EOL> else : <EOL> return None <EOL> @ data . setter <EOL> def data ( self , value ) : <EOL> if self . _packed_data : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if value is not None and not isinstance ( value , six . binary_type ) : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise ValueError ( msg ) <EOL> self . _data = value <EOL> @ property <EOL> def packed_data ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _packed_data : <EOL> return self . _packed_data <EOL> elif self . _data : <EOL> tmp_data = self . _data <EOL> for p in self . packaging : <EOL> tmp_data = p . pack ( tmp_data ) <EOL> return tmp_data . decode ( '<STR_LIT:ascii>' ) <EOL> else : <EOL> return None <EOL> @ packed_data . setter <EOL> def packed_data ( self , value ) : <EOL> if self . _data : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if value is not None and not isinstance ( value , six . text_type ) : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise ValueError ( msg ) <EOL> self . _packed_data = value <EOL> def to_obj ( self , return_obj = None , ns_info = None ) : <EOL> self . _collect_ns_info ( ns_info ) <EOL> artifact_obj = artifact_binding . ArtifactObjectType ( ) <EOL> super ( Artifact , self ) . to_obj ( return_obj = artifact_obj , ns_info = ns_info ) <EOL> if self . packaging : <EOL> packaging = artifact_binding . PackagingType ( ) <EOL> for p in self . packaging : <EOL> p_obj = p . to_obj ( ns_info = ns_info ) <EOL> if isinstance ( p , Compression ) : <EOL> packaging . add_Compression ( p_obj ) <EOL> elif isinstance ( p , Encryption ) : <EOL> packaging . add_Encryption ( p_obj ) <EOL> elif isinstance ( p , Encoding ) : <EOL> packaging . add_Encoding ( p_obj ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> type ( p ) ) <EOL> artifact_obj . Packaging = packaging <EOL> if self . packed_data : <EOL> artifact_obj . Raw_Artifact = RawArtifact ( self . packed_data ) . to_obj ( ns_info = ns_info ) <EOL> artifact_obj . type_ = self . type_ <EOL> return artifact_obj <EOL> def to_dict ( self ) : <EOL> artifact_dict = { } <EOL> super ( Artifact , self ) . to_dict ( artifact_dict ) <EOL> if self . packaging : <EOL> artifact_dict [ '<STR_LIT>' ] = [ p . to_dict ( ) for p in self . packaging ] <EOL> if self . packed_data : <EOL> artifact_dict [ '<STR_LIT>' ] = RawArtifact ( self . packed_data ) . to_dict ( ) <EOL> if self . type_ : <EOL> artifact_dict [ '<STR_LIT:type>' ] = self . type_ <EOL> return artifact_dict <EOL> @ staticmethod <EOL> def from_obj ( artifact_obj ) : <EOL> if not artifact_obj : <EOL> return None <EOL> artifact = Artifact ( ) <EOL> ObjectProperties . from_obj ( artifact_obj , artifact ) <EOL> packaging = artifact_obj . Packaging <EOL> if packaging : <EOL> for c in packaging . Compression : <EOL> artifact . packaging . append ( Compression . from_obj ( c ) ) <EOL> for e in packaging . Encryption : <EOL> artifact . packaging . append ( Encryption . from_obj ( e ) ) <EOL> for e in packaging . Encoding : <EOL> artifact . packaging . append ( Encoding . from_obj ( e ) ) <EOL> raw_artifact = artifact_obj . Raw_Artifact <EOL> if raw_artifact : <EOL> data = RawArtifact . from_obj ( raw_artifact ) . value <EOL> artifact . packed_data = six . text_type ( data ) <EOL> artifact . type_ = artifact_obj . type_ <EOL> return artifact <EOL> @ staticmethod <EOL> def from_dict ( artifact_dict ) : <EOL> if not artifact_dict : <EOL> return None <EOL> artifact = Artifact ( ) <EOL> ObjectProperties . from_dict ( artifact_dict , artifact ) <EOL> for layer in artifact_dict . get ( '<STR_LIT>' , [ ] ) : <EOL> if layer . get ( '<STR_LIT>' ) == "<STR_LIT>" : <EOL> artifact . packaging . append ( Compression . from_dict ( layer ) ) <EOL> if layer . get ( '<STR_LIT>' ) == "<STR_LIT>" : <EOL> artifact . packaging . append ( Encryption . from_dict ( layer ) ) <EOL> if layer . get ( '<STR_LIT>' ) == "<STR_LIT>" : <EOL> artifact . packaging . append ( Encoding . from_dict ( layer ) ) <EOL> raw_artifact = artifact_dict . get ( '<STR_LIT>' ) <EOL> if raw_artifact : <EOL> data = RawArtifact . from_dict ( raw_artifact ) . value <EOL> artifact . packed_data = six . text_type ( data ) <EOL> artifact . type_ = artifact_dict . get ( '<STR_LIT:type>' ) <EOL> return artifact <EOL> class Packaging ( entities . Entity ) : <EOL> """<STR_LIT>""" <EOL> _namespace = '<STR_LIT>' <EOL> def pack ( self , data ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def unpack ( self , packed_data ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> class Compression ( Packaging ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , compression_mechanism = None ) : <EOL> super ( Compression , self ) . __init__ ( ) <EOL> self . compression_mechanism = compression_mechanism <EOL> def to_obj ( self , return_obj = None , ns_info = None ) : <EOL> self . _collect_ns_info ( ns_info ) <EOL> obj = artifact_binding . CompressionType ( ) <EOL> if self . compression_mechanism : <EOL> obj . compression_mechanism = self . compression_mechanism <EOL> return obj <EOL> def to_dict ( self ) : <EOL> dict_ = { } <EOL> dict_ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if self . compression_mechanism : <EOL> dict_ [ '<STR_LIT>' ] = self . compression_mechanism <EOL> return dict_ <EOL> @ staticmethod <EOL> def from_obj ( compression_obj ) : <EOL> mechanism = compression_obj . compression_mechanism <EOL> return Compression . get_object ( mechanism ) <EOL> @ staticmethod <EOL> def from_dict ( compression_dict ) : <EOL> mechanism = compression_dict . get ( '<STR_LIT>' ) <EOL> return Compression . get_object ( mechanism ) <EOL> @ staticmethod <EOL> def get_object ( mechanism ) : <EOL> if mechanism == '<STR_LIT>' : <EOL> return ZlibCompression ( ) <EOL> elif mechanism == "<STR_LIT>" : <EOL> return Bz2Compression ( ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % mechanism ) <EOL> class ZlibCompression ( Compression ) : <EOL> def __init__ ( self ) : <EOL> super ( ZlibCompression , self ) . __init__ ( "<STR_LIT>" ) <EOL> def pack ( self , data ) : <EOL> return zlib . compress ( data ) <EOL> def unpack ( self , packed_data ) : <EOL> return zlib . decompress ( packed_data ) <EOL> class Bz2Compression ( Compression ) : <EOL> def __init__ ( self ) : <EOL> super ( Bz2Compression , self ) . __init__ ( "<STR_LIT>" ) <EOL> def pack ( self , data ) : <EOL> return bz2 . compress ( data ) <EOL> def unpack ( self , packed_data ) : <EOL> return bz2 . decompress ( packed_data ) <EOL> class Encryption ( Packaging ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , encryption_mechanism = None , encryption_key = None ) : <EOL> super ( Encryption , self ) . __init__ ( ) <EOL> self . encryption_mechanism = encryption_mechanism <EOL> self . encryption_key = encryption_key <EOL> def to_obj ( self , return_obj = None , ns_info = None ) : <EOL> self . _collect_ns_info ( ns_info ) <EOL> obj = artifact_binding . EncryptionType ( ) <EOL> if self . encryption_mechanism : <EOL> obj . encryption_mechanism = self . encryption_mechanism <EOL> if self . encryption_key : <EOL> obj . encryption_key = self . encryption_key <EOL> return obj <EOL> def to_dict ( self ) : <EOL> dict_ = { } <EOL> dict_ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if self . encryption_mechanism : <EOL> dict_ [ '<STR_LIT>' ] = self . encryption_mechanism <EOL> if self . encryption_key : <EOL> dict_ [ '<STR_LIT>' ] = self . encryption_key <EOL> return dict_ <EOL> @ staticmethod <EOL> def from_obj ( encryption_obj ) : <EOL> mechanism = encryption_obj . encryption_mechanism <EOL> key = encryption_obj . encryption_key <EOL> return Encryption . get_object ( mechanism , key ) <EOL> @ staticmethod <EOL> def from_dict ( encryption_dict ) : <EOL> mechanism = encryption_dict . get ( '<STR_LIT>' ) <EOL> key = encryption_dict . get ( '<STR_LIT>' ) <EOL> return Encryption . get_object ( mechanism , key ) <EOL> @ staticmethod <EOL> def get_object ( mechanism , key ) : <EOL> if mechanism == '<STR_LIT>' : <EOL> return XOREncryption ( key ) <EOL> if mechanism == '<STR_LIT>' : <EOL> return PasswordProtectedZipEncryption ( key ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % mechanism ) <EOL> class XOREncryption ( Encryption ) : <EOL> def __init__ ( self , key ) : <EOL> super ( XOREncryption , self ) . __init__ ( "<STR_LIT>" , key ) <EOL> def pack ( self , data ) : <EOL> return xor ( data , self . encryption_key ) <EOL> def unpack ( self , packed_data ) : <EOL> return xor ( packed_data , self . encryption_key ) <EOL> class PasswordProtectedZipEncryption ( Encryption ) : <EOL> def __init__ ( self , key ) : <EOL> super ( PasswordProtectedZipEncryption , self ) . __init__ ( "<STR_LIT>" , key ) <EOL> def unpack ( self , packed_data ) : <EOL> from zipfile import ZipFile <EOL> buf = six . StringIO ( packed_data ) <EOL> with ZipFile ( buf , '<STR_LIT:r>' ) as myzip : <EOL> filename = myzip . namelist ( ) [ <NUM_LIT:0> ] <EOL> data = myzip . read ( filename , self . encryption_key ) <EOL> return data <EOL> class Encoding ( Packaging ) : <EOL> """<STR_LIT>""" <EOL> def to_obj ( self , return_obj = None , ns_info = None ) : <EOL> self . _collect_ns_info ( ns_info ) <EOL> obj = artifact_binding . EncodingType ( ) <EOL> return obj <EOL> def to_dict ( self ) : <EOL> dict_ = { } <EOL> dict_ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> dict_ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return dict_ <EOL> @ staticmethod <EOL> def from_obj ( encoding_obj ) : <EOL> if encoding_obj : <EOL> return Base64Encoding ( ) <EOL> @ staticmethod <EOL> def from_dict ( encoding_dict ) : <EOL> if encoding_dict : <EOL> return Base64Encoding ( ) <EOL> class Base64Encoding ( Encoding ) : <EOL> def pack ( self , data ) : <EOL> return base64 . b64encode ( data ) <EOL> def unpack ( self , packed_data ) : <EOL> return base64 . b64decode ( packed_data ) </s>
