制作标记安全的ActiveX解决方案

声明:如有转载,请标明转载出处,谢谢!

 

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:0; mso-generic-font-family:roman; mso-font-pitch:variable; mso-font-signature:-1610611985 1107304683 0 0 159 0;} @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:-1610611985 1073750139 0 0 159 0;} @font-face {font-family:Verdana; panose-1:2 11 6 4 3 5 4 4 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:536871559 0 0 0 415 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt;} a:link, span.MsoHyperlink {mso-style-priority:99; color:blue; mso-themecolor:hyperlink; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {mso-style-noshow:yes; mso-style-priority:99; color:purple; mso-themecolor:followedhyperlink; text-decoration:underline; text-underline:single;} p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph {mso-style-priority:34; mso-style-unhide:no; mso-style-qformat:yes; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; text-indent:21.0pt; mso-char-indent-count:2.0; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt;} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:706221950; mso-list-type:hybrid; mso-list-template-ids:-1605717948 -1623441768 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l0:level1 {mso-level-text:%1); mso-level-tab-stop:none; mso-level-number-position:left; margin-left:33.0pt; text-indent:-18.0pt;} @list l1 {mso-list-id:1373454108; mso-list-type:hybrid; mso-list-template-ids:885918872 -259650730 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l1:level1 {mso-level-text:%1); mso-level-tab-stop:none; mso-level-number-position:left; margin-left:45.75pt; text-indent:-30.0pt;} @list l2 {mso-list-id:2052919894; mso-list-type:hybrid; mso-list-template-ids:1577632778 -739375984 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l2:level1 {mso-level-text:%1); mso-level-tab-stop:none; mso-level-number-position:left; margin-left:33.0pt; text-indent:-18.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} -->

1 、背景

   经常上网的人对 JS 都不会陌生, Mircosoft 提供的 WshShell 库可以让 JS 访问本地 Windows 内核,如运行本地程序、操作注册表内容、创建快捷方式和访问本地文件系统等。在执行这些操作时,我们需要创建一个 WshShell 对象: var obj =new ActiveXObject(“WScript.Shell”) ,通过 obj 执行我们需要的操作。由于 IE 的默认安全权限不允许我们使用 WScript.Shell ,也就是上述创建 obj 对象时会抛出异常,制作标记安全的 ActiveX 解决方案便应运而生。

2 ActiveX 控件原理与创建

   ActiveX 控件实质是一种支持 IUnknown 接口的 OLE 对象,或者说是 COM 对象,支持自注册和反注册,其生命期由容器控制。 ActiveX 支持以下几个特性:

1)  初始化安全:当初始化时,控件从 IPersist* 接口(本地或远程 URL )接收数据初始化状态,由于数据可能来自不信任源,因此就存在一个潜在的安全隐患。控件标注为初始化安全,相当于声明无论用什么值来初始化控件,都不会威胁到用户系统的安全。

2)  脚本安全:代码签名可以保证代码是可信任的。然而,允许脚本访问 ActiveX 控件又引出新的安全问题。即使控件在用户手上是安全的,但是被非信任脚本自动化也不一定就必然是安全的。例如, Microsoft Word 是可信任的工具,然而恶意脚本可以使用它的自动化模型来删除用户电脑上的文件、安装宏病毒或者更糟糕的事情。控件标注为脚本安全,相当于声明网页的脚本访问控件的函数或属性,控件本身不会危及用户安全行为。

3)  运行时许可:大多数 ActiveX 控件都支持设计时许可和运行时许可(免费控件例外)。设计时许可指开发者通过合法购买控件生成应用程序或者网页;运行时许可指用户运行的应用程序或者显示的页面加载了合法购买的控件。

4)  压缩脚印(为了快速下载):我们可以把控件打成 cab 包便于网络下载。

5)  数字认证:用户的 IE4 或后续版本的默认安全设置级下, HTML 页面里标识为 object 的对象必须经过数字签名。

   有两种创建 ActiveX 控件的方式:

