LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-26-2012, 06:32 PM   #1
iacchi
LQ Newbie
 
Registered: Oct 2011
Location: Uppsala, SE
Distribution: Debian testing
Posts: 20

Rep: Reputation: Disabled
Bash script to remove string from all text files, recursively


Hi,
sorry for bothering, I've tried to search on the forum and with Google but I haven't found anything useful; I believe it's just me that can't put 2 and 2 together.
Long story short: someone hacked my hosting space and added this code: http://pastebin.com/wqrLXUG4 in the first line of all text files (i.e. .php and not only), and I'm trying to find a way to remove it with a bash script without having to manually check every single file in my web space.
What I'd like to achieve is to run this script in the root folder of my account and make it delete this code from all text files in all the various folders and subfolders.
Unfortunately, that code is not the only thing in the first line, since it's simply appended before the text of the actual first line of the file, so I can't just remove the first line from every text file, otherwise I'll delete the good code in the first line of the file, too.

I've never really understood the syntax for regexps and I've little to none knowledge of bash scripting, so I really need a help with this, thanks.

Last edited by iacchi; 04-26-2012 at 06:33 PM.
 
Old 04-26-2012, 08:05 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Code:
find .  -type f -exec sed -i '1s#PATTERN##' "{}" \;
It would be better to replace the files from local copies. Also, determine how you are vulnerable. Permissions, remote file inclusion, configuration errors, etc.

A post at the top of the LQ Security forum has a link to a check list you should read, about how to procede investigating the breach, and securing your site.

Last edited by jschiwal; 04-27-2012 at 12:30 AM. Reason: matched single and double quotes.
 
Old 04-27-2012, 12:17 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
If you wish to just delete the line your sed could also be:
Code:
sed -i '1/PATTERN/d'
 
Old 04-27-2012, 12:27 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Quote:
Originally Posted by grail View Post
If you wish to just delete the line your sed could also be:
Code:
sed -i '1/PATTERN/d'
Maybe you meant
sed -i '/pattern/d'
or
sed -i '1d;2q'
But that would delete the entire line when the url was prepended to an existing line.
 
Old 04-27-2012, 01:19 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
oops ... my bad .. I simply thought the 2 might go together, ie. only look at the first line and delete if it matches the pattern. My thinking was, as opposed to your second suggestion
which might remove the first line from an unaffected file, to combine the two and delete ... obviously (now that I tested it) that does not work

Sorry OP ... ignore me ... the missus does when I am daft
 
Old 04-27-2012, 03:28 AM   #6
iacchi
LQ Newbie
 
Registered: Oct 2011
Location: Uppsala, SE
Distribution: Debian testing
Posts: 20

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jschiwal View Post
Code:
find .  -type f -exec sed -i '1s#PATTERN##' "{}" \;
mmm... I'm not sure I've understoond this. I've replaced PATTERN with the code string, but I get an error, something like (it's in Italian, I'm translating):
sed: expression -e #1, char 7318: Previuos regular expression not valid

which part of your code should I actually replace with the code string of the hack?

Quote:
Originally Posted by jschiwal View Post
It would be better to replace the files from local copies. Also, determine how you are vulnerable. Permissions, remote file inclusion, configuration errors, etc.

A post at the top of the LQ Security forum has a link to a check list you should read, about how to procede investigating the breach, and securing your site.
I know, and I'll surely investigate and probably replace the files, but until I find the time, I think this can be a good solution. Delete that code everywhere, and then make a search everywhere with grep of find to see if something have been left behind.

For the others: thanks, but as I said I can't just delete the first line entirely.
 
Old 04-27-2012, 04:09 AM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Could you post sample text in [code] blocks and disable url parsing in the options? The & symbol for instance is a meta-character which needs to be escaped.

Copy one of the files and test your sed command on it before usng it on the server.
Because urls contain slaashes, I used hashes instead. Nearly any pair of characters will work. If the url contains a hash, escape it or use something else..
 
Old 04-27-2012, 04:51 AM   #8
iacchi
LQ Newbie
 
Registered: Oct 2011
Location: Uppsala, SE
Distribution: Debian testing
Posts: 20

Original Poster
Rep: Reputation: Disabled
I'd rather not put the sample on the forum, basically because it's very long and I don't want to mess up the thread. Anyway, you find an example of a hacked file here: http://pastebin.com/uV6JXNVb
There are no hashes or ampersands on the hack code.

Just to be more clear, what I've done is to replace your PATTERN (not the hashes, too, just PATTERN) with everything between the first <?php ?> tags, tags included.
 
Old 04-27-2012, 05:04 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
For the others: thanks, but as I said I can't just delete the first line entirely.
hmmm ... obviously something else I missed

Looking at the example, how about:
Code:
sed -i '1s/<[^>]*>//' file
 
Old 04-27-2012, 05:22 AM   #10
iacchi
LQ Newbie
 
Registered: Oct 2011
Location: Uppsala, SE
Distribution: Debian testing
Posts: 20

Original Poster
Rep: Reputation: Disabled
grail: it looks like it's working, thanks!
 
  


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
bash script- text file manip- delete everything before/after string justin99 Programming 9 11-20-2014 03:16 AM
How to remove string in the text file ? Bash script dlugasx Linux - Server 9 06-05-2009 11:40 AM
help with bash script: remove * to the last . in string drkstr Linux - Software 3 04-25-2006 04:54 PM
remove part of string in bash script crewblunts Programming 2 03-16-2006 05:54 PM
bash-script: output text between two ocurrences of a specific string isl01jbe Programming 1 06-17-2004 02:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:12 PM.

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