ASP.NET虚拟主机安全漏洞解决方案[3]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 sukey00 的 blog


  其余所有的盘都只给于administrators和system用户的完全控制权限,删除其他所有用户并替换子目录。

  d:\test(用户网站目录)继承现有属性并增加app_test_user和iis_test_user完全控制的权限并替换子目录。

  以后每增加一个网站都以此类推。

  但是,至此,system.io还是对c:\windows又读取权限的,(怀疑network servers用户属于users组,但是好多服务都要使用users组来执行的,所以不能把c:\windwos去掉users组的读取权限)但必须知道系统路径,有两种方案解决。

  1、 再安装系统的时候使用无人值守安装,更换c:\windows默认安装路径,如更改为c:\testtest(要符合dos的命名规则,不能超过8个字符)。这个是必需的

  2、 以下位置具有指派给 iis_wpg 的权限:

  %windir%\help\iishelp\common – 读取
  %windir%\iis temporary compressed files – 列出、读取、写入
  %windir%\system32\inetsrv\asp compiled template – 读取
  inetpub\wwwroot(或内容目录)- 读取、执行

  此外,iis_wpg 还具有以下用户权限:

  忽略遍历检查(sechangenotifyprivilege)

  作为批处理作业登录(sebatchlogonright)

  从网络访问此计算机(senetworklogonright)

  当然两种方法结合起来算是最安全的方案,一般使用第一种方案已经算是很安全的,毕竟是用一个webshell来猜测8位字符的目录还是需要花费时间的。使用防火墙很容易就能察觉出来,并加以控制。

  第二种可能根据所安装软件不同还要相应增加目录的读取权限,详细情况要根据软件来确定。

  如果主机用户比较多,这将是一个相当大的劳动量,推荐使用程序来解决问题,下面给出网上不常见的针对iis应用程序池操作的代码和针对iis虚拟目录的操作代码。

操作iis应用程序池

using system;
using system.directoryservices;
using system.reflection;

namespace adsi1
{
 ///
 /// small class containing methods to configure iis.
 ///
 class configiis
 {
  ///
  /// the main entry point for the application.
  ///
  [stathread]
  //主程序入口,可以选择用哪些,我为了方便,全部功能都写上去了。
  static void main(string[] args)
  {
   string apppoolname = "myapppool";
   string newvdir1 = "myvdir";
   directoryentry newvdir = createvdir(newvdir1);

   createapppool(apppoolname);
   assignapppool(newvdir, apppoolname);

   configapppool("stop",apppoolname);
  }

  //创建虚拟目录
  static directoryentry createvdir (string vdirname)
  {
   directoryentry newvdir;
   directoryentry root=new directoryentry("iis://localhost/w3svc/1/root");
   newvdir=root.children.add(vdirname, "iiswebvirtualdir");
   newvdir.properties["path"][0]= "c:\\inetpub\\wwwroot";
   newvdir.properties["accessscript"][0] = true;
   newvdir.commitchanges();
   return newvdir;
  }

  //创建新的应用程序池。
  static void createapppool(string apppoolname)
  {
   directoryentry newpool;
   directoryentry apppools=new directoryentry("iis://localhost/w3svc/apppools");
   newpool=apppools.children.add(apppoolname, "iisapplicationpool");
   newpool.commitchanges();
  }

  static void assignapppool(directoryentry newvdir, string apppoolname)
  {
   object[] param={0, apppoolname, true};
   newvdir.invoke("appcreate3", param);
  }

  //method是管理应用程序池的方法,有三种start、stop、recycle,而apppoolname是应用程序池名称
  static void configapppool(string method,string apppoolname)
  {
   directoryentry apppool = new directoryentry("iis://localhost/w3svc/apppools");
   directoryentry findpool = apppool.children.find(apppoolname,iisapplicationpool");
   findpool.invoke(method,null);
   apppool.commitchanges();
   apppool.close();
  }

  //应用程序池的列表
  static void apppoollist()
  {
   directoryentry apppool = new directoryentry("iis://localhost/w3svc/apppools");
   foreach(directoryentry a in apppool.children)
   {
    console.writeline(a.name);
   }
  }

  private void vdirtoapppool()
  {
   directroryentry vd = new directoryentry("iis://localhost/w3svc/1/root/ccc");
   console.writeline(vd.properties["apppoolid"].value.tostring());
  }
 }
}


 iis6操作的例子

using system;
using system.directoryservices;
using system.collections;
using system.text.regularexpressions;
using system.text;

namespace wuhy.toolbox
{
 /// </summary>

 public class iisadminlib
 {
  #region username,password,hostname的定义
  public static string hostname
  {
   get
   {
    return hostname;
   }
   set
   {
    hostname = value;
   }
  }

 public static string username
 {
  get
  {
   return username;
  }
  set
  {
   username = value;
  }
 }