1)          使用 ATL COM AppWizard 向导新建一个 ActiveX 工程,新建(或者使用“ ClassWizard ”新建) ATL Class 类, 向导会自动产生一个 coclass 和一个默认接口。在“ ClassView ”标签下,右键点击生成的默认接口名,如果添加方法,则选取“ Add Method… ”;如果要添加属性,则选取“ Add Property… ”。如果添加的方法是供外界调用的,则要声明为 public 。在方法中可以添加本地调用的一些内核操作。

2)          使用 MFC ActiveX ControlWizard 向导创建 MFC 控件。使用这种方式创建控件,有用户反馈不能调用控件提供的方法问题,我想可能是因为控件方法不具有 public 访问属性,或者是自动化接口问题。

3 ActiveX 安全问题

   脚本里使用 ActiveX 会弹出讨厌的对话框,这是因为控件没有标注为安全的,可能是未声明初始化安全 / 脚本安全 / 两者都是。有两种方式解决这样的问题:

1 )一种方法是使用组件种类管理,在系统注册表中创建相应的项。在加载控件前, IE 会先检查注册表,然后确定是否显示这些项。

BEGIN_CATEGORY_MAP(CXMediaJSFunc)

  IMPLEMENTED_CATEGORY(CATID_SafeForScripting)

  IMPLEMENTED_CATEGORY(CATID_SafeForInitializing)

END_CATEGORY_MAP()

2 )控件实现 IObjectSafety 接口。如果控件实现 IObjectSafety 接口, IE 在加载控件之前,会先调用 IObjectSafety ::SetInterfaceSafetyOptions 方法,再决定控件是否初始化 / 脚本安全。

包含头文件 #include <atlctl.h>

类声明中继承 IObjectSafety 接口: class CYourClass …public IObjectSafetyImpl< CYourClass,INTERFACESAFE_FOR_UNTRUSTED_CALLER|INTERFACESAFE_FOR_UNTRUSTED_DATA>

添加接口名 BEGIN_COM_MAP(CYourClass) COM_INTERFACE_ENTRY(IObjectSafety) END_COM_MAP()

如此即可。

当然,除了上述两种方法,还有数字签名,这个需要花钱购买 KEY ,本文就不深入讨论。

4 、脚本测试

   JS 调用 ActiveX 有两种方式:

1)  静态调用:在 HTML 中声明控件对象,然后通过声明的对象 id 进行调用。

<object id=" 控件 ID" classid="clsid: 控件 CLSID" codebase=" 控件位置 "></object>

document.getElementById(" 控件 ID ").Test() Test 为控件中的公有方法。

2)  动态调用: new 一个 ActiveX 对象,然后通过对象调用控件方法。

Var obj = new ActiveXObject(“ATTEST.JSFunc.1”);obj.Test()

   测试中,对话框没有了,函数调用成功。但是对于 WindowsXP SP2 及以上版本,默认安全级别的 IE 打开任何包括了控件或脚本的网页会在 IE 地址栏下面弹出一个条框,这是为了提醒用户这样的操作可能会有安全上的风险,可以在网页源文件的 <html> <head> 之间添加注释 <!--  saved from url=(0025)http://localhost/ d/9.html --> 即可。这句话的作用是让 IE 使用 Internet 区域的安全设置,而非本地电脑上的区域设置。 0025 表示后面 URL 的字符个数, URL 字符串指向注册这个组件或脚本的地方。

5 、展望

   本文是基于 IE 浏览器内核展开的,开发环境是 VC6.0 。现在用户中流行的浏览器还有 Firefox chrome 等其他浏览器,让其他浏览器也支持访问控件是进一步的工作。如果有感兴趣的网友,欢迎一起探讨。

6 、参考

   ms-help://MS.MSDNQTR.2004JAN.1033/ActiveX/workshop/components/activex/tutorial.htm

  http://www.cnblogs.com/llf/archive/2006/05/10/ActiveXSafety.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值