LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 07-14-2021, 11:24 PM   #1
buckbuchanan
LQ Newbie
 
Registered: Jul 2021
Posts: 6

Rep: Reputation: Disabled
Noob question about being hacked


Hi all

I am managing a small website for a friend and I got hacked. I wonder if anyone can help me identify how and prevent it from happening again?

What happened - someone modified functions.php. They added some code to the file, then renamed it to functions.php123.

How can I see how they managed to get in to that file? What do I need to do to prevent them from getting in again?

Thanks
 
Old 07-14-2021, 11:46 PM   #2
lovemeslk
Member
 
Registered: Feb 2020
Location: Rantoul IL
Distribution: Slackware
Posts: 350

Rep: Reputation: 72
When in networking certain files you can set to no modify even root.
I hope some one in here with networking picks this thread up.
 
Old 07-15-2021, 01:24 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
in general you need to secure your host and web server, but that is not that simple. So there is no "single-command" solution, but you need to check all your services, configurations, permissions, network settings (and a few other things).
Additionally you may need to check your log files to find out what's happened.

All of these settings depend on your hardware and software and hard to give you any help without knowing anything about that.
 
Old 07-15-2021, 08:12 AM   #4
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,597

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546

Log files might tell you what happened, (though depending on the attacker's level of access/competence they could be wiped/forged).

Most likely it's because you're running insecure code that trusted user input and allowed someone to inject their own code.
(There's a number of ways to do that, and it's hard to say more without seeing the code.)

Another possibility is there's a weak password in use somewhere which they managed to determine.
If so, change all relevant passwords, and consider whether any other accounts linked to same/similar emails need addressing.

There's other possibilities, but really there's no point saying more: in all situations step 1 is talk to your hosting provider.


Last edited by boughtonp; 07-15-2021 at 08:14 AM.
 
Old 07-15-2021, 08:43 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938
On a shared-hosting environment, sometimes the host does not properly set the security rules for each user's account ... it is actually possible for another user to cd into your home directory, examine its content, and perhaps modify it.

Your web site should be running under a user/group, such as nobody, that does not have write-access to any directories that contain code or resources. If the program needs to write somewhere, only that location should be read/write to it, and it should not be possible for a user to get a directory listing.

Unfortunately, most hosts do not use OpenVPN nor provide VPN-first access to their resources – they simply expose ssh and they usually allow passwords (not certificates). Therefore, your passwords must be pure-random and changed frequently.
 
Old 07-15-2021, 10:22 AM   #6
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Basic reasons are:
  • wrong permissions or ownership on files (if I type the URL of a script or key file, will it come up in my browser?)
  • incorrect configuration
  • using unsanitized user input (just ask Bobby Tables)
  • unpatched vulnerabilities in software
  • leaving services running that you don't need (ftp, print)
  • using insecure add-ons (looking at you, Wordpress)
  • default or simple or no passwords (you wouldn't do that, would you?)

Some features that may or may not apply: Only allow login/admin on a separate management IP with strict firewall rules. Do remote logging so if (when) you get hacked, they can't wipe the logs. Test on a separate machine; wipe and reinstall everything on update.
 
2 members found this post helpful.
Old 07-15-2021, 12:03 PM   #7
uteck
Senior Member
 
Registered: Oct 2003
Location: Elgin,IL,USA
Distribution: Ubuntu based stuff for the most part
Posts: 1,173

Rep: Reputation: 501Reputation: 501Reputation: 501Reputation: 501Reputation: 501Reputation: 501
If you are using Wordpress check the extensions you loaded to see if they have a reported security hole. That is the most common way people get access to sites running Wordpress.
 
Old 07-15-2021, 04:09 PM   #8
buckbuchanan
LQ Newbie
 
Registered: Jul 2021
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks everyone for the replies. Definitely some things to look at there.

One question on all this... When the server was made and Wordpress installed, I noticed that all the wordpress files were owned by a certain user (let's call it UserX). Most of the files have rw-r--r-- rights. Most folders have rwxr-xr-x. Does that look right?

I don't understand how I can tighten that down any more without accidentally locking myself out of being able to access the files.

Thanks
 
Old 07-15-2021, 04:38 PM   #9
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,597

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546
Quote:
Originally Posted by buckbuchanan View Post
One question on all this... When the server was made and Wordpress installed, I noticed that all the wordpress files were owned by a certain user (let's call it UserX). Most of the files have rw-r--r-- rights. Most folders have rwxr-xr-x. Does that look right?

I don't understand how I can tighten that down any more without accidentally locking myself out of being able to access the files.
From memory, that sounds about right - I think there's documentation somewhere on the Wordpress site that explains the different folder permissions - but permissions protect your files from other users on the server, they're less relevant if the attacker gets in through a vulnerability in Wordpress (or more likely, one of the plugins), and thus can execute their own PHP code.

You can try putting the names of the plugins you're using into a vulnerability database search - if something comes up you need to check the details and fix it (might be as simple as upgrading to latest version), through if nothing comes up that doesn't guarantee security.

 
Old 07-15-2021, 05:58 PM   #10
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,976

Rep: Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623
Is it possible that a theme or other proper use caused this?
 
Old 07-15-2021, 06:21 PM   #11
buckbuchanan
LQ Newbie
 
Registered: Jul 2021
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks for the replies - all good stuff.

How could I tell if it is a theme?

It happened again last night so I still haven't fixed the problem. They managed to alter functions.php again.

I ran the 'last' command but all I saw was my own logins.

Any thoughts as to how I can actually see the hackers activity? If I could see that I might be able to understand where they are getting in.

Interesting thing is the time stamp on the file was 02 July - so it's not even showing that it was edited last night.

Thanks
 
Old 07-15-2021, 07:59 PM   #12
uteck
Senior Member
 
Registered: Oct 2003
Location: Elgin,IL,USA
Distribution: Ubuntu based stuff for the most part
Posts: 1,173

Rep: Reputation: 501Reputation: 501Reputation: 501Reputation: 501Reputation: 501Reputation: 501
Try running a clamav scan on the system to find any command shells they are uploading giving them remote access.
The location of anything you find might give a clue as to how they are getting in.
 
Old 07-15-2021, 08:13 PM   #13
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,976

Rep: Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623
No telling how a hacker got in, if they really did.
I'd consider recording network packets. Then search again if fails. Step up the firewall to include white list, black list, geo limits.
There have been some holes in wordpress a long while ago.

Do you notice the changes? What are the changes other than file?
 
Old 07-15-2021, 08:30 PM   #14
buckbuchanan
LQ Newbie
 
Registered: Jul 2021
Posts: 6

Original Poster
Rep: Reputation: Disabled
I didn't notice any other changes. The user had inserted code to create a user in the wordpress db. It looks like they weren't successful and as far as I can see they didn't get any further than that.

But it is interesting they tried the exact same thing 2 nights in a row.
 
Old 07-15-2021, 08:51 PM   #15
v00d00101
Member
 
Registered: Jun 2003
Location: UK
Distribution: Devuan Beowulf
Posts: 514
Blog Entries: 1

Rep: Reputation: 37
Personally i would backup everything to somewhere else. Wipe the server. Restore the site from backup. Then harden the crap out of it. Although if you are using Wordpress thats almost like trying to bailout a sinking ship with a thimble.

As an admin you can do a certain amount to secure the backend, but the website side is a different matter. Unless you are an experienced programmer you will not be able to shore up the wordpress problem. You can probably mitigate it up to a point by keeping on top of patches and updates, but their is always a possibility that the attacker is using a zero day that no one has found yet.

One thing you could try is restore the functions.php from backup then make it immutable (chattr +i). Presumably that file has no need to be changed very often. In fact any file that doesnt need to be changed you could do the same with. Its very sledgehammer to nut level, but it should work if you are running the webserver as a non-privileged user (preferably with no login). If it then gets changed again, well you have bigger problems because the attacker has found and is probably using a privilege escalation exploit, since only root can remove the immutable attribute. If that happens, look at your root password, get rid of sudo if its installed, in fact get rid of sudo regardless or lock it down so no one can use it.

ClamAV could be a port of call, definitely rkhunter/chkrootkit as well. If it finds a rootkit, then you definitely need to scrub the server and start over.

Good luck. I've been there in the past. I didnt faff around. I just wiped it, restored it and moved on. You can spend weeks trying to work out how they got in, but without a bunch of experience and maybe some pentesting knowledge you will struggle. If everything is up to date and it happened regardless, its either a password issue or maybe a zero day. You could also maybe email the logs to yourself every 15 mins for a couple of days to try and work out how they are getting in.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] My network is hacked for sure. I want to reinstall but it will be hacked again. MsRefusenik Linux - Security 19 10-18-2010 05:02 PM
noob with a noob question phoonerorlater Linux - Newbie 2 09-29-2004 03:43 PM
Complete noob question from a noob noob_hampster Linux - Software 2 09-04-2003 12:03 AM
I think I'm being hacked. How to proceed? Noerr Linux - Security 25 05-31-2002 03:48 AM
help! ssh password being denied for ALL acccounts (hacked?) JustinHoMi Linux - Security 4 05-26-2002 05:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 08:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration