简体   繁体   English

Base64使用re和base64模块解码字符串的一部分

[英]Base64 decode a section of a string using re and base64 modules

Can anyone demonstrate how you can base64 decode a particular section of a string using a regex search? 谁能证明您如何使用正则表达式搜索对base64字符串的特定部分进行解码? I would like the final result to return the entire string but with the base64 areas decoded. 我希望最终结果返回整个字符串,但已解码base64区域。

Text between the category tags and subcategory tags should be decoded and then entire strinf should be returned. 类别标签和子类别标签之间的文本应被解码,然后应返回整个strinf。

<attack_headline><site_id>1</site_id><category>U1FMIEluamVjdGlvbg==</category><subcategory>Q2xhc3NpYyBTUUwgQ29tbWVudCAmcXVvdDstLSZxdW90Ow==</subcategory><client_ip>192.168.1.102</client_ip><date>1363807248</date><gmt_diff>0</gmt_diff><reference_id>E711-3EFB-5F43-5FAC</reference_id></attack_headline>

Based on my comment, here's an example using lxml.etree , which assumes your input is XML (if HTML, use lxml.html instead): 根据我的评论,这是一个使用lxml.etree的示例,该示例假定您的输入是XML(如果是HTML,请改用lxml.html ):

>>> import base64
>>> import lxml.etree
>>> text = "<attack_headline><site_id>1</site_id><category>U1FMIEluamVjdGlvbg==</category><subcategory>Q2xhc3NpYyBTUUwgQ29tbWVudCAmcXVvdDstLSZxdW90Ow==</subcategory><client_ip>192.168.1.102</client_ip><date>1363807248</date><gmt_diff>0</gmt_diff><reference_id>E711-3EFB-5F43-5FAC</reference_id></attack_headline>"
>>> xml = lxml.etree.fromstring(text)
>>> for tag_with_base64 in ('category','subcategory'):
...     node = xml.find(tag_with_base64)
...     if node:
...         node.text = base64.b64decode(node.text)
>>> lxml.etree.tostring(xml)
'<attack_headline><site_id>1</site_id><category>SQL Injection</category><subcategory>Classic SQL Comment &amp;quot;--&amp;quot;</subcategory><client_ip>192.168.1.102</client_ip><date>1363807248</date><gmt_diff>0</gmt_diff><reference_id>E711-3EFB-5F43-5FAC</reference_id></attack_headline>'
events = client.service.get_recent_attacks("",epoch_time_last,epoch_time_now,1,"",15)
text = re.sub('(?<!<\/attack_headline>)\s*\n\s*', '',  events)
xml = lxml.etree.fromstring(text)
for tag_with_base64 in ('category','subcategory'):
    node = xml.find(tag_with_base64)
    node.text = base64.b64decode(node.text)
lxml.etree.tostring(xml)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM