Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contrib: Ask for captcha in signet getcoins.py #23162

Merged
merged 1 commit into from
Oct 5, 2021

Conversation

laanwj
Copy link
Member

@laanwj laanwj commented Oct 2, 2021

As the faucet will always ask for a captcha now, the current script is no longer usable.

Change the getcoins.py script to print the captcha in dot-matrix to the terminal, using unicode Braille characters.

$ ./getcoins.py  -a tb1q9srj96uj4uk5gva9k7h2zxuy6ttu7ltsagu0gl
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠙⠢⢄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠑⠢⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠴⠊⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⢦⣄⣀⠀⠀⠀⠉⠒⠤⣀⠀⠀⢀⣀⠄⠀⠀⠀⠀⠀⢀⣀⣀⣤⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠤⠊⠁⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠹⣍⠹⣦⡀⠀⠀⠀⢀⡿⢻⣿⣿⠋⠀⠀⠀⠀⢸⠋⢩⣤⣴⣿⠃⠀⠀⠀⠀⠀⠀⠈⡟⠓⠒⠒⠖⠒⢛⣿⡖⠀⠀⠀⠀⠀⡶⠒⠛⠛⠒⠒⠶⣄⠴⠊⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢳⡈⢻⣄⠀⣰⠏⣴⣿⠟⠀⠉⠓⠢⢄⣀⣸⡀⢸⣿⠋⠉⠀⠀⠀⠀⠀⠀⠀⠀⣧⠀⣿⣿⣿⠿⠿⠟⠃⠀⠀⠀⠀⠀⣷⠀⣾⣿⡿⣷⠊⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⣆⠹⣾⢃⣾⣿⠋⠀⠀⠀⠀⠀⠀⢸⣟⡓⣾⣟⣯⡄⠀⠀⠀⠀⠀⠀⠀⠀⡿⠐⣛⡛⠛⠲⣄⠀⠀⠀⠀⠀⠀⢀⡸⣷⡻⢯⡤⢟⣼⣿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣧⢠⣾⡿⠁⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇⣿⣿⠛⠏⠓⠒⠤⠤⣀⣀⣀⠀⠛⣿⡿⠿⠿⣆⣹⣧⡠⠤⠔⠒⠉⠁⣰⠟⣥⣶⣶⣌⠹⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡿⢸⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⣉⠉⠉⠉⢉⡏⢸⣿⡀⠀⠀⠀⠀⠀⡟⢸⣿⠏⠉⢹⡆⣻⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠈⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⠛⠳⠶⠶⠛⣁⣼⣿⡇⠀⠀⠀⠀⠀⣧⠈⠻⠶⠶⠛⣀⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠿⠿⢷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠛⠿⠿⠿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⠻⠿⣿⣿⣿⣿⡿⠿⠀⠀⠀⠀⠀⠀⠈⠻⠿⣿⣿⣿⣿⣿⠿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
Enter captcha: Yf58
Payment of 0.00100000 BTC sent with txid e0a96a2a768d1ae0d22eaa2942476927d956445276bdf508dfd2f0e2e836f300

The only added dependency is that convert from ImageMagick (or compatible fork) is installed.

Captchas are disabled by default for custom faucets, but it is possible to specify a custom captcha URL on the command line.

Also adds a way to change the amount requested with e.g. -n 0.1.

Edit: credit where credit is due, yope gave me the idea of using Braille characters as a widely available way to do graphics in the terminal. Another possibility is 2x2 block characters but it is a lot less compact. Yet another one is VT340 Sixel graphics but it's not supported in many terminal emulators.

@laanwj laanwj requested a review from kallewoof October 2, 2021 03:21
@laanwj laanwj force-pushed the 2021-10-getcoins branch 2 times, most recently from 355ece0 to bd15038 Compare October 2, 2021 03:23
As the faucet will always ask for a captcha now, the current script is
no longer usable.

Change the script to print the captcha in dot-matrix to the terminal,
using unicode Braille characters.
@kallewoof
Copy link
Member

kallewoof commented Oct 2, 2021

This is incredible! Alternative would be to do the heavy lifting (unicode-ifying) server-side, but that might be too DoS-risky. Testing as soon as ImageMagick finishes installing.

@kallewoof
Copy link
Member

kallewoof commented Oct 2, 2021

Tested ACK a6d9675

@laanwj
Copy link
Member Author

laanwj commented Oct 2, 2021

This is incredible! Alternative would be to do the heavy lifting (unicode-ifying) server-side, but that might be too DoS-risky
Testing as soon as ImageMagick finishes installing.

That's a good point! Even having to render the image as a bitmap on the server side might be more DoS-risky.

FWIW, I first used Pillow on the Python side, but it doesn't currently support SVG. so I worked around it by using ImageMagick to convert to a trivial image format (PPM) and loading that manually. If the image format was changed to something else we could use Pillow, instead. But I'm not really sure it's a more convenient dependency than ImageMagick.

Copy link
Contributor

@lsilva01 lsilva01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome solution for this problem.