 public static string password
 {
  get
  {
   return password;
  }
  set
  {
   if(username.length <= 1)
   {
    throw new argumentexception("还没有指定好用户名。请先指定用户名");
   }
  password = value;
 }
}

public static void remoteconfig(string hostname, string username, string password)
{
 hostname = hostname;
 username = username;
 password = password;
}

private static string hostname = "localhost";
private static string username;
private static string password;
#endregion

#region 根据路径构造entry的方法

/// <summary>
/// 根据是否有用户名来判断是否是远程服务器。
/// 然后再构造出不同的directoryentry出来
/// </summary>
/// <param name="entpath">directoryentry的路径</param>
/// <returns>返回的是directoryentry实例</returns>

public static directoryentry getdirectoryentry(string entpath)
{
 directoryentry ent;
 if(username == null)
 {
  ent = new directoryentry(entpath);
 }
 else
 {
  // ent = new directoryentry(entpath, hostname+"\\"+username, password, authenticationtypes.secure);
  ent = new directoryentry(entpath, username, password, authenticationtypes.secure);
 }
 return ent;
}

#endregion
#region 添加,删除网站的方法

/// <summary>
/// 创建一个新的网站。根据传过来的信息进行配置
/// </summary>
/// <param name="siteinfo">存储的是新网站的信息</param>

public static void createnewwebsite(newwebsiteinfo siteinfo)
{
 if(! ensurenewsiteenavaible(siteinfo.bindstring))
 {
  throw new duplicatedwebsiteexception("已经有了这样的网站了。" + environment.newline + siteinfo.bindstring);
 }
 string entpath = string.format("iis://{0}/w3svc", hostname);
 directoryentry rootentry = getdirectoryentry(entpath);
 string newsitenum = getnewwebsiteid();
 directoryentry newsiteentry = rootentry.children.add(newsitenum, "iiswebserver");
 newsiteentry.commitchanges();
 newsiteentry.properties["serverbindings"].value = siteinfo.bindstring;
 newsiteentry.properties["servercomment"].value = siteinfo.commentofwebsite;
 newsiteentry.commitchanges();
 directoryentry vdentry = newsiteentry.children.add("root", "iiswebvirtualdir");
 vdentry.commitchanges();
 vdentry.properties["path"].value = siteinfo.webpath;
 vdentry.commitchanges();
}

/// <summary>
/// 删除一个网站。根据网站名称删除。
/// </summary>
/// <param name="sitename">网站名称</param>

public static void deletewebsitebyname(string sitename)
{
 string sitenum = getwebsitenum(sitename);
 string siteentpath = string.format("iis://{0}/w3svc/{1}", hostname, sitenum);
 directoryentry siteentry = getdirectoryentry(siteentpath);
 string rootpath = string.format("iis://{0}/w3svc", hostname);
 directoryentry rootentry = getdirectoryentry(rootpath);
 rootentry.children.remove(siteentry);
 rootentry.commitchanges();
}

#endregion
#region start和stop网站的方法

public static void startwebsite(string sitename)
{
 string sitenum = getwebsitenum(sitename);
 string siteentpath = string.format("iis://{0}/w3svc/{1}", hostname, sitenum);
 directoryentry siteentry = getdirectoryentry(siteentpath);
 siteentry.invoke("start", new object[] {});
}

public static void stopwebsite(string sitename)
{
 string sitenum = getwebsitenum(sitename);
 string siteentpath = string.format("iis://{0}/w3svc/{1}", hostname, sitenum);
 directoryentry siteentry = getdirectoryentry(siteentpath);
 siteentry.invoke("stop", new object[] {});
}

#endregion
#region 确认网站是否相同

/// <summary>
/// 确定一个新的网站与现有的网站没有相同的。
/// 这样防止将非法的数据存放到iis里面去
/// </summary>
/// <param name="bindstr">网站邦定信息</param>
/// <returns>真为可以创建,假为不可以创建</returns>

public static bool ensurenewsiteenavaible(string bindstr)
{
 string entpath = string.format("iis://{0}/w3svc", hostname);
 directoryentry ent = getdirectoryentry(entpath);
 foreach(directoryentry child in ent.children)
 {
  if(child.schemaclassname == "iiswebserver")
  {
   if(child.properties["serverbindings"].value != null)
   {
    if(child.properties["serverbindings"].value.tostring() == bindstr)
    {
     return false;
    }
   }
  }
 }
 return true;
}

#endregion
#region 获取一个网站编号的方法
/// <summary>
/// 获取一个网站的编号。根据网站的serverbindings或者servercomment来确定网站编号
/// </summary>
/// <param name="sitename"></param>
/// <returns>返回网站的编号</returns>
/// <exception cref="notfoundwebsiteexception">表示没有找到网站</exception>

public static string getwebsitenum(string sitename)
{
 regex regex = new regex(sitename);
 string tmpstr;
 string entpath = string.format("iis://{0}/w3svc", hostname);
 directoryentry ent = getdirectoryentry(entpath);
 foreach(directoryentry child in ent.children)
 {
  if(child.schemaclassname == "iiswebserver")
  {
   if(child.properties["serverbindings"].value != null)
   {
    tmpstr = child.properties["serverbindings"].value.tostring();
    if(regex.match(tmpstr).success)
    {
     return child.name;
    }
   }
   if(child.properties["servercomment"].value != null)
   {
    tmpstr = child.properties["servercomment"].value.tostring();
    if(regex.match(tmpstr).success)
    {
     return child.name;
    }
   }
  }
 }

 throw new notfoundwebsiteexception("没有找到我们想要的站点" + sitename);
}

#endregion
#region 获取新网站id的方法
/// <summary>
/// 获取网站系统里面可以使用的最小的id。
/// 这是因为每个网站都需要有一个唯一的编号,而且这个编号越小越好。
/// 这里面的算法经过了测试是没有问题的。
/// </summary>
/// <returns>最小的id</returns>

public static string getnewwebsiteid()
{

本文关键:ASP.NET虚拟主机安全漏洞解决方案
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top