Help with Find/Replace Javascript Injected Strings in mulitple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with Find/Replace Javascript Injected Strings in mulitple files
# 1  
Old 05-28-2009
Help with Find/Replace Javascript Injected Strings in mulitple files

Hi, guys, I'm not a high-end programmer, but I've been trying to write a script to remove all of the b.rtbn2.cn (and b.adserv.cn and any future variation) injected script tags on the server. (Still working on security fixes to prevent it in the future, just need to clean up now.)

My approach is 2 parted. First, make a file listing all of the filenames of infected files. Second, feed that file to a find/replace script. I want this to be a script file I can just run in the future with no modifications.

In March I figured out how to make a file listing all of the infected files and it worked fine, but when I ran it again this month after another malicious script injection, it's only finding the infected files in some of the directories instead of all of them. (If I change the directory to search a specific directory in /home/infecteddirectory then it finds it, but if I run it to search /home it doesn't find any in the above directory at all and I don't understand why. (It found some of the files in sub directories, but not in other sub directories.)

First part: Find all infected files in /home. The first one is the original line from March (worked then but not now) and the second on is the one modified to hopefully find all variations in May:

Code:
find /home -exec egrep -q "b.rtbn2.cn|b.adserv.cn" '{}' \; -print 2>/dev/null >infected_files_march2009 &

find /home -exec egrep -q "b\.([0-9]|[A-Z]|[a-z])+\.cn" '{}' \; -print 2>/dev/null >infected_files_may2009 &

Either one seems to be finding /home/infecteddirectory1 but then skipping /home/infecteddirectory2. Not sure why it's not listing all files in all subdirectories that are infected, please let me know if you see any scripting errors in the above code. It's like it's only doing a partial recursive and randomly stopping in the middle and saying it's done.

Second Part: I need help getting the sed/while do part to work. It currently doesn't work, but I can't quite figure out why. This first version is only looking for b.rtbn2.cn and not the any variety of b.something.cn. I wanted to get this working and then try to add the variable part, but I'm lost. I would like it just to leave the <script></script> part and take out the src="http://b.rtbn2.cn" part. (I found out if you leave the script tags in then the hacking program doesn't put them back in if it hits again.

Code:
cat infected_files_may2009|while read line ; do sed -i"s#<script src='http:\/\/b\.rtbn2\.cn\/E\/J\.JS'>##g" $line ;

Any help getting this to work would be greatly appreciated. I did search the boards, but didn't find an example close enough for me to follow.

Thanks!

Last edited by zzlegs; 05-28-2009 at 02:48 PM..
# 2  
Old 05-28-2009
Quote:
Originally Posted by zzlegs
Code:
cat infected_files_may2009|while read line ; do sed -i"s#<script src='http:\/\/b\.rtbn2\.cn\/E\/J\.JS'>##g" $line ;

the cat and while loop are useless. just use sed.
Code:
sed -i "s#.....................#" infected_files

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed find 2 strings and replace one

Hi Everyone, I want to find this 2 strings in a single line a file and replace the second string. this is the line i need to find <param name="user" value="CORE_BI"/> find user and CORE_BI and replace only CORE_BI with admin so finally the line should look like this. <param... (5 Replies)
Discussion started by: shajay12
5 Replies

2. Shell Programming and Scripting

sed Find and Replace Text Between Two Strings or Words

I am looking for a sed in which I can recognize all of the text in between two indicators and then replace it with a place holder. For instance, the 1st indicator is a list of words "no|noone|havent" and the 2nd indicator is a list of punctuation ".|,|!".From a sentence such as "noone... (3 Replies)
Discussion started by: owwow14
3 Replies

3. Shell Programming and Scripting

Find multiple strings and replace single string

Hi, following Perl code i used for finding multiple strings and replace with single string. code: #!/usr/bin/perl my @files = <*.txt>; foreach $fileName (@files) { print "$fileName\n"; my $searchStr = ',rdata\)' | ',,rdata\)' | ', ,rdata\)'; my $replaceStr =... (2 Replies)
Discussion started by: chettyravi
2 Replies

4. UNIX for Dummies Questions & Answers

Find and replace mulitple charaters in filenames

I have a virtual pdf printer set up on my server which produces files with the following prefix: smbprn_00000044_Microsoft_Word_-_OriginalFilename.pdfthe number in the center of the file increase by one for each new file. I want to remove all the charaters infront of OriginalFilename.pdf using... (14 Replies)
Discussion started by: barrydocks
14 Replies

5. Shell Programming and Scripting

substitution to mulitple files

Hello all, I am trying to make a script that will apply a substitution to any number of files given on the command line. Example would be ~/Unix/script/subst car boat myFile1.txt myFile2.txt myFile3.txt This is the code I have so far but it does not function as needed. PAT=$1 shift... (10 Replies)
Discussion started by: ramn214
10 Replies

6. UNIX for Advanced & Expert Users

Find and replace txt between two strings in flat file

Hi There... I need to serach and replace strngs in a text file. My file has; books.amazon='Let me read' news.bestseller='xyz' expected output is books.amazon=NONFOUND news.bestseller=NONFOUND Can I first find the text between string1= books.amazon=' and string2= ' (locate the text... (1 Reply)
Discussion started by: Hiano
1 Replies

7. UNIX for Dummies Questions & Answers

how to find and replace strings in multiple files

Hi All, Iam new to unix, I need to find string and replace it in the file name. Like text_123_0.txt,text_123_1.txt,text_123_2.txt. I need to search 123 and replace it with 234 . Is there any unix command to replace them in single command since i have 5 directories. So i need to go each and every... (0 Replies)
Discussion started by: etldeveloper
0 Replies

8. Shell Programming and Scripting

AWK find/replace 2 strings in one shot

Friends, I have a file with contents like: interface Serial0/4/0/0/1/1/1/1:0 encapsulation mfr multilink group 101 interface Serial0/4/0/0/1/1/1/2:0 encapsulation ppp multilink group 101 I just have to repace mfr with ppp and ppp with mfr in a single shot. I tried using... (4 Replies)
Discussion started by: shrijith1
4 Replies

9. Shell Programming and Scripting

Need help on Mulitple files mutliple actions

Hi all, I have mistkanely gzipped twice an entire folder and sub folders, and also renamed the files during that process. I am trying to undo this, and I need help to create the batch to work on it. All folders are under my images directory, I have a output.txt file that holds all the... (1 Reply)
Discussion started by: saariko
1 Replies

10. Shell Programming and Scripting

Find Strings in Files

experts, i am using solaris 9. there are serveral *.log files in a directory "/var/alllogs/". Among the files one or several files contain the string "0198634873". I want do- cat *.log | grep "0198634873"; And want to see which Files(file_name) contain that string. Plus output will go to a... (3 Replies)
Discussion started by: thepurple
3 Replies
Login or Register to Ask a Question