查找、解码和替换文本文件中的所有 base64 值
Find, decode and replace all base64 values in text file
我有一个 SQL 转储文件,其中包含带有 html 链接的文本,例如:
<a href="http://blahblah.org/kb/getattachment.php?data=NHxUb3Bjb25fZGF0YS1kb3dubG9hZF9ob3d0by5wZGY=">attached file</a>
我想查找、解码和替换每个链接中文本的 base64 部分。
我一直在尝试使用 Python w/正则表达式和 base64 来完成这项工作。但是,我的正则表达式技能无法胜任这项任务。
我需要 select 任何以
开头的字符串
'getattachement.php?data='
并以
结尾
'"'
然后我需要使用 base64.b64decode()
解码 'data=' 和 '"' 之间的部分
结果应该类似于:
<a href="http://blahblah.org/kb/4/Topcon_data-download_howto.pdf">attached file</a>
我认为解决方案类似于:
import re
import base64
with open('phpkb_articles.sql') as f:
for line in f:
re.sub(some_regex_expression_here, some_function_here_to_decode_base64)
有什么想法吗?
编辑:对任何感兴趣的人的回答。
import re
import base64
import sys
def decode_base64(s):
"""
Method to decode base64 into ascii
"""
# fix escaped equal signs in some base64 strings
base64_string = re.sub('%3D', '=', s.group(1))
decodedString = base64.b64decode(base64_string)
# substitute '|' for '/'
decodedString = re.sub('\|', '/', decodedString)
# escape the spaces in file names
decodedString = re.sub(' ', '%20', decodedString)
# print 'assets/' + decodedString + '"' # Print for debug
return 'assets/' + decodedString + '"'
count = 0
pattern = r'getattachment.php\?data=([^&]+?)"'
# Open the file and read line by line
with open('phpkb_articles.sql') as f:
for line in f:
try:
# globally substitute in new file path
edited_line = re.sub(pattern, decode_base64, line)
# output the edited line to standard out
sys.stdout.write(edited_line)
except TypeError:
# output unedited line if decoding fails to prevent corruption
sys.stdout.write(line)
# print line
count += 1
你已经有了,你只需要小块:
模式:r'data=([^&]+?)"'
将匹配 data=
之后和 "
之前的所有内容
>>> pat = r'data=([^&]+?)"'
>>> line = '<a href="http://blahblah.org/kb/getattachment.php?data=NHxUb3Bjb25fZGF0YS1kb3dubG9hZF9ob3d0by5wZGY=">attached file</a>'
>>> decodeString = re.search(pat,line).group(1) #because the b64 string is capture by grouping, we only want group(1)
>>> decodeString
'NHxUb3Bjb25fZGF0YS1kb3dubG9hZF9ob3d0by5wZGY='
然后你可以使用str.replace()
方法和base64.b64decode()
方法来完成剩下的。我不想只为您编写代码,但这应该让您对去哪里有一个很好的了解。
我有一个 SQL 转储文件,其中包含带有 html 链接的文本,例如:
<a href="http://blahblah.org/kb/getattachment.php?data=NHxUb3Bjb25fZGF0YS1kb3dubG9hZF9ob3d0by5wZGY=">attached file</a>
我想查找、解码和替换每个链接中文本的 base64 部分。
我一直在尝试使用 Python w/正则表达式和 base64 来完成这项工作。但是,我的正则表达式技能无法胜任这项任务。
我需要 select 任何以
开头的字符串'getattachement.php?data='
并以
结尾'"'
然后我需要使用 base64.b64decode()
解码 'data=' 和 '"' 之间的部分结果应该类似于:
<a href="http://blahblah.org/kb/4/Topcon_data-download_howto.pdf">attached file</a>
我认为解决方案类似于:
import re
import base64
with open('phpkb_articles.sql') as f:
for line in f:
re.sub(some_regex_expression_here, some_function_here_to_decode_base64)
有什么想法吗?
编辑:对任何感兴趣的人的回答。
import re
import base64
import sys
def decode_base64(s):
"""
Method to decode base64 into ascii
"""
# fix escaped equal signs in some base64 strings
base64_string = re.sub('%3D', '=', s.group(1))
decodedString = base64.b64decode(base64_string)
# substitute '|' for '/'
decodedString = re.sub('\|', '/', decodedString)
# escape the spaces in file names
decodedString = re.sub(' ', '%20', decodedString)
# print 'assets/' + decodedString + '"' # Print for debug
return 'assets/' + decodedString + '"'
count = 0
pattern = r'getattachment.php\?data=([^&]+?)"'
# Open the file and read line by line
with open('phpkb_articles.sql') as f:
for line in f:
try:
# globally substitute in new file path
edited_line = re.sub(pattern, decode_base64, line)
# output the edited line to standard out
sys.stdout.write(edited_line)
except TypeError:
# output unedited line if decoding fails to prevent corruption
sys.stdout.write(line)
# print line
count += 1
你已经有了,你只需要小块:
模式:r'data=([^&]+?)"'
将匹配 data=
之后和 "
>>> pat = r'data=([^&]+?)"'
>>> line = '<a href="http://blahblah.org/kb/getattachment.php?data=NHxUb3Bjb25fZGF0YS1kb3dubG9hZF9ob3d0by5wZGY=">attached file</a>'
>>> decodeString = re.search(pat,line).group(1) #because the b64 string is capture by grouping, we only want group(1)
>>> decodeString
'NHxUb3Bjb25fZGF0YS1kb3dubG9hZF9ob3d0by5wZGY='
然后你可以使用str.replace()
方法和base64.b64decode()
方法来完成剩下的。我不想只为您编写代码,但这应该让您对去哪里有一个很好的了解。