Symptoms: Accounts keep singing up on your instance. The username are all random syllables (kind of sounds like Japanese), they have random full English/American-sounding names, their email address are all from random domains as if their owner have a pretty big budget on that and they all have different IPs."><metaproperty="og:type"content="article"><metaproperty="og:url"content="http://toasters.rocks/how-to-thoroughly-destroy-spam-accounts-from-mastodon/"><metaproperty="article:section"content><metaproperty="article:published_time"content="2018-11-26T12:00:00+00:00"><metaproperty="article:modified_time"content="2019-11-26T06:10:02+00:00"><metaname=twitter:cardcontent="summary"><metaname=twitter:titlecontent="How to thoroughly destroy spam accounts from Mastodon"><metaname=twitter:descriptioncontent="Well,itseemsMastodonhaveaproblemwithaparticularspambot.Asaninstanceadmin,Iinvestigatedandhere’smyfindings.
Symptoms: Accounts keep singing up on your instance. The username are all random syllables (kind of sounds like Japanese), they have random full English/American-sounding names, their email address are all from random domains as if their owner have a pretty big budget on that and they all have different IPs."><metaname=theme-colorcontent="#660066"><title>How to thoroughly destroy spam accounts from Mastodon - toasters rocks</title><linkrel=stylesheethref=http://toasters.rocks/css/toastersrocks.min.css></head><body><header><imgsrc=/img/icon.png><h1>toasters rocks</h1></header><main><aside><nav><ahref=/><iclass="fas fa-home"></i>
<spanstyle=color:#fe3801>SoundCloud</span></a><br></nav></aside><article><div><h2name=top>How to thoroughly destroy spam accounts from Mastodon</h2><p></p><iclass="far fa-calendar-alt"></i>
#<aclass="btn btn-sm btn-outline-dark tag-btn"href=http://toasters.rocks/tags/tech>Tech</a><br><iclass="fas fa-hourglass"></i> ~3 minutes</div><p>Well, it seems Mastodon have a problem with a particular spambot. As an instance admin, I investigated and here’s my findings.</p><p>Symptoms: Accounts keep singing up on your instance. The username are all random syllables (kind of sounds like Japanese), they have random full English/American-sounding names, their email address are all from random domains as if their owner have a pretty big budget on that and they all have different IPs.</p><p>First step: Pass all of those IPs through the Whois database and find something in common. And indeed, they all seem to come from the same ISP, for the most part, so it’s trivial to craft a command to get their IP ranges.</p><p>(Note: The next commands have been tested at the time of writing on a Debian 9 server, on other OSes, your mileage may vary. I say this because the BSD implementation of the whois tool is very different from its GNU counterpart, from what I can see. This also assumes that you’re on the account Mastodon runs and <code>cd</code>’d in its live directory and you know that you shouldn’t type the <code>$</code> as it’s your prompt. And you actually know what you’re doing, make backups first, etc. Also install the <code>ipcalc</code> package first, it’ll help.)</p><pre><code>$ whois -i admin-c,tech-c GS19550-RIPE | grep inetnum | cut -f9- -d"" | sed "s/ //g" | xargs -n1 ipcalc -rn | grep -v deaggregate >> banlist.txt
</code></pre><p>(Actual command will vary from spambot to spambot, but it’s the one the fediverse is currently plagued with and I have no doubt if there’s another spammer it’s going to be similar. Also from here, the instructions are for Mastodon 2.6, for any other software it’s going to be something else, but yeah, you’re basically going to kick IP ranges out.)</p><p>Now that you have a bunch of IPs to ban, enter the Rails console:</p><divclass=highlight><divclass=chroma><tableclass=lntable><tr><tdclass=lntd><pretabindex=0class=chroma><code><spanclass=lnt>1
</span></code></pre></td><tdclass=lntd><pretabindex=0class=chroma><codeclass=language-shdata-lang=sh><spanclass=line><spanclass=cl> $ <spanclass=nv>RAILS_ENV</span><spanclass=o>=</span>production bundle <spanclass=nb>exec</span> rails c
</span></span></code></pre></td></tr></table></div></div><p>Now this bit of code will tell you their usernames, to make sure you have no false positives.</p><divclass=highlight><divclass=chroma><tableclass=lntable><tr><tdclass=lntd><pretabindex=0class=chroma><code><spanclass=lnt>1
</span></span></code></pre></td></tr></table></div></div><p>And now, this is the fun part, destroy them!</p><divclass=highlight><divclass=chroma><tableclass=lntable><tr><tdclass=lntd><pretabindex=0class=chroma><code><spanclass=lnt>1
</span></span></code></pre></td></tr></table></div></div><p>Now you can’t see them anymore. If you missed a few, you can always put their IPs in <code>banlist.txt</code> and repeat the process. Now to make sure they’re not going to come back, you can either put them in your iptables or your favourite firewall, or put a blacklist in your nginx. Run this as your normal admin user:</p><divclass=highlight><divclass=chroma><tableclass=lntable><tr><tdclass=lntd><pretabindex=0class=chroma><code><spanclass=lnt>1
</span></span></code></pre></td></tr></table></div></div><p>Then edit your nginx config and put an <code>include blacklist;</code> somewhere in your instance’s server block. Don’t forget to reload nginx!</p><p>And there you go. How to find out and destroy spambots for dummies. Have fun and don’t forget to do this once in a while :)</p><p>Sources: