Need help in adding missing tag in php pages


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in adding missing tag in php pages
# 1  
Old 11-21-2015
Question Need help in adding missing tag in php pages

hi,
I am still a newbie on ssh but trying hard.
my friends website was hit by some virus which included a long encrypted malware code on each and every php file she had.

I was able to use sed command via ssh to remove the malware codes but now most pages don't have a opening <?php tag.

i made a copy of pages and tried many options but some or most files are not getting <?php at the start of the *.php page.
sed command i used removed the malware matching string but didn't left any space on start of files.


Can someone please help me with this.

many thanks for your time.
# 2  
Old 11-22-2015
Please, try:
Code:
perl -i -npe '$_="<?php\n$_" if $. == 1 and not /^<\?php/; $.=0 if eof' *.php

It will convert a php file content from this:
Code:
echo readfile("webdictionary.txt");
?>

to this:
Code:
<?php
echo readfile("webdictionary.txt");
?>

This User Gave Thanks to Aia For This Post:
# 3  
Old 11-22-2015
You may want to share your sed script to identify where the <?php lead in was removed.

A sed solution for your problem:
Code:
sed '1 {/<?php/b;s/^/<?php/}' file >TMP  && mv TMP file

This User Gave Thanks to RudiC For This Post:
# 4  
Old 11-22-2015
Thanks Aia & RudiC

RudiC your code says
sed: can't read file: No such file or directory

showing 2 screenshots

the malware code I removed using sed
find . -name "*.php" -exec sed -i '/isset/d' {} \;

Image

but after removal <?php tag got missing
below is start of processed page after sed

Image

earlier it was
Image
# 5  
Old 11-22-2015
Replace "file" with your actual file name (which you did not provide!).
# 6  
Old 11-22-2015
Thanks again

there are 200+ files in each folder :-(

anything which does this in folder tree or global inside /home/user

---------- Post updated at 11:55 PM ---------- Previous update was at 11:50 PM ----------

Quote:
Originally Posted by Aia
Please, try:
Code:
perl -i -npe '$_="<?php\n$_" if $. == 1 and not /^<\?php/; $.=0 if eof' *.php

It will convert a php file content from this:
Code:
echo readfile("webdictionary.txt");
?>

to this:
Code:
<?php
echo readfile("webdictionary.txt");
?>

works like a charm Thanks SmilieSmilie
but haven't checked it thoroughly

As all the backup my friend has is also infected with malware copy, she cannot afford to loose the only working copy online.

I am going to test this for next few hours before i run it on LIVE server.

---------- Post updated at 11:58 PM ---------- Previous update was at 11:55 PM ----------

RudiC: can you please share your expertise and tell why this code didn't worked like it was supposed to be

grep -Lr --include=*.php "<?php" /home/path/ | xargs sed -i "1s/^/<?php \n/"
# 7  
Old 11-22-2015
Quote:
Code:
sed -i '/isset/d'

That would delete any lines, regardless of how long, that contains the string insset in its content, and it would do it in the input file. Since <?php is part of that line it will disappear.
The following would had done what it appears you wanted, leaving a <?php intact.
Code:
sed -i '/isset/c\<?php'

Of course, if there are other lines in the middle of the file with the string isset, it would replace it with <?php which might not be what you want.
Another way would be to find more about the isset block and craft an appropriate regex. Using your example:

Code:
sed -i 's/if(!isset.*$//'


Last edited by Aia; 11-22-2015 at 06:14 PM..
This User Gave Thanks to Aia For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. What is on Your Mind?

PHP Man Pages Now Available (Over 10,000)

Hello! Yesterday we added over 10,000 PHP man pages to our man page section. I've not yet got our unique and special recursive search feature working; but you can still access the PHP man pages directly by selecting PHP in the man sets drop down menu (left side) and then section 3 (right... (3 Replies)
Discussion started by: Neo
3 Replies

2. Shell Programming and Scripting

Help with missing XML tag

Hello All, I am struggling with many huge XML files with lots of Account details including at least one Membership tag, in that Membership tag one xml tag was missed that is MembershipIdentifier: (There are many Account tags with at least one Membership tag are there in each file) ...... ... (4 Replies)
Discussion started by: VasuKukkapalli
4 Replies

3. UNIX for Dummies Questions & Answers

Adding missing lines in file

Dear all, I have a file with two columns - the first column is increasing every 50, the second column is just count (e.g. 5). However, when count is zero, no line is present. Sample: How can I change the file so as to include lines with zero count? e.g. in the previous file to put... (4 Replies)
Discussion started by: TheTransporter
4 Replies

4. UNIX for Dummies Questions & Answers

Adding missing lines in file

Dear all, I have a file with two columns - the first column is increasing every 50, the second column is just count (e.g. 5). However, when count is zero, no line is present. Sample: 1950 7 2000 14 2050 7 2100 13 2150 10 2200 9 2250 7 2300 8 2350 7... (1 Reply)
Discussion started by: TheTransporter
1 Replies

5. Solaris

Missing man pages on SunOS Rel.5.10

Hello, last week I installed SunOS Release 5.10 on my "new" 220R. Unfortunately there seem to be no man pages, although I installed the End-User software package. Yes I know there are lots of similar topics and I hope you will help me nevertheless. Which further information do you need? ... (6 Replies)
Discussion started by: pseudocoder
6 Replies

6. Ubuntu

useradd - shell missing features after adding user

Hi, I need to create a user from a bash script so i have to use useradd. The problem is that when i create a user with: useradd -d /home/sample -m sample after i login with that user I have no history in bash, path do not appears, i can't use arrows and so on. When I use adduser everything is... (5 Replies)
Discussion started by: ktm
5 Replies

7. UNIX for Advanced & Expert Users

Adding a CVS tag to a new file

All, I've been working with perl scripts and shell scripts for quite some time now. I've been making code changes and submitting them into cvs. But I've never created a new file and added it to the directory tree. I know the cvs commands to add it to the directory tree. What I don't know is... (1 Reply)
Discussion started by: rahulrathod
1 Replies
Login or Register to Ask a Question

Featured Tech Videos