python - Bash 字符串变量在使用时会发生不可预知的变化
问题描述
我从 chrome cookie 文件中获取 cookie“encrypted_value”,然后解码,但是字符串变量之一,当我使用它时出现了意外的变化。
如果直接输出:
echo "$SEID"
输出是:
aa312d7a2a15ab67a16f39495dbc22bf9930dafaf70b3eddbd955b0fb39bd7ef661af6ac15d0d98fbbc179c9d6e85901b56c2c4efd9a40df013060d7
当我这样做时:
echo "$SEID;"
但是输出改变了,你应该看到倒数第二个字符从零变成了分号!!!
aa312d7a2a15ab67a16f39495dbc22bf9930dafaf70b3eddbd955b0fb39bd7ef661af6ac15d0d98fbbc179c9d6e85901b56c2c4efd9a40df;13060d7
我的价值来自这个脚本:
SEID=$(get_cookies_from_chrome "xxxx.com" "SEID")
get_cookies_from_chrome
这是:
#!/usr/local/bin/python3
#coding=utf-8
import os
import sys
import sqlite3
import keyring
from Crypto.Cipher import AES
from Crypto.Protocol.KDF import PBKDF2
my_pass = keyring.get_password('Chrome Safe Storage', 'Chrome')
my_pass = my_pass.encode('utf8')
iterations = 1003
cookie_file = os.path.expanduser('~/Library/Application Support/Google/Chrome/Default/Cookies')
salt = b'saltysalt'
length = 16
iv = b' ' * length
class ChromeCookies:
@staticmethod
def aes_decrypt(token):
key = PBKDF2(my_pass, salt, length, iterations)
cipher = AES.new(key, AES.MODE_CBC, IV=iv)
dec_token = cipher.decrypt(token)
return dec_token
@staticmethod
def query_cookies(host_key, name):
with sqlite3.connect(cookie_file) as conn:
sql = 'select encrypted_value from cookies where host_key="%s" and name = "%s"' % (host_key, name)
result = conn.execute(sql).fetchall()
return result
@staticmethod
def get_value(host_key, name):
result = ChromeCookies.query_cookies(host_key, name)
if len(result) != 0:
return ChromeCookies.aes_decrypt(result[0][0][3:]).decode('utf-8')
else:
return None
if __name__ == '__main__':
print(ChromeCookies.get_value(sys.argv[1], sys.argv[2]))
解决方案
您的脚本必须生成带有 CR 字符的文本,请参阅为什么我的工具输出会覆盖自身以及如何修复它?. 例如,如果您的脚本输出123456789\rabcde
并将其保存在 SEID 中,那么echo "$SEID"
将输出看起来像的 abcde6789
内容,然后echo "$SEID;"
将输出看起来像的东西abcde;789
推荐阅读
- javascript - Nightmarejs,点击文本,如果存在
- r - r- 将变量列表输入到矩阵中以进行 t.test
- azure - 具有 Blob 存储的 Azure Functions。如何为子文件夹创建 BlobTrigger?
- logstash - 通过 grok 过滤器解析日志
- r - 使用 dplyr 将输入从 Shiny 传递到 plotly 时,filter_imp 出错
- c++ - VSCode:调试时无法打开图像(C++/SFML)
- ios - 如何在 iOS 11+ 中录制 FLAC 格式
- c++ - 代码块编译错误与运算符 << 不匹配
- c# - Microsoft Bot Framework 通过键入“Reset”来重置 Bot 在 Azure 中不起作用
- python - 带有分组复选框的 Django 表单