Site Encryption
This tutorial will show you how to encrypt an entire site by recursively working though the folder structure encrypting all the cfm files and copying any non-cfm files whilst keeping the directory structure intact.
Just set the variables at the top and save the file as encrypt.cfm or whatever. I’ve kept the output tags in so you can see what’s going on as it’s handy for debugging any problems.
<cfparam name="encryptFile" default="">
<!---
The purpose of this file is to provide a method to recursively encrypt all ColdFusion files with a folder.
The script first goes through all the folders starting from a set point and builds up a query of all the folders and file locations
Finally it will either encrypt the file, or copy it if it is a non-cfm file, to a new location.
CAVEAT:
There does appear to be a bug in CF MX6.1 when using a combination of the version number in cfencode and stating a header in the command line.
Basically if you want a header in the encrypted file you must use v1 encrypting otherwise use v2
--->
<!---set the root folder for the site you want to encrypt--->
<cfset startDir="D:\sites\pathtosite">
<!---set a root folder for where you want to save the encrypted/non-encrypted files--->
<cfset newDir="D:\sites\pathtoencrypted site">
<!---set the cfm file type (cfm or cfml)--->
<cfset fileExt="cfm">
<!---set the path to the cfencode.exe file--->
<cfset pathtoexe="c:\cfusionmx\bin\cfencode.exe">
<!---set the cfencode version--->
<cfset encVer="2">
<!---if you set the encVer to 1 you can set a header (see caveat above)--->
<cfset encHeader="This file has been encrypted. Unauthorised decryption attempts strictly forbidden">
<!---
Mimics the cfdirectory, action="list" command.
Updated with final CFMX var code.
Fixed a bug where the filter wouldn't show dirs.
@param directory The directory to list. (Required)
@param filter Optional filter to apply. (Optional)
@param sort Sort to apply. (Optional)
@param recurse Recursive directory list. Defaults to false. (Optional)
@return Returns a query.
@author Raymond Camden (ray@camdenfamily.com)
@version 2, April 8, 2004
@modified by Philip Williams (contact@openmindedsolutions.co.uk)
@version 2.1, May 3rd 2006
Revisions:
Removed extraneous information for the directory query
--->
<cffunction name="directoryList" output="false" returnType="query">
<cfargument name="directory" type="string" required="true">
<cfargument name="filter" type="string" required="false" default="">
<cfargument name="sort" type="string" required="false" default="">
<cfargument name="recurse" type="boolean" required="false" default="true">
<!--- temp vars --->
<cfargument name="dirInfo" type="query" required="false">
<cfargument name="thisDir" type="query" required="false">
<cfset var path="">
<cfset var temp="">
<cfif not recurse>
<cfdirectory name="temp" directory="#directory#" filter="#filter#" sort="#sort#">
<cfreturn temp>
<cfelse>
<!--- We loop through until done recursing drive --->
<cfif not isDefined("dirInfo")>
<cfset dirInfo = queryNew("name,type,directory")>
</cfif>
<cfset thisDir = directoryList(directory,filter,sort,false)>
<cfif server.os.name contains "Windows">
<cfset path = "\">
<cfelse>
<cfset path = "/">
</cfif>
<cfloop query="thisDir">
<cfset queryAddRow(dirInfo)>
<cfset querySetCell(dirInfo,"name",name)>
<cfset querySetCell(dirInfo,"type",type)>
<cfset querySetCell(dirInfo,"directory",directory)>
<cfif type is "dir">
<!--- go deep! --->
<cfset directoryList(directory & path & name,filter,sort,true,dirInfo)>
</cfif>
</cfloop>
<cfreturn dirInfo>
</cfif>
</cffunction>
<!---ceate the dirList query--->
<cfscript>
dirList = directoryList("#startDir#","","name desc");
</cfscript>
<cfoutput query="dirList">
<cfset currFold=directory>
<cfset newFold=ReplaceNocase(directory, startDir, newDir)>
currfold:#currfold#<br />
newfold:#newfold#<br />
<!---if the new directory does not exist, we create it--->
<cftry>
<cfdirectory action="create" directory="#newFold#">
newfold created<br />
<cfcatch>newfold exists<br /><!---do nothing as the folder exists---></cfcatch>
</cftry>
<!---if the file is a cfm file then we encode it here--->
<cfif type EQ "file" AND ListLast(name, '.') EQ "#fileExt#">
<cfset inputFile="#currFold#\#name#">
<cfset outputFile="#newFold#\#name#">
#inputfile#<br />
#outputfile#<br />
<!---dependent on the encryption version, we set the command line arguments--->
<cfswitch expression="#encVer#">
<cfcase value="1">
<cfscript>
ArgumentLine=inputFile;
ArgumentLine=ArgumentLine&" ";
ArgumentLine=ArgumentLine&outputFile;
ArgumentLine=ArgumentLine&" ";
ArgumentLine=ArgumentLine&" /q /h ";
ArgumentLine=ArgumentLine&chr(34);
ArgumentLine=ArgumentLine&encHeader;
ArgumentLine=ArgumentLine&chr(34);
ArgumentLine=ArgumentLine&" ";
ArgumentLine=ArgumentLine&" /v 1";
</cfscript>
</cfcase>
<cfcase value="2">
<cfscript>
ArgumentLine=inputFile;
ArgumentLine=ArgumentLine&" ";
ArgumentLine=ArgumentLine&outputFile;
ArgumentLine=ArgumentLine&" ";
ArgumentLine=ArgumentLine&" /q /v 2";
</cfscript>
</cfcase>
</cfswitch>
argumentline:#argumentline#<br />
<cfexecute
name="#PathToExe#"
arguments="#argumentLine#"
timeout="10">
</cfexecute>
encrypt complete<br />
<!---the file is not a cfm file so we just copy it to the new location--->
<cfelseif type EQ "file" AND ListLast(name, '.') NEQ "#fileExt#">
<cftry>
<cffile action="copy" source="#currFold#\#name#" destination="#newFold#\#name#">
copy:#currFold#\#name#
<cfcatch>nocopy:#currFold#\#name#<!---do nothing, file already exists---></cfcatch>
</cftry>
<cfelse>
</cfif>
<br /><br />
</cfoutput>
Site encryption complete!
That’s it! A complete site encrypted via one simple file!
Submitted by Phil Williams
Email: support<at>openmindhosting.co.uk
Web: http://www.openmindhosting.co.uk