5D Library › 零人工多站点容错架构、支付防欺诈状态机与被动现金流排队论 · English

第一章 转账回单:去重、图像哈希与先放行后抽查

问题:一张截图能用几次

许多小型数字业务不接入支付网关,而是让读者转账后上传银行或钱包的截图,由系统或人工核对后放行。最常见的作弊是重复使用:把同一张截图(或略作修改的截图)用于第二笔订单,或者用别人的截图。最省事的防法是记录每张回单流水号的末几位,遇到重复就拒绝。本章先计算这个办法为什么不行,再讨论图像哈希,最后计算「先放行、后抽查」的经济条件。

尾号碰撞:生日问题

若流水号的末 kk 位均匀分布,nn 笔诚实订单中至少有两笔尾号相同的概率是经典的生日问题 (Feller 1968)。更直接的代价是误判:累计接受 MM 笔订单之后,一张诚实的新回单的尾号已被用过的概率是 1−(1−10−k)M1 – (1 – 10^{-k})^M。

推导

# Chapter 1. [p01] Last-k-digit receipt checks and hash look-alikes.
# (1) A manual-payment site accepts a bank-transfer screenshot and
# stores only the last k digits of its reference number to stop reuse.
# If tails are uniform, among n honest orders the chance of at least
# one shared tail is 1 - prod_{i<n} (1 - i / 10^k) (birthday problem).
# After M accepted orders, a new honest receipt is wrongly flagged as
# "reused" with chance 1 - (1 - 10^-k)^M.
# (2) A 64-bit perceptual hash flags two images as the same receipt if
# they differ in at most d bits. For unrelated images, bits differ
# with chance 0.5; for two honest receipts made by the same banking
# app (same layout, different numbers) they differ with chance 0.15;
# a re-saved copy of one receipt differs with chance 0.03 per bit.
import math

def birthday(n, k):
    p = 1.0
    for i in range(n):
        p *= 1 - i / 10 ** k
    return 1 - p

def first_n(k, target=0.5):
    n, p = 1, 1.0
    while 1 - p < target:
        p *= 1 - n / 10 ** k
        n += 1
    return n

def tail(d, q, bits=64):
    return sum(math.comb(bits, i) * q ** i * (1 - q) ** (bits - i)
               for i in range(d + 1))

if __name__ == "__main__":
    print("   (1) P(some shared tail) among n orders")
    print("       k        n=50    n=100    n=500   n=5000   n for 0.5")
    for k in (4, 6, 8):
        row = [birthday(n, k) for n in (50, 100, 500, 5000)]
        print("       {} {:11.4f} {:8.4f} {:8.4f} {:8.4f} {:10d}".format(
            k, *row, first_n(k)))
    print("       honest receipt flagged after M = 5,000 orders:")
    for k in (4, 6, 8):
        print("         k={}: {:.4f}".format(k, 1 - (1 - 10 ** -k) ** 5000))
    print("   (2) P(Hamming distance <= d) for a 64-bit perceptual hash")
    print("        d   unrelated   same app   re-saved copy")
    for d in (4, 6, 10):
        print("       {:2d} {:11.2e} {:10.4f} {:15.4f}".format(
            d, tail(d, 0.5), tail(d, 0.15), tail(d, 0.03)))
    print("\nReading: four-digit tails collide by the time a shop has")
    print("taken 119 orders, and after 5,000 orders four honest")
    print("receipts in ten would be refused. A perceptual hash finds")
    print("re-saved copies but also matches honest receipts from the")
    print("same app; neither replaces the full reference number.")
#    (1) P(some shared tail) among n orders
#        k        n=50    n=100    n=500   n=5000   n for 0.5
#        4      0.1155   0.3914   1.0000   1.0000        119
#        6      0.0012   0.0049   0.1173   1.0000       1178
#        8      0.0000   0.0000   0.0012   0.1175      11775
#        honest receipt flagged after M = 5,000 orders:
#          k=4: 0.3935
#          k=6: 0.0050
#          k=8: 0.0000
#    (2) P(Hamming distance <= d) for a 64-bit perceptual hash
#         d   unrelated   same app   re-saved copy
#         4    3.68e-14     0.0280          0.9569
#         6    4.51e-12     0.1365          0.9970
#        10    9.98e-09     0.6374          1.0000
#
# Reading: four-digit tails collide by the time a shop has
# taken 119 orders, and after 5,000 orders four honest
# receipts in ten would be refused. A perceptual hash finds
# re-saved copies but also matches honest receipts from the
# same app; neither replaces the full reference number.
末 k 位尾号至少一次碰撞的概率随订单数变化。虚线为 0.5。

只核对末 4 位时,50 笔订单中就有 0.1155 的概率出现碰撞,100 笔时 0.3914,到 119 笔时超过一半。更糟的是误判:累计 5,000 笔订单之后,诚实的新回单有 0.3935 的概率被当成「重复」拒绝。末 6 位在 1178 笔时过半,末 8 位在 11775 笔时过半。尾号再长,也只是把问题推后;而且尾号本身很容易被改图伪造。正确的做法是核对完整流水号,并与金额、时间与收款账户一起比对。

图像哈希能做什么

另一类办法是对截图本身计算哈希。密码学哈希(如 SHA-256)对任何一个字节的改变都会给出完全不同的值 (National Institute of Standards and Technology 2015),因此只能发现逐字节相同的文件;截图被重新保存或裁剪一下就认不出来了。感知哈希把图像压成一个 64 位的指纹,相似的图像指纹也相近,用汉明距离阈值 dd 判断「是同一张」。

程序考虑三种情形:两张毫不相关的图像(每一位不同的概率 0.5)、同一个银行 App 生成的两张诚实回单(版式相同、数字不同,每一位不同的概率设为 0.15)、同一张回单重新保存后的副本(每一位不同的概率 0.03)。阈值 d=4d = 4 时,重新保存的副本有 0.9569 的机会被认出,毫不相关的图像几乎不会误配;但同一 App 的两张诚实回单有 0.0280 的机会被误判为同一张。阈值放宽到 10,副本全部认出,可同一 App 的诚实回单误配升到 0.6374。

也就是说,感知哈希能发现「同一张图再用一次」,却分不清「同一个 App 的两张不同回单」,因为后者的大部分像素本来就相同。它只能作为辅助信号,不能替代完整流水号。

先放行、后抽查

即使核对做得好,人工核对银行流水仍然需要时间。「零人工」的设计因此倾向于先放行:上传回单后立即开放内容,事后按比例 aa 抽查,查出伪造即撤销。这是否足以让伪造无利可图?经济学中对犯罪的分析给出的框架是比较预期收益与「被抓概率 × 惩罚」(Becker 1968)。

设商品价值 v=99v = 99,伪造的工夫折合 2;被抓之前已经消耗的比例为 ff(即时交付的测算 f=1f = 1,按周解锁的课程 ff 很小);被抓时还会失去抵押 LL(店内积分、待结佣金、被级联撤销的下线账户)。白嫖者的期望收益为 v(1−a(1−f))−aL−2v(1 – a(1-f)) – aL – 2,当 aa 超过 a*=(v−2)/(v(1−f)+L)a^* = (v-2)/(v(1-f) + L) 时为负。

推导

# Chapter 1. [p02] Optimistic release, sampled audits and revocation.
# Access is released as soon as a receipt is uploaded; a share a of
# orders is audited against the bank statement within a few days. A
# freeloader who uploads a fake receipt gains the product's value v,
# of which a share f is consumed before any audit can revoke access
# (f = 1 for an instant reading, small for a course that drips weekly).
# If caught he also loses a stake L (store credit, pending commissions,
# referred accounts revoked in cascade) and spent effort c. His
# expected gain is
#     EV(a) = v (1 - a (1 - f)) - a L - c,
# negative iff a > a* = (v - c) / (v (1 - f) + L). An audit costs 1.5;
# the cost of deterrence per order is 1.5 a*.

V, C, AUDIT = 99.0, 2.0, 1.5

def a_star(f, L):
    den = V * (1 - f) + L
    return float("inf") if den == 0 else (V - C) / den

if __name__ == "__main__":
    print("   audit share a* that makes faking unprofitable (v = 99):")
    print("      f (consumed early)   L=0       L=50      L=200     L=500")
    for f in (1.0, 0.5, 0.2, 0.05):
        cells = []
        for L in (0, 50, 200, 500):
            a = a_star(f, L)
            cells.append("   never" if a > 1 else "{:8.3f}".format(a))
        print("      {:18.2f} ".format(f) + "  ".join(cells))
    print("   audit cost per order at a*, f = 0.05:")
    for L in (0, 50, 200):
        a = a_star(0.05, L)
        print("      L={:3d}: a* = {:.3f}, cost {:.2f}".format(
            L, a, AUDIT * a))
    print("\nReading: with nothing at stake (L = 0) no audit rate deters")
    print("a freeloader, even when value is released slowly: auditing")
    print("every order still leaves him what he consumed early. A stake")
    print("lost on detection - credit, commissions, referred accounts")
    print("revoked in cascade - is what makes sampled audits work, and")
    print("slow release lowers the audit share it needs.")
#    audit share a* that makes faking unprofitable (v = 99):
#       f (consumed early)   L=0       L=50      L=200     L=500
#                     1.00    never     never     0.485     0.194
#                     0.50    never     0.975     0.389     0.177
#                     0.20    never     0.751     0.347     0.167
#                     0.05    never     0.673     0.330     0.163
#    audit cost per order at a*, f = 0.05:
#       L=  0: a* = 1.031, cost 1.55
#       L= 50: a* = 0.673, cost 1.01
#       L=200: a* = 0.330, cost 0.49
#
# Reading: with nothing at stake (L = 0) no audit rate deters
# a freeloader, even when value is released slowly: auditing
# every order still leaves him what he consumed early. A stake
# lost on detection - credit, commissions, referred accounts
# revoked in cascade - is what makes sampled audits work, and
# slow release lowers the audit share it needs.
让伪造无利可图所需的抽查比例,随被抓时失去的抵押变化。

结论很明确:被抓时一无所失(L=0L = 0)的白嫖者,任何抽查比例都吓不住他,即使内容按周慢慢解锁也一样:全部抽查,他仍然留下了被抓之前已经用掉的部分(f=0.05f = 0.05 时所需的比例是 1.031,超过 1)。真正起作用的是抵押:L=200L = 200 时,即时交付的商品只需抽查 0.485 的订单,慢慢解锁(f=0.05f = 0.05)时只需 0.330,每单的抽查成本 0.49。

这说明「先放行、后抽查」的设计必须同时设计被抓时的损失:把积分、佣金与下线关系挂在同一身份上,查出伪造时一并撤销(级联撤销);而新注册、一无所有的账户,要么先核验再放行,要么只放行低价值的部分。

假说 · 尚未验证

作者的诠释(尚未验证): 在作者的业务中,绝大多数伪造回单来自新注册、没有积分与下线的账户;对这些账户先核验后放行,对有抵押的老账户先放行后抽查,可以在不明显降低转化的前提下让伪造无利可图。什么会证明这一判断错:若真实的抽查记录显示伪造主要来自有积分或下线的老账户,或者对新账户延迟放行使其转化率大幅下降,那么这一分流规则不成立。

小结

末 4 位尾号在 119 笔订单时就有一半的碰撞机会,累计 5,000 笔后会误拒 0.3935 的诚实回单;必须核对完整流水号。感知哈希能认出重新保存的副本,却会把同一 App 的诚实回单当成同一张。先放行、后抽查只有在被抓者会失去抵押时才有威慑力;抵押为 200 时,抽查 0.485 的订单即可。

参考文献

Becker, Gary S. 1968. “Crime and Punishment: An Economic Approach.” Journal of Political Economy 76 (2): 169–217.
Feller, William. 1968. An Introduction to Probability Theory and Its Applications. 3rd ed. Vol. 1. Wiley.
National Institute of Standards and Technology. 2015. Secure Hash Standard (SHS). Federal Information Processing Standards Publication Nos. 180-4. NIST.

Fan, Y. (2026). Zero-Touch Multi-Site Fault Tolerance, Payment Anti-Fraud State Machines and Passive Cashflow Queuing: Receipt-Hash Deduplication, Self-Referral Blocking, Host-Header Routing and Availability Bounds [零人工多站点容错架构、支付防欺诈状态机与被动现金流排队论] (Version 1.0.0). 5D Library. https://doi.org/10.5281/zenodo.24090201 · Licensed under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0).