| « Many More Minutes | Slowing Time » |
"I work as a support developer for the trading desk at a fairly large bank," Jay P.L. writes, "some of the automatic trading systems used have grown organically from what can originally be called an Excel spread-sheet. However, the complexity can sometimes be overwhelming."
Jay continues, "When traders find functionality that doesn’t quite work as expected, our usual response is, 'just send over the spreadsheet and we'll take a look at it.' Cracking open the VBA and debugging through the issue usually finds the cause relatively quickly. Most of the time, the issue can be blamed on a close colleague who made the mistake during a particularly hectic trading atmosphere under an abundant amount of stress and pressure. These situations are usually overlooked and forgiven. The issue fixed and returned."
"Usually forgiven. Imagine my joy at finding the following code snippet."
Sub LogIn()
On Error Resume Next
Result = BatchLogin("jpl", "England1")
If Err.Number <> 0 Then
Err.Clear
PassWordReset
ElseIf Len(Result) = 0 Then
PassWordReset
End If
End Sub
Sub PassWordReset()
With ThisWorkbook.VBProject.VBComponents.Item("DataBase")
SecStr = .CodeModule.Lines(3, 1)
SecArr = Split(Right(SecStr, Len(SecStr) - 22), ",")
UserID = Replace(SecArr(0), Chr(34), "")
With frmLogIn
.txbUserId = UserID
.Show
If Len(.txbUserId) = 0 Then End
UserID = .txbUserId
Password = .txbPassword
End With
AppSts = Application.ScreenUpdating
Application.ScreenUpdating = True
Application.ScreenUpdating = AppSts
On Error Resume Next
Result = BatchLogin(CStr(UserID), CStr(Password))
If Err.Number = 0 And Len(Result) > 0 Then
q = Chr(34)
.CodeModule.DeleteLines 3, 1
On Error GoTo 0
.CodeModule.InsertLines 3, _
" Result = BatchLogin(" _
& q & UserID & q & "," _
& q & Password & q & ")"
Else
PassWordReset
End If
End With
End Sub
Jay adds, "I find the interesting point being the self-modifying VBA code, even though it can be enjoyed on so many levels."
As noted, it modifies itself. Specifically, it reads its own source code, pulls out line 3: Result = BatchLogin("jpl", "England1") extracts the username (by dropping the first 22 characters and then splitting on the comma) and then replaces the line with a new one that (hopefully) looks exactly the same except that the password is different.
It's painful to even think about what would happen if some unsuspecting coder were to add or remove lines to the top of the script, of change the indentation, or in any other way make line 3 look any different from what the rest of the code expects. |
| « Many More Minutes | Slowing Time » |