Accepting projects ·CZ Get in touch →

Hacked website: clean it up or rebuild it?

Deleting a few suspicious files is often not enough. When to rebuild a hacked website from clean sources, and what to do in the first hour.

When you find out someone has hacked your website, the first idea is usually to find the malicious file and delete it. Attackers count on that. Using one case from our practice, we show why cleaning in place is often not enough, when to rebuild the site and what to do in the very first hour.

How you notice a hack

Often you don't. Today's malware tries hard not to be noticed by the owner. A typical example is so-called cloaking. A normal visitor sees the normal website, but the search engine crawler gets completely different content, for example pages advertising someone else's e-shops in another language. You notice nothing. Meanwhile Google indexes spam under your domain.

Be alert when:

  • search results for your site show foreign titles or a foreign language
  • Google Search Console reports thousands of new pages you never created
  • visitor statistics suddenly drop to zero
  • the admin has users you don't know

While cleaning one company website, the malicious code kept coming back. It returned even after we changed the FTP password and disconnected the database, and the attacker had left five hidden administrator accounts. We no longer clean a site like that, we rebuild it from official sources.

Lukáš Hrubý Managing director, HRUBY Software

Why deleting files is not enough

In this case it was not one file. The attacker had created five hidden administrator accounts in the database. For some of them, the registration date was moved years back so they would look old. On the server they left several webshells, scripts for remote control, hidden in directories with names resembling WordPress files.

Above all, there was a so-called reinjector. Code that put the malicious content back into the site's main file every time someone cleaned it. It put it back even after we changed the FTP password and disconnected the database. That means the attacker most likely had access somewhere else too, probably to the hosting control panel.

Whoever cleans in place has to find absolutely everything. Miss a single file and in a few days the site is hacked again. That is why we don't rely on cleaning a hacked website in place.

What rebuilding the website means

It is not about a new design or new content. The files of the system and plugins are downloaded from official sources and uploaded to a clean location. From the hacked site, only what can be checked is taken over: the database content and uploaded images.

In our case, apart from the hidden accounts, the database was clean. We verified it against a test copy of the site that was not reachable from the internet and therefore stayed clean. Without such a copy the check takes more work, but you can still compare the content with what should be on the site.

Cleaning a site in place makes sense only in exceptional cases. For example, when you know exactly how the attacker got in, the infection is fresh and you have something to compare against. If you are not sure, rebuild.

What to do right away

  1. Put the site into maintenance mode. You protect visitors, and Google stops fetching more spam.
  2. Save a copy of the hacked state, including logs. You will need it to find the cause. Download the logs immediately, hosting providers often delete them after a few days. In our case the hosting kept them for seven days, so the traces of the first intrusion were already gone.
  3. Go through the admin users and remove the ones you don't know. Don't trust the registration date.
  4. Change all passwords: admin, FTP, database and hosting control panel. In the panel, check scheduled tasks and FTP accounts, the attacker may have left a back door there.
  5. Generate new security keys. In WordPress these are the keys in the wp-config.php file. This logs out every session, including the attacker's.
  6. Rebuild the files from official sources, and only then bring the site back online.
  7. In Google Search Console, request a new crawl and the removal of the spam URLs from the results.

So it does not happen again

Because the logs had been deleted, we could not prove the way in with full certainty. The most likely explanation was a combination of two mistakes. A backup from earlier changes was sitting in the site's public folder and could be downloaded through a browser. It contained the complete database, including the administrators' password hashes. On top of that, the database server accepted connections from the internet and had a weak password.

  • Don't leave backups in a folder the website serves. They belong on another server, or should be removed right after they are verified.
  • Close the database port to the internet.
  • Delete leftovers of automatic installers and remote management tools you don't use.
  • Set up file change monitoring, so you don't learn about a hack by chance.

When to call an expert

If you find unknown accounts or files, or the malicious code comes back after deletion, it is a job for someone who does this regularly. We establish the extent of the hack as part of a security audit and handle critical findings right away as an incident. We then offer monitoring of updates and file changes as part of website management.

Written by Ing. Lukáš Hrubý, HRUBY Software · ← All articles

Dealing with the same thing?

We'll look at it with you. The first consultation is free. More about the service: Security Audits.

Book a consultation →
// Contact

Get in touch.

Tell us what you need, or just call. We reply within one business day.

// Encrypted in transit · privacy policy