Forum Moderators: phranque

Message Too Old, No Replies

Using grep on .php in the /home/ directory

         

csdude55

7:15 am on Dec 16, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Since using Cloudflare, I'm getting emails from my site and I can't figure out the origin!

Quick backstory: several years ago I had a script that was constantly being hammered with injection attempts, using a falsified query string. So I set it up to send me an email with the exact query string each time, and then I gave the user a forbidden response code. Eventually I got it all straightened out and removed the bit of code where it emailed the query string to me.

The emails I'm getting now have that same email subject, but the body is blank!

I looked at the PHP script that originally had that, and confirmed that the email code is definitely not there. So now I'm thinking, maybe I uploaded it to one of my other sites and renamed it, and now it's being pinged by the Cloudflare bot?

The only way I know for sure to find out is to use grep to look for the exact email subject across the entire /home/ directory. But, of course, I need it restricted to PHP scripts or it'll spend a week scanning images!

This is the easy part:

grep "Email Subject" /home/*


I'm taking an educated guess that I'd use this to restrict it to PHP:

grep "Email Subject" /home/*.php


Would that delve in to all subdirectories?

phranque

10:16 am on Dec 16, 2023 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



no

Jonesy

5:06 pm on Dec 16, 2023 (gmt 0)

10+ Year Member Top Contributors Of The Month



man grep ...
-r, --recursive
Read all files under each directory, recursively, .......

csdude55

7:10 pm on Dec 16, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks :-)

Going down that same path, now I'd like to only search certain subdirectories. For my example, let's say that I want to search /home/foo/public_html, /home/bar/public_html, /home/lorem/public_html, and /home/ipsum/public_html.

Thoughts?

# I found an example using {foo,bar} but can't find it in the docs
# I'm also using -l in grep now to just show the filename; I don't think -r is necessary if I use find
find /home/{foo,bar,lorem,ipsum}/public_html/ -type f -name "*.php" -exec grep -l 'Email Subject' {} \;


My entire /home/ directory is about 200G, so I'm trying to limit the search time as much as possible. And not crash the server :-O

I'm also quite worried about running a command that accidentally deletes or damages a file... been there, done that! That's why I'm posting before testing :-)

phranque

6:14 am on Dec 17, 2023 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



BTW the concept you are studying is called globbing which is a form of file name expansion used in *nix systems.

afaik there is no form of or operator in filename globbing expressions so you must list each of the starting points for the find command.
e.g.:
find /home/foo/public_html /home/bar/public_html /home/lorem/public_html /home/ipsum/public_html -type f -name "*.php" -exec grep -l 'Email Subject' {} \;


similarly with grep:
grep 'Email Subject' /home/foo/public_html/*.php /home/bar/public_html/*.php /home/lorem/public_html/*.php /home/ipsum/public_html/*.php


also note that when using rm, especially with the recursive option, it can be useful to use one of the prompting options. (-i, -I, or -interactive)

csdude55

7:17 am on Dec 17, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There was once, many years ago, that a single typo on my end wiped out about 10 years worth of data... with no backup! Ever since, I've been very cautious about using new code that can access everything.

I know in my heart that I'm probably not gonna accidentally type "rm" instead of "grep". But you know the meme...

[imgb.ifunny.co...]