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

Blocked domains flood gserver entries #12700

Merged
merged 3 commits into from Jan 21, 2023

Conversation

Quix0r
Copy link

@Quix0r Quix0r commented Jan 20, 2023

It has shown in the past that trolls from the infamous gab.best and also more recently activitypub-troll.cf domains have used a flaw in Friendica to flood the gserver table with millions (see this link) of useless records (tons of invalid and not findable sub domains) of records that just pile up and seem to never end.

With this change, I was finally able to block them from entering my and other's database tables. This PR also contains a change to to avoid redundant invocations of Strings::normaliseLink() which can safe some CPU cycles and speedup the code.

- some trolls managed to flood gserver with useless URLs. They can be blocked
  by domain blocking them, but still it floods gserver table with dead entries
- this hack tries to change that so they won't enter gserver at all. Let's hope
  these trolls as `activitypub-trolls.cf` learn a lesson (and get adults soon)

Signed-off-by: Roland Häder <roland@mxchange.org>
- just for consistency ...
src/Model/GServer.php Outdated Show resolved Hide resolved
Comment on lines +574 to +576
} elseif (Network::isUrlBlocked($url)) {
Logger::info('Server domain is blocked', ['url' => $url]);
return false;
Copy link
Collaborator

Choose a reason for hiding this comment

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

elseif here is superfluous since there's a return in the parent condition.

Suggested change
} elseif (Network::isUrlBlocked($url)) {
Logger::info('Server domain is blocked', ['url' => $url]);
return false;
}
if (Network::isUrlBlocked($url)) {
Logger::info('Server domain is blocked', ['url' => $url]);
return false;

Copy link
Author

Choose a reason for hiding this comment

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

What do you mean by "parent condition"?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The condition where the elseif originated.

Copy link
Author

Choose a reason for hiding this comment

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

I usually only make a 2nd if() block if there is code between the upper and the then added lower one:

if (someCondition) {
// Do something, like logging, closing/freeing resources
return false;
}

// Do other things:
doOtherCode();

if (otherCondition) {
// Do something, like logging, closing/freeing resources
return false;
}

Otherwise, when there is no code in between these two if() blocks, I merge them with elseif() or when they do the exact same thing, I merge them with ||.

Copy link
Collaborator

@MrPetovan MrPetovan Jan 21, 2023

Choose a reason for hiding this comment

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

Let me provide a more actionable suggestion.

Suggested change
} elseif (Network::isUrlBlocked($url)) {
Logger::info('Server domain is blocked', ['url' => $url]);
return false;
if (!Strings::compareLink($url, $original_url)) {
self::setFailureByUrl($original_url);
if (!self::getID($url, true)) {
self::detect($url, $network, $only_nodeinfo);
}
return false;
}
if (Network::isUrlBlocked($url)) {
Logger::info('Server domain is blocked', ['url' => $url]);
return false;
}

These condition have nothing to do with each other because of the return false. If there was anything but a return a elseif would be warranted, but not in this case where it adds complexity for no benefit.

src/Worker/UpdateServerPeers.php Outdated Show resolved Hide resolved
src/Worker/UpdateServerPeers.php Show resolved Hide resolved
@Quix0r
Copy link
Author

Quix0r commented Jan 20, 2023

DELETE FROM `gserver` WHERE `url` LIKE '%activitypub-troll.cf%' OR `url` LIKE '%gab.best%'

Query executed OK, 0 rows affected. (0.149 s)

So far really effective! Without this change, both the later one would have flooded again with even millions(!) of sub domains.

- moved if() block to suggested position by MrPetovan, for me I want to have all
  conditions checked at the start of the method, e.g. no unwanted null
  references or (in this case) if the URL is blacklisted
- normalized URLs are without SSL, means http://host/path/file.ext so they exist
  only once for contacts and servers (aka. instances)
- documented returned type `void`
@MrPetovan MrPetovan merged commit 0681f94 into friendica:develop Jan 21, 2023
@MrPetovan MrPetovan added this to the 2023.03 milestone Jan 21, 2023
Logger::info('Set failed status for existing server', ['url' => $url]);
if (self::isDefunct($gserver)) {
self::archiveContacts($gserver['id']);
}
return;
}
self::insert(['url' => $url, 'nurl' => Strings::normaliseLink($url),

if (Network::isUrlBlocked($url)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This block here is not needed. AFAIK we don't call this function at all for blocked servers. If we should, then we had to check earlier for blocked servers and should call the appropriate function from there.

@Quix0r Quix0r deleted the features/blocklist-gserver branch January 27, 2023 08:26
@tek-aevl
Copy link

tek-aevl commented Apr 4, 2023

This is killing my power bill, I have 1000 of duplicate entry errors, because of this same issue.

@MrPetovan
Copy link
Collaborator

Can you please share a sample of duplicate entry errors?

@tek-aevl
Copy link

tek-aevl commented Apr 13, 2023

Ok, I forgot to respond to this sooner,

Event details

Date Level Context Message
2023-04-12T12:50:56Z ERROR worker DB Error

Data

code
2006
error
MySQL server has gone away
callstack
GServer::insert, GServer::setFailure, GServer::detect, GServer::check, GServer::getID, GServer::add, UpdateServerPeers::execute, Worker::execFunction
params
INSERT INTO `gserver` (`url`, `nurl`, `network`, `created`, `last_failure`, `failed`) VALUES ('https://32tbqwum0.activitypub-troll.cf', 'http://32tbqwum0.activitypub-troll.cf', 'unkn', '2023-04-12 12:50:56', '2023-04-12 12:50:56', 1)
2023-04-09T15:03:00Z ERROR worker DB Error

Data

code
1062
error
Duplicate entry 'http://39cqpqb9m.activitypub-troll.cf' for key 'nurl'
callstack
GServer::insert, GServer::setFailure, GServer::detect, GServer::check, GServer::getID, GServer::add, UpdateServerPeers::execute, Worker::execFunction
params
INSERT INTO `gserver` (`url`, `nurl`, `network`, `created`, `last_failure`, `failed`) VALUES ('https://39cqpqb9m.activitypub-troll.cf', 'http://39cqpqb9m.activitypub-troll.cf', 'unkn', '2023-04-09 15:03:00', '2023-04-09 15:03:00', 1)

@MrPetovan
Copy link
Collaborator

Can you please confirm what version of Friendica you are running and what pattern you set in your server block list related to this attack?

@tek-aevl
Copy link

tek-aevl commented Apr 16, 2023

https://www.urbanmind.net/friendica

Using PiHole blacklisting, with (.|^)activitypub-troll.cf$ and cloudflare dns firewall.

@MrPetovan
Copy link
Collaborator

MrPetovan commented Apr 16, 2023

Ok, the *.activitypub-troll.cf pattern looks good and you seem to be running the 2023.01 version. I also see you have enabled some sort of CloudFlare DDoS protection which prevents JS and CSS files to load.

@MrPetovan
Copy link
Collaborator

So, I have looked into the chain of calls you provided, and it should have been stopped in 4 different places given the error and your blocked server domain pattern:

  • In src/Worker/UpdateServerPeers.php:60 when we check for the existence of a GServer by normalized URL.
  • In src/Model/GServer.php:127 when we try to retrieve a GServer by normalized URL.
  • In src/Model/GServer.php:344 when we try to retrieve a GServer by normalized URL.
  • In src/Model/GServer.php:412 when we try to retrieve a GServer by normalized URL.

I don't have a good explanation for this.

I also note that we've added a number of checks against the server domain pattern block list in the upcoming version 2023.04 so updating to it should reduce your woes.

@tek-aevl
Copy link

tek-aevl commented Apr 17, 2023

The CSS not loading, was not intended, but it helps.

I think I fixed the css not loading issue, it was hidden in my web shield settings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants