在我的Python程序中,我已經使用了Pickle模塊來保存用戶定義,然後將它們加載回來,以備運行程序。現在根據我從Python Wiki網站上的UsingPickle文章瞭解到,Pickle文件可能會被黑客入侵等,使其不安全。Python:讓泡菜文件更安全嗎?
我注意到,泡椒文件往往只是留在了Python腳本所在的目錄。有沒有一種方法,使這些文件更安全和隱蔽的的視線?如果是這樣,當在安裝腳本中包含Pickle文件時,這會如何影響我在腳本上使用cx_Freeze?
import pickle
terms = pickle.load(open("save.p", "rb"))
def print_menu():
print('Computing Terms')
print()
print('0. Quit')
print('1. Look Up a Term')
print('2. Add a Term')
print('3. Redefine a Term')
print('4. Delete a Term')
print('5. Display All Terms')
while True:
print_menu()
print()
choice = input('Choice: ')
if choice == '0':
break
elif choice == '1':
print('\n')
term = input('Type in a term you wish to see: ')
if term in terms:
definition = terms[term]
print('\n')
print(term, '-', definition, '\n')
print()
print('----------------------------------------------------------------')
print()
print()
else:
print('This term does not exist. Try adding it instead.\n')
print()
print('----------------------------------------------------------------')
print()
print()
elif choice == '2':
print('\n')
term = input('What term would you like to add?: ')
if term not in terms:
print('\n')
definition = input('What\'s the definition?: ')
terms[term] = definition
pickle.dump(terms, open("save.p", "wb"))
print('\n')
print(term, 'has been added.\n')
print()
print('----------------------------------------------------------------')
print()
print()
else:
print('\n')
print('Term already exists, try redefining it instead.\n')
print()
print('----------------------------------------------------------------')
print()
print()
elif choice == '3':
print('\n')
term = input('Which term do you want to redefine?: ')
if term in terms:
definition = input('What\'s the new definition?: ')
terms[term] = definition
pickle.dump(terms, open("save.p", "wb"))
print('\n')
print(term, 'has been redefined.\n')
print()
print('----------------------------------------------------------------')
print()
print()
else:
print('\n')
print('That term doesn\'t exist, try adding it instead.')
print()
print('----------------------------------------------------------------')
print()
print()
elif choice == '4':
print('\n')
term = input('Which term would you like to delete?: ')
if term in terms:
del terms[term]
pickle.dump(terms, open("save.p", "wb"))
print('\n')
print('The term has been deleted.\n')
print()
print('----------------------------------------------------------------')
print()
print()
else:
print('\n')
print('This term doesn\'t exist.')
print()
print('----------------------------------------------------------------')
print()
print()
elif choice == '5':
print('\n')
print('The terms available are: ')
print()
for term in sorted(terms):
print(term)
print()
print()
print('----------------------------------------------------------------')
print()
print()
else:
print('\n')
print('Sorry, but ', choice, ' is not a valid choice.\n')
print()
print('----------------------------------------------------------------')
print()
print()
JSON本質上更安全嗎? [jsonpickle的文檔](http://jsonpickle.github.io/)說,「**警告:**從不可信源加載JSON字符串表示潛在的安全漏洞.jsonpickle不會嘗試清理輸入。 「 JSON和其他的安全性或缺乏安全性是否會影響它們的使用? – kuzzooroo
@kuzzooroo JSON是一種數據格式。這只是一種組織和存儲數據的方式。數據格式本身與應用程序如何處理和(ab)使用數據格式無關。從我可以看到的jsonpickle嘗試向JSON添加pickle功能,這意味着當decode()事物時,您可以'eval()'原始的,不可信的代碼;這就是爲什麼他們添加了安全說明。 – tyteen4a03