Tested ACK a6d9675 on Ubuntu 20.04 with the command below:
./contrib/signet/getcoins.py -i /usr/bin/convert -c /path/src/bitcoin-cli -- -signet -datadir=/path/bitcoin -rpcwallet="w1"

@maflcko
Copy link
Member

maflcko commented Oct 2, 2021

Might be nice to use python tensorflow to pre-solve the captcha for the user

@dunxen
Copy link
Contributor

dunxen commented Oct 2, 2021

Tested ACK a6d9675

Screenshot from 2021-10-02 12-51-19

Copy link
Contributor

@shaavan shaavan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK a6d9675
Tested successfully on Ubuntu 20.04

Screenshot:
PR

This is quite a significant addition for the bitcoin-cli. Also, the addition of now to able choose the requested amount is cherry on the cake. I do have to mention, though; the captcha looks so beautiful on the command line!

@theStack
Copy link
Contributor

theStack commented Oct 2, 2021

Very neat, Concept ACK!

Copy link
Contributor

@stratospher stratospher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested ACK a6d9675 on macOS 10.15.7. This PR:

  • Fetches the captcha in res and uses imagemagick to convert it into pixel representation(stored as bytes object).
  • It’s then stored as RGB tuples in self._grid and uses print_image() to print the braille unicode characters.

To prevent spamming of faucet requests, faucet fund requests check captcha and with this script, the captcha gets printed in the terminal so that the script can work again. It’s a great solution!

Screenshot 2021-10-02 at 4 35 31 PM

@practicalswift
Copy link
Contributor

Concept ACK

I love this idea and what a nice compact implementation. Impressive in its simplicity!

@yanmaani
Copy link

yanmaani commented Oct 3, 2021

utACK.

Theoretically, it is possible to do it in non-unicode terminals using normal ASCII. It doesn't look as pretty though. There's some very advanced algorithms for this, but a dumb pixel_value = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'."[n] ought to do for simple purposes like this.

You may also want to get ImageMagick to do the binarization. It has a bit more sophisticated facilities for that, if the captcha ever gets harder.

@luke-jr
Copy link
Member

luke-jr commented Oct 3, 2021

Viewing the opening comment in Chromium makes it look very unreadable, but the screenshots others have posted look fine.

Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK a6d9675 ♟️

Tested with OpenBSD 6.9, worked like a charm after installing the ImageMagick package. The converted captcha output looks good and is well-readable. Also tried out all of the three newly introduced parameters --imagemagick, --amount and --captcha (though for the last I only tested the failure case due to a lack of alternative signet faucets).

@laanwj
Copy link
Member Author

laanwj commented Oct 5, 2021

Theoretically, it is possible to do it in non-unicode terminals using normal ASCII. It doesn't look as pretty though. There's some very advanced algorithms for this, but a dumb pixel_value = "$@b%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/|()1{}[]?-_+~<>i!lI;:,"^`'."[n] ought to do for simple purposes like this.

Feel free to add another mode, if you can get that readable enough. Preferably without adding a dependency on libaa or such, or a lot of code, at least this is straightforward and looking at the comments here, unicode Braille support is common enough.

You may also want to get ImageMagick to do the binarization. It has a bit more sophisticated facilities for that, if the captcha ever gets harder.

That's an interesting suggestion. It can also do dithering instead of a naive threshold, which might or might not help. I didn't initially want to rely on ImageMagick too much, in case the image format changes to something that Pillow can natively handle, but as it's not using that anymore, it doesn't seem bad to commit further to that direction.

Edit: I tried adding -monochrome but this doesn't seem to make it better, if anything it gets slightly more noisy (see especially the "e"). I think I prefer to keep it like this:
captcha-faucet-comparison

@ghost
Copy link

ghost commented Oct 5, 2021

Concept ACK

I had suggested captcha in #22565 (comment)

Although now that I was thinking about possible ways to test this, there is one issue which can be managed by adding a warning that "captcha" is based on third party URL.

If the website gets hacked, attacker can show something else instead of regular captcha.

@laanwj
Copy link
Member Author

laanwj commented Oct 5, 2021

Although now that I was thinking about possible ways to test this, there is one issue which can be managed by adding a warning that "captcha" is based on third party URL.

As far as I know, the captcha is generated on the same host as the faucet itself. The faucet could already return any kind of information in an error that is printed directly to the console (including control characters, any unicode and escape sequences). The captcha isn't worse in that sense. If you want to harden it you could do various things:

  • Sanitize any strings coming from the faucet.
  • Require the captcha to be in a specific image format (this is currently left open), by making the input -:svg. This is a compromise as it would limit flexibility to change image formats in the future.
  • Limit the size/complexity of the SVG, limit dimensions—don't know if ImageMagick has introspection into this before actually rendering the thing, you might need a sifferent SVG parser.

You're welcome to work on this but I see it as outside the scope of this pull request.

@laanwj laanwj merged commit 113b863 into bitcoin:master Oct 5, 2021
@ghost ghost mentioned this pull request Dec 11, 2021
@ghost ghost mentioned this pull request Jan 20, 2022
@bitcoin bitcoin locked and limited conversation to collaborators Oct 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet