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:\Windows\Microsoft.NET\Framework\v4.0.30319>InstallUtil.exe C:\Users\localhost\Desktop\WindowsService\MyFirstService\bin\Debug\7dapExec.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!\n";
    return 0;

}

 但是還是被攔截了,我又嘗試了利用自己手上一些有簽名的文件加載我的dll去創建服務,但是還是會被攔截(但是這種方法bypass360沒問題)

現在腦殼里面想的就是搞一個火絨簽名的exe去加載我的創建服務惡意文件bypass

暫時還沒有想到啥其他方法

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM