ASP: mod_rewrite or URLRewrite with Classic ASP (Sorta)
Using a Custom 404 error script in IIS makes it possible to emulate a very basic URL rewrite.
Sample Environment
- IIS 6
- Website domain: "www.me.com"
- URLRewrite directory: "code"
Steps
1. Create a file named rewrite.asp in the code directory.
2. Add a Custom Error for the 404 Not Found. Open up IIS -> Right click Website -> Properties -> Custom Errors -> 404,URL,"/code/rewrite.asp" or you can do it programatically:
Add custom 404 for a specific directory in a specific website.
<%
on error resume next
myDomain = "www.me.com"
myrewritedir = "code"
Set objW3SVC = GetObject("IIS://localhost/W3SVC")
For Each objSITE in objW3SVC
If objSITE.class = "IIsWebServer" Then
websiteNameArr = objSITE.ServerBindings
for j = 0 to Ubound(websiteNameArr)
websiteName = websiteNameArr(j)
If instr(websiteName,myDomain) > 0 then
Set objIISNewDir = GetObject("IIS://localhost/W3SVC/" & objSite.Name & "/root")
Set CodeDir = objIISNewDir.Create("IIsWebDirectory",myrewritedir )
CodeDir.SetInfo
Set objIISNewDir = Nothing
Set objIISRewriteRootDir = GetObject("IIS://localhost/W3SVC/" & objSite.Name & "/root/" & myrewritedir)
CustomErrors = objIISRewriteRootDir.HttpErrors
For i = 0 To UBound(CustomErrors)
If Left(CustomErrors(i),3) = "404" then
CustomErrors(i) = "404,*,URL,/" & myrewritedir & "/rewrite.asp"
objIISRewriteRootDir.HttpErrors = CustomErrors
objIISRewriteRootDir.SetInfo
Exit For
End If
Next
Set objIISRewriteRootDir = Nothing
End if
Next
End if
Next
%>3. Copy and paste the following code into rewrite.asp
<%
'In IIS, 404 pages that are directed to an URL have the "error" URL attached in the query string.
'It looks something like this 404;http://www.me.com:80/code/nosuchfileblahblah1.asp
'We're gonna use it.. so grab it.
script = request.servervariables("QUERY_STRING")
if instr(script,"/") > 1 then
myArray = split(script,"/")
if instr(myArray(Ubound(myArray)),".asp") = 0 then
myID = myArray(Ubound(myArray)) 'This is the method for obtaining the ID if you end your URL in the ID. Example: http://www.me.com/code/this-is-good-code/1
else
myID = replace(myArray(Ubound(myArray)),".asp","") 'This is the method for obtaining the ID if you end your URL in a fake extension. Example: http://www.me.com/code/this-is-good-code/1.asp
end if
end if
'Now that you've extracted your code ID, just go about your business, you are done!
'Here's some sample code
if isNumeric(myID) then 'Make sure it's an ID and not some malicious code
response.write "The ID Extracted from the URL is: " & myID
else
response.redirect "http://www.me.com"
end if
%>4. Call your tricked out webpage URI.
I installed a sample of this script @ netnerds.net. Click on the following URL to see the results: netnerds.net/code/example-in-the-house/1. You can also look around RealCajunRecipes.com. I use that code (heavily modified) in the recipes directory to make SEO friendly URLs. Woo!
Site-to-Site VPN using Windows 2003
[super old post, reposted]
A few weeks ago, I asked in an IT forum if it was possible to have a "perma-VPN" connection with Windows XP.
After searching for awhile, I found that Microsoft calls perma-VPNs "Demand-Dial Interfaces" and these can only be found in the Windows Server family, not Windows XP.
The best way toget as close to a perma-vpn as possible in XP is to Use Dial Up Networking to login. It initiates the connection for you and you can go about your day. The blinkie icon and actual having to dial in reminded me that this was not a transparent solution..and I really wanted one. So I decided to look into a better solution.
You can use a Windows 2003 server on your network to do WAN dialing using RRAS and Demand Dial Connections. Its incredible easy to setup.
For kicks, I even installed Win2k3 on my laptop (lots of unused licenses at work) and just do a Demand Dial connection to each of the two outside servers. Outlook works like a charm where ever I go.
Here is a quickie view of the topology of my network: (the name of my domain is windomain.com)
| Function of Server | Physical Location | Internal IP (behind firewall) | External Hostname |
| Main | Downtown LA | 10.0.0.100 | dtown.windomain.net |
| Backup | Santa Monica LA | 10.0.1.100 | sm.windomain.net |
| Laptop | West Hollywood | 10.0.2.100 | weho.windomain.net |
As stated previously, each of the three machines are Windows 2003 servers. I will explain more about the "External Hostnames" in a bit.
The "firewall" is a regular broadband router with VPN Pass-through enabled and port 1723 forwarding to the internal RRAS server.
Because I pay for bandwidth on the Main server, I decided to try to route as little traffic through it as possible. Thus, Laptop maintains two Demand-Dial Interfaces even though I could have actually reached Backup via the WAN link through Main.
Why did I list "External Hostname?" Well, only one server has a truly static hostname and in order for this WAN to work smoothly, hostnames for dial-ins are important. Backup and Laptop are on SoHo DSL lines sotheir IPs change every now and again so I use my internal DNS server to give the external IPs a DNS entry. When their IP changes due to a power outage or firewall reboot, I simply go update the DNS for windomain.net to point to the new IP. Each ofthe servers use my internal DNS server so they don't look for "real" entries of windomain.net, which of course, would not have the sm, weho and dtown entries.
I won't go into extreme detail on how to setup the RRAS but here's a quick idea of what I did to connect Main and Backup
- On all three servers, I setup RRAS to support Demand-Dial Interfaces
- On Main, I opened the RRAS interface and right clicked on Interfaces then selected "Create new Demand-Dial Interface"
- I named the Interface Main2Backup
- On the next few prompts, I selected VPN and PPTP
- When prompted to enter a hostname for the router to which I am connecting, I typed in sm.windomain.net
- I selected Route packets on this interface and create new user account for remote router to dial [back]in
- Destination -> Add -> Destination (recall the IP address for Backup/sm above) is 10.0.1.0. Subnet is 255.255.255.0. Metric is left at 1.
- NextIwasprompted to create a new user. The username is created from the name of the Interface. In this instance, my username is Main2Backup
- Now, I am prompted for the dial-out credentials. Immediately after I am finished with this setup, I will go setup a Demand-Dial Interface on Backup. I know that I will follow the same pattern so the username I will create on Backup/sm will be Backup2Main. I enter this information now.
- Finish. Repeat the above steps with slightly different hostname, interface name and dial-in account name on Backup. Eventually, the same will be done on Laptop.
In conclusion, Windows 2003 RRAS provides an awesome and easy solution for setting up WANs without physical router hardware. The process can take as little as a few minutes once appropriate DNS entries have been made.
Good resources for this topic can be found at http://www.microsoft.com/vpn.The whitepaper I used for my setup is found here.
VBScript: Track Outbound E-mail Addresses in Exchange
I use the code below to extract e-mail addresses from outbound e-mails and insert them into a SQL table. This is useful when using custom Exchange/SQL Server solutions to put a lid on spam ;D
<script LANGUAGE="VBScript">
Sub ISMTPOnArrival_OnArrival(ByVal Msg, EventStatus )
On Error Resume Next
EventStatus = 0 'Run the next sink by default.
Connstring = "Driver={SQL Server}; Server=servername; Database=dbname; UID=username; PWD=mypass"
Set envfields = Msg.EnvelopeFields
Set fields = Msg.Fields
RecipList = LCase(envfields("http://schemas.microsoft.com/cdo/smtpenvelope/recipientlist"))
IpAddr = LCase(envfields("http://schemas.microsoft.com/cdo/smtpenvelope/clientipaddress"))
Sender = LCase(fields.Item("urn:schemas:mailheader:from"))
recipList = replace(LCase(RecipList),"smtp:","")
recipArray = split(RecipList,";")
For aa = LBound(recipArray) To UBound(recipArray)
If Len(recipArray(aa)) > 0 and Len(ipaddr) = 0 and Sender = """chrissy lemaire"" <chrissy@netnerds.net>" Then
Set rs = CreateObject("adodb.recordset")
strSQL = "select whiteline from whitelist where whiteline = '" & recipArray(aa) & "'"
rs.Open strSQL,Connstring,1,2
If rs.eof and rs.bof Then
rs.Addnew
rs("whiteline") = recipArray(aa)
rs.Update
End If
rs.close
Set rs = nothing
End If
Next
Msg.DataSource.Save
EventStatus = 0 'cdoRunNextSink
End Sub
</script>SQL: Quickly count instances of a variable within a string.
Using SQL and want to know how many times 'http' appears in a string using about one line of code? Find out by creatively using the replace() function.
Declare @myStr varchar(1500), @countStr varchar(100)
Set @myStr = 'http://www.buydrugshere.com go here! http://www.onlinemortage.com right there!'
Set @countStr = 'http'
Select (len(@myStr)-len(replace(@myStr,@countStr,'')))/len(@countStr) as theCountThis method is effective in any language that has a replace function.
FreeBSD to SuSE
Awhile back I tried out FreeBSD for a couple months. I was used to SuSE (which I'm back to) so I ran a few commands to make my environment a bit more familiar. If you are a recent convert, you can use this code too.
whereis bash
chsh change shell to bash (used sysinstall to install bash)
ln -s /etc/periodic/weekly/310.locate /bin/updatedb (for locate)
pw user mod chrissy -G wheel (for su to work)
alternatively, I could have done this:
edit /etc/ssh/sshd_config add PermitRootLogin YES
but I didnt.
edit /etc/rc.conf add apache_enable="yes"
edit .profile add PS1="\h:\w "
edit .profile add alias dir="ls -la"
made note that restart scripts are in here: /etc/rc.d/ASP: Sustain Remote Cookie Sessions in an ASP/VBScript
I dug up this cold from my old netnerds blog. For Googlers wondering if sustaining a remote session is possible, the answer is yes; I've sustained remote cookie sessions using both ASP & VBScript. I've provided simplified code below. It should be self explanatory. If not, drop me a comment and I'll explain it.
<%
url1 = "http://www.netnerds.net/session/login.asp"
url2 = "http://www.netnerds.net/session/controlPanel.asp"
data1 = "username=bobby&pass=thepass&submit=Login"
theCookie = httpSessionRequest(1, "POST", url1, data1, noCookie, noViewState)
finalHTML = httpSessionRequest(2, "GET", url2, nodata, theCookie, noViewState)
response.write finalHTML
'---------------------------------------------------------
'THE FUNCTION
'---------------------------------------------------------
Function httpSessionRequest(theStep, method, url, data, cookie, viewState)
'FYI, viewstate code has been ripped out.
'Previously, I screenscraped to get the viewstate hidden field for aspx pages.
baseURL = "http://www.netnerds.net/" 'This is to fix any broken images in the output.
if len(cookie) = 0 then cookie = "dummy=dummy;"
HTTPReferrer = Trim(url)
postVars = Trim(data)
Set XMLHTTP = server.CreateObject("MSXML2.serverXMLHttp")
XMLHTTP.open method, Trim(url), false
if UCASE(method) = "POST" Then
XMLHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
End If
XMLHTTP.SetRequestHeader "Referer", HTTPReferrer 'just in case the server cares
XMLHTTP.setRequestHeader "Cookie", "excuse the Microsoft bug"
XMLHTTP.setRequestHeader "Cookie", cookie
XMLHTTP.send postVars
'wait for response
While XMLHTTP.readyState <> 4
XMLHTTP.waitForResponse 1000
Wend
strHeaders = XMLHTTP.getAllResponseHeaders()
hArr = split(strHeaders,"Set-Cookie: ")
for kk = 1 to ubound(hArr)
theCookie = left(hArr(kk),instr(hArr(kk),"path=/")-2)
myCookie = myCookie & " " & theCookie
next
if len(myCookie) = 0 then mycookie = cookie
sReturn = replace(XMLHTTP.responsetext,"../",baseURL)
if cint(theStep) = 1 then
httpSessionRequest = mycookie
elseif cint(theStep) = 2 then
httpSessionRequest = sReturn
elseif cint(theStep) = 3 then
'You can add stuff here to debug
httpSessionRequest = mycookie
response.write theCookie & "<p>" & mycookie & "<p>" & sReturn & "<hr /><p>"
end if
set XMLHTTP = nothing
END FUNCTION
%>Change Exchange Server’s Administrative Group using adsiedit
The MSPress Exchange Training Kit supposedly says that you must uninstall/reinstall an Exchange Server in order to move it from one Administrative Group to another. I managed to do change the administrative group for one of my servers today using adsiedit. Here’s how I did it.
VBScript: Ban IPs in IIS Programatically
I used the following code a while back as part of a solution to automate the banning of spammers via their IP address.
'Here, we will pretend this is an imported list
Dim XMLarr(1)
XMLarr(0) = "65.19.238.21"
XMLarr(1) = "198.31.175.100"
Set objIIS = GetObject("IIS://localhost/w3svc") 'careful, this sets IPDENY on every child web
Set IISipsec = objIIS.IPSecurity
If (IISipsec.GrantByDefault = True) Then
arrIP = IISipsec.IPDeny
arrIPSize = ubound(arrIP)
For i = 0 to arrIPSize
arrIPstring = arrIPstring & "," & arrIP(i) 'going to use for dupes later.
Next
ReDim preserve arrIP(arrIPSize + UBound(XMLarr)+1)
for i = 0 to UBound(XMLarr)
myNum = arrIPSize + i+1
If InStr(arrIPstring,XMLarr(i)) = 0 Then
arrIP(myNum) = XMLarr(i)
End If
Next
IISipsec.IPDeny = arrIP
objIIS.IPSecurity = IISipsec
objIIS.SetInfo
End If
Set IISipsec = nothing
Set objIIS = nothingRIBCL: Reset Administrator Password on iLO
Last night I got locked out of my Compaq DL360's iLO. I searched the web to find how to reset the Administrator password and read that, for the most part, I'd have to be at the console. Bah! My servers are in San Jose and I'm up here in San Francisco, I didn't want to make a trip just to reset my passwords. I continued to look around the web for a solution and found that I could reset the Administrator password (or even add another user with admin privs) using Compaq's Remote Insight Board Command Language. Apparently, if you are processing RIBCL commands through your given operating sytem so long as you have the rights to login to the server. I logged in as my Windows Domain Admin and performed the following steps (Linux users, you can download the RPM for HPONCFG):
1. I installed SNMP because it was a preprequesite for HP Insight Management Agents.
2. I dowloaded and installed the HP Insight Management Agents.
3. I then downloaded HP Lights-Out Online Configuration Utility.
4. I ran into NTVDM errors trying to run the file so I just used WinRAR to extract the contents into C:\hp\ilo. I also extracted the zip file contained within the initial archive.
5. I then downloaded the HP Lights-Out XML Scripting Sample for Windows (Linux users can download the files in tgz format here or here.) extracted it and found the file I was looking for --
<ribcl VERSION="2.0">
<login USER_LOGIN="Administrator" PASSWORD="boguspassword">
<user_INFO MODE="write">
<mod_USER USER_LOGIN="Administrator">
<password value="newpass"/>
</mod_USER>
</user_INFO>
</login>
</ribcl>6. Using notepad, I opened up the sample file and modified it slightly. Initially, I just removed the LOGIN and ran the file but HPONCFG gave me a syntax error. I then added it back and gave the Administrator a bogus password. Apparently, the LOGIN line is required for syntax reasons but it is not actually processed.
7. Next, I opened a command line and changed directories to C:\hp\ilo and typed the following:
HPONCFG.exe /f Administrator_reset_pw.xml /l log.txt > output.txt8. I opened up Firefox, navigated to my iLO machine and viola! I was able to login as Administrator.
If changing Administrator's password seems too scary, you can also add another user with administrator privileges. You can then login as that user and change the Administrator password via the web console. Use the following code, suited to your liking:
<ribcl version="2.0">
<login USER_LOGIN="Administrator" PASSWORD="boguspass">
<user_INFO MODE="write" >
<add_USER
USER_NAME="Chrissy"
USER_LOGIN="Chrissy"
PASSWORD="mynewpass">
<reset_SERVER_PRIV value = "Y" />
<admin_PRIV value = "Y" />
</add_USER>
</user_INFO>
</login>
</ribcl>Here's a zip of just hponcfg.exe, add_user.xml, and Administrator_reset_pwd.xml. Since it's an exe with no apparent supporting files, y ou may be able to just use that.
I see quite a few people have hit this page..if you found it useful, please let me know! If not, drop me a note and I'll see how I can help.


