demo_1.py 1.2 KB

123456789101112131415161718192021222324252627282930
  1. def build_filter(tenant_id: str, tender_document_id=None):
  2. safe_tenant = tenant_id.replace('"', '\\"')
  3. print(f'safe_tenant: {safe_tenant}')
  4. expr = f'tenant_id == "{safe_tenant}"'
  5. if tender_document_id:
  6. safe_tender_document = tender_document_id.replace('"', '\\"')
  7. print(f'safe_tender_document: {safe_tender_document}')
  8. expr += f' and tender_document_id == "{safe_tender_document}"'
  9. return expr
  10. # 例子 1:正常值(看不出变化)
  11. print("【例1】正常值")
  12. print("输入 tenant=abc, tender_document=123")
  13. print("输出:", build_filter("abc", "123"))
  14. print("-" * 40)
  15. #
  16. # 例子 2:值里面带了双引号(重点看转义效果)
  17. print("【例2】tenant 里带双引号")
  18. print('输入 tenant=te"nt, tender_document=co"urse')
  19. print("输出:", build_filter('te"nt', 'co"urse'))
  20. print("-" * 40)
  21. # 例子 3:恶意注入尝试(双引号被转义成了字符,失去注入能力)
  22. '''
  23. 如果没有转义代码(没有 replace),拼出来的字符串会变成:
  24. tenant_id == "123" and tender_document_id == "" or "1"="1"
  25. '''
  26. print("【例3】模拟注入攻击")
  27. print('输入 tenant=tenant, tender_document=" or "1"="1')
  28. print("输出:", build_filter("tenant", '" or "1"="1'))