欢迎光临
我们一直在努力

Is your .NET Code safe?-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

is your code safe?

vs.nets use of microsoft intermediate language creates big advantages, but exposes vb.net on the desktop.

by dan fergus
url: http://www.vbpj.com/upload/free/features/vbpj/2001/05may01/df0501/df0501-1.asp

in the visual studio.net (vs.net) framework, compilers such as vb, visual c++, and c# compile their source programs into microsoft intermediate language (msil), which is subsequently just-in-time (jit)-compiled into native machine code before execution. but you might not know exactly what happens in vs.net when you click on the build button, or whether your proprietary code and information are safe from prying eyes and tampering when you ship il code to your customer. ill step through the inner workings of the .net framework to show you whats new and explain what concerns you should have with msil under vb.net.

Is your .NET Code safe?-.NET教程,Asp.Net开发 what you need:
vb.net beta 1
Is your .NET Code safe?-.NET教程,Asp.Net开发

you need to be clear about several points. first, .net is designed for client/server and web apps. software is moving to the internet and to client/server-based applications, so many applications now look more like browsers than the old-style interface. .net follows this trend.
also note that .net isnt suitable for desktop applications if you care about protecting your intellectual property because you cant protect it in managed msil code on the desktop. this is a shocker to me. although msils premise is good and the .net framework and common language runtime (clr) are stable, theyre just not feasible or workable in a standalone desktop application from a security standpoint. as a vb programmer, or even a c# programmer, you have no way to write anything but this unprotected managed code in .net.
because of this limitation, you must write unmanaged c++ code if you want to protect the code in your desktop app. the only sure way to protect intellectual property: wrap the code into an unmanaged c++ component and use the com interoperability interface to call it from .net managed code. unfortunately, this isnt a workable option for vb or c# programmers.
you also should know that active server pages.net (asp.net) applications are safe because they run entirely on the server. this is the nirvana of .net—running code thats buried away on a protected server, far from the reach of any party interested in looking at your code. asp.net makes web development incredibly easy, and visual basic.net (vb.net) is a good tool for writing asp.net apps.
vb.net has a steep learning curve, and .net as a whole will experience slow acceptance. migrating from vb6 to vb.net is not easy, and youll need to support vb6 applications for some time before porting them. vb6 might now be the microsoft stepchild, but many developers will use it for a long time to come.
msil is old—but new
see what happens when you build a project in vb.net: create a sample project you can use as you generate code and assemblies. open vs.net, create a new visual basic project, add a label control to the form, and change its text property to "good bye visual basic 6.0" (see figure 1). instead of the standard hello world app, youll write a goodbyevb6 app.

Is your .NET Code safe?-.NET教程,Asp.Net开发
figure 1 | create an app under .net. click here.

you need to know some boundaries and terms before diving into .net. first, the idea of il is nothing new. the vb and c++ compilers have generated il for years, but no one discussed this publicly and no one documented it. the single biggest change from the way you shipped applications in the past is in the code the compiler generates. other than the name, the new msil bears little resemblance to the il of the vb6 compilers—so if youve worked with il in the past, prepare yourself for a whole new experience. look at an msil fragment generated from the goodbyevb6 application (see figure 2).
this code sets up a stack of eight bytes, then pushes the this pointer onto the stack and calls the get_label1 method. then the code pushes the desired label text onto the stack and calls the set_text method (see the sidebar, "how to read msil," for a more detailed explanation of the structure).
when you run the compiler, you generate a program thats not a true executable as we know it today. instead, its called an assembly (see the glossary sidebar). an assembly is a grouping of files deployed as a single file. in todays architecture, you might think of a single executable as an assembly. more accurately, an assembly groups the executable file with any support dlls, images, resources, or help files.

Is your .NET Code safe?-.NET教程,Asp.Net开发
figure 2 | treading on unsafe ground. click here.

an assembly almost always consists of at least two files: the executable and the manifest. the manifest is a list of all the files that exist inside the assembly. the executable content inside the assembly is referred to individually as a module. conceptually, modules correspond to dlls or exes; each module contains metadata, in addition to the metadata of its parent assembly. the assembly format is an enhanced version of the current portable executable (pe) format.
the standard pe header comes at the beginning of the file. inside the file is the clr header, followed by the data required to load the code into its process space—referred to as metadata (see figure 3). metadata describes to the execution engine how the module should be loaded, what additional files it needs, how to load those additional files, and how to interact with com and the .net runtime. metadata also describes the methods, interfaces, and classes contained in the module or assembly. the information the metadata provides allows the jit compiler to compile and run the module. the metadata section exposes much of your applications internals and eases the transition from disassembled il to useful code.

Is your .NET Code safe?-.NET教程,Asp.Net开发
figure 3 | of modules and metadata click here.

at the heart of .nets code deployment is the issue of managed code—code written exclusively to run under the clrs control. you can create managed code from vb.net, c#, or c++, but c++ is the only language that can create unmanaged code on the .net platform. you cant use vb6 to create unmanaged code for the .net platform because you ship assembled i386 instruction code rather than il code with vb6. you cant ship anything but il code when you use managed code, as you must with vb.net.
now look at the benefits of using the new msil code. when you leave your code at the msil stage, you can install and run it on any platform that supports the clr. this might not be a big deal to you now because the list of platforms that currently support .net is short: only 32-bit windows. but soon that list will include 64-bit platforms and .net for windows ce devices (pocket pcs). leaving your code as msil allows you to move seamlessly to these and other new platforms in the future.
another advantage of msil: the jit compiler converts the msil to native code on the target machine. so the jit compiler can take advantage of the specific hardware and optimize the code for that specific platform. this comes in handy, for example, when optimizing the code for particular registers or op-codes found on certain hardware that has a particular processor. look at the compile tabs advanced optimizations button in vb6s project properties. using the metadata in the assembly, the jit compiler knows what your code does and what the platform supports, makes these optimization decisions for you on the fly, and enhances your code performance.
yet another benefit concerns the two vs of .net: validation and verification. validation is a series of tests you can perform on your module to ensure the metadata, msil code, and file format are consistent. code that cant pass these tests might crash the execution engine or the jit compilers. once you validate your module, the code is correct and ready to run.
the code is verified when the jit compiler converts it from msil to native code. verification involves checking the metadata to ascertain the program cannot access memory or other resources it doesnt have permission for. verified code is also type-safe. this check is done even if the program is compiled directly to native code, but its not 100-percent accurate unless the jit compiler does it, because the test results depend on metadata from other assemblies. if you compile to native code before shipping, you run the risk of another assembly changing on the target machine, which will make your program type-unsafe.
using the jit compiler guarantees all related assemblies current versions are considered when the validation and verification are done. this procedure ensures that the running program will be type-safe and that it will run with the correct security permissions. you can verify and validate your code yourself using the .net sdks peverify tool.
reverse engineering is easy
perhaps the single biggest concern with shipping your assembly as msil instead of compiled code is security. remember, the assembly has a manifest of all the packages modules, and the metadata describes each of the modules in detail. the .net sdk ships with a program called ildasm, an il disassembler that takes the module and dumps well-formatted il code and metadata descriptions for all your applications modules. it cant get any easier to reverse engineer your code (see listing 1).
one common retort to this perceived problem: a real application is big and the volume of il dumped would be overwhelming. this might stop an amateur, but it wont bother someone who really wants to crack your code. the truth is this: the dump from ildasm is much easier to read than the dump from a disassembly of compiled code. an interested party can learn a lot about your application from the il dump.
keep your company secrets secret, according to microsoft, by putting any module containing company secrets on a protected server. thats fine if your program is an asp.net client/server application, but it doesnt work well if your application is a standard desktop application. how do you protect intellectual property then? the msil assembler documentation cites a reference to the command-line parameter /owner:

ilasm ... /ownerilasm ... /owner=fergus

this option encrypts an assembly with a password to prevent it from being disassembled. the problem is that microsoft is going to remove this option, which didnt do a good job in the first place. so you cant protect intellectual property in your desktop applications written with managed c++, c#, or vb code for .net beta 1.
but theres hope. before the final .net release candidate is completed, microsoft might introduce an obfuscator that alters your msils private methods to make them unreadable to anyone except the clr jit compiler. however, this wont hide your applications public methods or calls it must make to an external library. changing the names or hiding these public calls would make it impossible for the clr to link to the external functions. so a hacker can still see any slick tricks you use when calling to the system dlls when he or she digs through your il code (see the sidebar, "obfuscators hide vulnerable code").
you can use only one method now to protect your intellectual property on a desktop application. as a vb developer, you might find this difficult, but you must write your critical code in unmanaged c++ and access it from your vb.net application using the interoperability mechanism provided for accessing unmanaged code.
you cant jit-compile the code before shipping because all managed code must ship as msil. but you can compile the code to assembly form when you install it on the target machine. this sounds fine at first. the code on the install disk is still il, however, so you can extract it from the setup file manually and disassemble it separately from the install. once the image is installed on the users disk, its in assembly form rather than il. in addition to security, this buys you a little speed when the application runs because the jit compiler doesnt need to compile the il code then.
third-party vendors might hesitate
as an application vendor for desktop applications, you know what you have to do. you can write unmanaged c++ and use it from your managed vb code. you designed your application and feel safe because you know the code is good and wont corrupt process spaces it shouldnt corrupt. although managed code is guaranteed to behave itself, your unmanaged code is stable and reliable, and its yours. however, if youre a third-party vendor and choose unmanaged code over managed code in your components, you force consumers to step back from the benefits of .net and open themselves up to the same problems they have now. part of .nets beauty is the ability to write managed code and know that doing so wont corrupt the memory used by the process space of your application and other applications. but without that knowledge, vendors might avoid writing controls in .net managed code to restrict consumer access to the il and potentially to algorithms their application uses.
i love vs.net, and vb.net in particular. the ides enhancements themselves provide enough of a reason to change to vb.net (see the vbpj .net article in resources). the language enhancements add to your programming tools, and the simple access to the underlying os makes your declare life easier. vb.net serves as a fine tool for creating secure asp.net applications. however, if your main target is a thick client or desktop application, you might want to investigate the issues before shipping code with vs.net. well see what microsoft does to help developers who program desktop applications.

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Is your .NET Code safe?-.NET教程,Asp.Net开发
分享到: 更多 (0)