• windows api和创建服务


    什么是API?

    API(应用程序接口)是一组预定义的Windows函数,用于控制每个Windows元素的外观和行为(从桌面窗口的外观到新进程的内存分配)。每个用户操作都会导致执行多个或多个API函数,从而告诉Windows发生了什么。

    它类似于Windows的本机代码。其他语言仅充当外壳程序,以提供一种自动化且更轻松的方法来访问API。在.NET中,我们可以使用平台Interop Services调用Win 32 API,该平台位于System.Runtime.InteropServices命名空间。

    Windows API位于Windows系统目录中的DLL中,例如User32.dll,GDI32.dll,Shell32.dll等。这些基本的win32 API分为三种不同的类型,具体取决于它们中驻留的代码。
    
    分离如下
    
    User32.dll-处理用户界面内容 
    Kernel32.dll-文件操作,内存管理 
    Gdi32.dll-涉及图形化Whatnots 

    现在,我将介绍如何在您的.NET应用程序中使用这些Win32 API来享受Win32 API的奢华。我要介绍的这些用法是我们将要进行的一些步骤,并讨论一些实时示例,这些示例将向您展示Win32 API的美丽。

    让我们从API声明开始,正如我之前提到的,.NET具有可与外部DLL一起使用的Interop Services。因此,通过在应用程序中使用Syetem.Runtime.InteropServices命名空间,您可以获得一些功能,通过这些功能可以调用外部应用程序。

    写入服务名和描述

    这里给出Myservice的代码(代码对应服务启动暂停终止时候执的操作)

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.ServiceProcess;
    using System.Text;
    using System.Threading.Tasks;
    using System.Timers;
    
    namespace MyFirstService
    {
        public partial class Service1 : ServiceBase
        {
    
            Timer timer = new Timer(); // name space(using System.Timers;)
            public Service1()
            {
                InitializeComponent();
            }
    
            protected override void OnStart(string[] args)
            {
                WriteToFile("Service is started at " + DateTime.Now);
                timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
                timer.Interval = 5000; //number in milisecinds
                timer.Enabled = true;
            }
    
            protected override void OnStop()
            {
                WriteToFile("Service is stopped at " + DateTime.Now);
            }
    
            private void OnElapsedTime(object source, ElapsedEventArgs e)
            {
                WriteToFile("Service is recall at " + DateTime.Now);
            }
    
            public void WriteToFile(string Message)
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "\Logs";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                string filepath = AppDomain.CurrentDomain.BaseDirectory + "\Logs\ServiceLog_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt";
                if (!File.Exists(filepath))
                {
                    // Create a file to write to. 
                    using (StreamWriter sw = File.CreateText(filepath))
                    {
                        sw.WriteLine(Message);
                    }
                }
                else
                {
                    using (StreamWriter sw = File.AppendText(filepath))
                    {
                        sw.WriteLine(Message);
                    }
                }
            }
        }
    }
    

     使用net的创建服务方式

    c:WindowsMicrosoft.NETFrameworkv4.0.30319>InstallUtil.exe C:UserslocalhostDesktopWindowsServiceMyFirstServiceinDebug7dapExec.exe
    

     

     可以看见安装完成

     

     但是在火绒全开保护的情况下会创建失败

     由于代码是类似sc create方式创建的服务我这里尝试更底层一些去看看能不能绕过

    // WindowsAPIcreatServer.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
    //
    #include <stdio.h>
    #include <Windows.h>
    #include <iostream>
    
    #define SERVICE_NAME "Myservice"
    #pragma comment(lib, "advapi32.lib")
    
    void ServiceInstall()
    {
        SC_HANDLE schSCManager;
        SC_HANDLE schService;
        char binpath[]= "c:\programdata\config.exe";
        char szPath[MAX_PATH] = { 0 };
        if (!GetModuleFileNameA(NULL, szPath, MAX_PATH))
            return;
    
        schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
        if (!schSCManager)
            return;
    
        schService = CreateServiceA(schSCManager,
            SERVICE_NAME,
            SERVICE_NAME,
            SERVICE_ALL_ACCESS,
            SERVICE_WIN32_OWN_PROCESS,
            SERVICE_AUTO_START,
            SERVICE_ERROR_NORMAL,
            binpath,
            NULL,
            NULL,
            NULL,
            NULL,
            NULL);
     /*   GetModuleFileName(NULL, path, MAX_PATH);
        if ((bslash = strrchr(path, '\')))
            *bslash = 0;
    
        strcpy(binpath, path);
        strcat(binpath, "\wircd.exe");
        hService = CreateService(hSCManager, "UnrealIRCd", "UnrealIRCd",
            SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
            SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, binpath,
            NULL, NULL, NULL, NULL, NULL);*/
        if (!schService)
        {
            CloseServiceHandle(schSCManager);
            return;
        }
    
        CloseServiceHandle(schSCManager);
        CloseServiceHandle(schService);
    }
    int main(int argc, char** argv)
    {
        ServiceInstall();
        std::cout << "Hello World!
    ";
        return 0;
    
    }
    

     但是还是被拦截了,我又尝试了利用自己手上一些有签名的文件加载我的dll去创建服务,但是还是会被拦截(但是这种方法bypass360没问题)

    现在脑壳里面想的就是搞一个火绒签名的exe去加载我的创建服务恶意文件bypass

    暂时还没有想到啥其他方法

  • 相关阅读:
    oracle连接本地数据库
    ERWin 7.2下载安装及注册机
    关于oracle中to_char和to_date的用法
    2016年11月26号随笔(关于oracle数据库)
    SQL Server Browser服务的作用
    正则表达式
    server重启导致执行包的job运行失败
    Windows Log和SQL SERVER errorlog
    windows services创建和部署
    c# 读取App.config
  • 原文地址:https://www.cnblogs.com/-zhong/p/14502469.html
Copyright © 2020-2023  润新知