+ Reply to Thread
Results 1 to 8 of 8

Porting Python code to VB?

Hybrid View

  1. #1
    Forum Contributor Rhudi's Avatar
    Join Date
    03-08-2013
    Location
    South Carolina, US
    MS-Off Ver
    Professional Plus 2016 aka Office 365
    Posts
    201

    Porting Python code to VB?

    **DISCLAIMER** THIS IS NOT FOR HACKING. TO GET THIS FAR, YOU ALREADY HAVE DEEP ACCESS TO LIVE EQUIPMENT.

    The only point of Cisco Type 7 or Juniper Type $9$ is to obfuscate text, to prevent "over the shoulder" reading.

    I already have a VB Script to Decode Cisco Type 7 passwords.

    But, I now need to decode Juniper $9$ passwords.
    AGAIN: ONCE I HAVE A JUNIPER $9$ PASSWORD, I AM DEEP INTO THE CONFIGURATION DATA OF MY EQUIPMENT. I AM NOT HACKING ANYTHING.

    Here is a link to this type of decode done in Python Script (which I don't know how to read):
    https://github.com/mhite/junosdecode...junosdecode.py

    So, here is my question... Can someone point me some VB to decode $9$ (without accusing me of hacking)?

  2. #2
    Forum Contributor Rhudi's Avatar
    Join Date
    03-08-2013
    Location
    South Carolina, US
    MS-Off Ver
    Professional Plus 2016 aka Office 365
    Posts
    201

    Re: Porting Python code to VB?

    There are many websites where I can paste a $9$ string and it will return the Decoded text. I don't want to resort to such external means, even though any plaintext passwords would be completely isolated from their context.

    So, looks like it's time to learn python so I can port it to VB.

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Porting Python code to VB?

    Python apparently is good for data analysis.
    I flirted with it for a while, but the indentation stuff is a complete switch off for me.
    Do not even think VB, it might be dead in a couple of years time.

  4. #4
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Porting Python code to VB?

    there are several options that allow integration/mix of python and excel
    xlwings
    pyxll

    however you will need to install python or to compile the script to exe. The former being the easiest solution, but on the other hand, if you have python installed, you can simply run the scrip
    If you are pleased with a member's answer then use the Star icon to rate it.

  5. #5
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Porting Python code to VB?


  6. #6
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Porting Python code to VB?

    Here it is

    Option Explicit
    '---------------------------------------------------------------------------------------
    ' Module    : juniper_decrypt
    ' Author    : Boyan Kolev
    ' Github    : https://github.com/boyank/juniper_decrypt
    ' Purpose   : Decrypt Juniper $9$ Type password. Ported to VBA from Python https://github.com/mhite/junosdecode
    '---------------------------------------------------------------------------------------
    
    Function juniper_decrypt(strPassword) As Variant
    
        Dim intCounter As Integer
        Dim intGap As Integer
        Dim intLen As Integer
        Dim intDiff As Integer
        Dim strChars As String
        Dim strFirst As String
        Dim strToss As String
        Dim strPrev As String
        Dim strDecrypt As String
        Dim strNibble As String
        Dim strChar1 As String
        Dim strChar2 As String
        Dim vChar As Variant
        Dim vFamily As Variant
        Dim vNum_Alpha As Variant
        Dim vNibble As Variant
        Dim vDecode As Variant
        Dim objEncoding As Object
        Dim objAlpha_Num As Object
        Dim objExtra As Object
        Dim objGaps As Object
    
    
        On Error GoTo juniper_decrypt_error_handler
    
        vNum_Alpha = Array("Q", "z", "F", "3", "n", "6", "/", "9", "C", "A", "t", "p", "u", "0", "O", _
                           "B", "1", "I", "R", "E", "h", "c", "S", "y", "r", "l", "e", "K", "v", "M", "W", "8", "L", "X", "x", _
                           "7", "N", "-", "d", "V", "b", "w", "s", "Y", "2", "g", "4", "o", "a", "J", "Z", "G", "U", "D", "j", _
                           "i", "H", "k", "q", ".", "m", "P", "f", "5", "T")
    
        vFamily = Array(Array("Q", "z", "F", "3", "n", "6", "/", "9", "C", "A", "t", "p", "u", "0", "O"), _
                        Array("B", "1", "I", "R", "E", "h", "c", "S", "y", "r", "l", "e", "K", "v", "M", "W", "8", "L", "X", "x"), _
                        Array("7", "N", "-", "d", "V", "b", "w", "s", "Y", "2", "g", "4", "o", "a", "J", "Z", "G", "U", "D", "j"), _
                        Array("i", "H", "k", "q", ".", "m", "P", "f", "5", "T"))
    
    
        Set objEncoding = CreateObject("Scripting.Dictionary")
        objEncoding.Add objEncoding.Count, Array(1, 4, 32)
        objEncoding.Add objEncoding.Count, Array(1, 16, 32)
        objEncoding.Add objEncoding.Count, Array(1, 8, 32)
        objEncoding.Add objEncoding.Count, Array(1, 64)
        objEncoding.Add objEncoding.Count, Array(1, 32)
        objEncoding.Add objEncoding.Count, Array(1, 4, 16, 128)
        objEncoding.Add objEncoding.Count, Array(1, 32, 64)
    
        Set objAlpha_Num = CreateObject("Scripting.Dictionary")
        For intCounter = LBound(vNum_Alpha) To UBound(vNum_Alpha):
            objAlpha_Num.Add vNum_Alpha(intCounter), intCounter
        Next intCounter
    
    
        Set objExtra = CreateObject("Scripting.Dictionary")
        For intCounter = LBound(vFamily) To UBound(vFamily)
            For Each vChar In vFamily(intCounter):
                objExtra.Add vChar, 3 - intCounter
            Next vChar
        Next intCounter
    
        If Left(strPassword, 3) = "$9$" Then
            strChars = Right(strPassword, Len(strPassword) - 3)
            vNibble = Nibble(strChars, 1)
            strFirst = vNibble(0)
            strChars = vNibble(1)
            vNibble = Nibble(strChars, objExtra(strFirst))
            strToss = vNibble(0)
            strChars = vNibble(1)
            strPrev = strFirst
            strDecrypt = vbNullString
            Do While strChars <> vbNullString:
                vDecode = objEncoding(Len(strDecrypt) Mod objEncoding.Count)
                vNibble = Nibble(strChars, (UBound(vDecode) - LBound(vDecode) + 1))
                strNibble = vNibble(0)
                strChars = vNibble(1)
                Set objGaps = CreateObject("Scripting.Dictionary")
                For intCounter = 1 To Len(strNibble):
                    strChar1 = strPrev
                    strChar2 = Mid(strNibble, intCounter, 1)
                    intDiff = objAlpha_Num(strChar2) - objAlpha_Num(strChar1)
                    intLen = UBound(vNum_Alpha) - LBound(vNum_Alpha) + 1
                    intGap = (objAlpha_Num(strChar2) - objAlpha_Num(strChar1) Mod intLen) - 1
                    If intDiff < 0 Then
                        intGap = intGap + intLen
                    End If
                    strPrev = strChar2
                    objGaps.Add objGaps.Count, intGap
                Next intCounter
                strDecrypt = strDecrypt & Gap_Decode(objGaps, vDecode)
            Loop
            juniper_decrypt = strDecrypt
        Else
            juniper_decrypt = CVErr(xlErrValue)
        End If
        Exit Function
        
    juniper_decrypt_error_handler:
        MsgBox Err.Description
    End Function
    
    Function Nibble(strCref, intLength) As Variant
        Dim strNib As String
        Dim strRest As String
    
        If Len(strCref) < intLength Then
            Err.Raise Number:=1 + vbObjectError, Description:="Ran out of characters: hit " & strCref & ", expecting " & intLength & " chars."
        Else
            strNib = Left(strCref, intLength)
            strRest = Right(strCref, Len(strCref) - intLength)
            Nibble = Array(strNib, strRest)
        End If
    End Function
    
    Function Gap_Decode(objGaps As Object, vDec As Variant) As String
        Dim num As Integer
        Dim intIndex As Integer
    
        If objGaps.Count <> (UBound(vDec) - LBound(vDec) + 1) Then
            Err.Raise Number:=2 + vbObjectError, Description:="Nibble and decode size not the same!"
        Else
            For intIndex = LBound(vDec) To UBound(vDec)
                num = num + objGaps(intIndex) * vDec(intIndex)
            Next intIndex
            Gap_Decode = Chr(num Mod 256)
        End If
    End Function
    I'll have a look again later, to see if I can improve something. I have uploaded the file to github repo at https://github.com/boyank/juniper_decrypt. I don't have any $9$ password so I used one from https://www.m00nie.com/juniper-type-9-password-tool/
    test_password: $9$DMk5Ftu1rK80OvLXxdVHq.fz6B1heK80ORSeW-dUjH
    output: www.m00nie.com
    Last edited by buran; 08-26-2016 at 07:23 AM. Reason: add github repo

  7. #7
    Forum Contributor Rhudi's Avatar
    Join Date
    03-08-2013
    Location
    South Carolina, US
    MS-Off Ver
    Professional Plus 2016 aka Office 365
    Posts
    201

    Re: Porting Python code to VB?

    Thank you so much.

  8. #8
    Forum Contributor Rhudi's Avatar
    Join Date
    03-08-2013
    Location
    South Carolina, US
    MS-Off Ver
    Professional Plus 2016 aka Office 365
    Posts
    201

    Re: Porting Python code to VB?

    This is tested and works exactly as hoped. I have my own little OCD Style quirks. So, following is what I did to it.

    You taught me an awesome trick in this. Loading an array of arrays. *boggle* I didn't know you could do that.

    I still have no idea how this works the Vigenère cipher, but it sure does!
    Interesting links:
    http://www.counton.org/explorer/code...ere-cipher.php
    https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher

    Function juniper_decrypt$(strPassword$)
    '---------------------------------------------------------------------------------------
    ' Module    : juniper_decrypt - v1.0
    ' Author    : Boyan Kolev ( 2016-08-26 )
    ' Github    : https://github.com/boyank/juniper_decrypt
    ' Purpose   : Decrypt Juniper $9$ Type password. Ported to VBA from Python https://github.com/mhite/junosdecode
    '---------------------------------------------------------------------------------------
    '
    ' 2016-08-26 - Style Changes
    '
    Dim intCounter%, intGap%, intLen%, intDiff%
    Dim strChars$, strFirst$, strToss$, strPrev$, strDecrypt$, strNibble$, strChar1$, strChar2$
    Dim vChar As Variant
    Dim vFamily As Variant
    Dim vNum_Alpha As Variant
    Dim vNibble As Variant
    Dim vDecode As Variant
    Dim objEncoding As Object
    Dim objAlpha_Num As Object
    Dim objExtra As Object
    Dim objGaps As Object
    '
        On Error GoTo juniper_decrypt_error_handler
    '
        vNum_Alpha = Array("Q", "z", "F", "3", "n", "6", "/", "9", "C", "A", "t", "p", "u", "0", "O", _
                           "B", "1", "I", "R", "E", "h", "c", "S", "y", "r", "l", "e", "K", "v", "M", "W", "8", "L", "X", "x", _
                           "7", "N", "-", "d", "V", "b", "w", "s", "Y", "2", "g", "4", "o", "a", "J", "Z", "G", "U", "D", "j", _
                           "i", "H", "k", "q", ".", "m", "P", "f", "5", "T")
    '
        vFamily = Array(Array("Q", "z", "F", "3", "n", "6", "/", "9", "C", "A", "t", "p", "u", "0", "O"), _
                        Array("B", "1", "I", "R", "E", "h", "c", "S", "y", "r", "l", "e", "K", "v", "M", "W", "8", "L", "X", "x"), _
                        Array("7", "N", "-", "d", "V", "b", "w", "s", "Y", "2", "g", "4", "o", "a", "J", "Z", "G", "U", "D", "j"), _
                        Array("i", "H", "k", "q", ".", "m", "P", "f", "5", "T"))
    '
        Set objEncoding = CreateObject("Scripting.Dictionary")
        objEncoding.Add objEncoding.Count, Array(1, 4, 32)
        objEncoding.Add objEncoding.Count, Array(1, 16, 32)
        objEncoding.Add objEncoding.Count, Array(1, 8, 32)
        objEncoding.Add objEncoding.Count, Array(1, 64)
        objEncoding.Add objEncoding.Count, Array(1, 32)
        objEncoding.Add objEncoding.Count, Array(1, 4, 16, 128)
        objEncoding.Add objEncoding.Count, Array(1, 32, 64)
    '
        Set objAlpha_Num = CreateObject("Scripting.Dictionary")
        For intCounter = LBound(vNum_Alpha) To UBound(vNum_Alpha):
            objAlpha_Num.Add vNum_Alpha(intCounter), intCounter
        Next intCounter
    '
        Set objExtra = CreateObject("Scripting.Dictionary")
        For intCounter = LBound(vFamily) To UBound(vFamily)
            For Each vChar In vFamily(intCounter):
                objExtra.Add vChar, 3 - intCounter
            Next vChar
        Next intCounter
    '
        Select Case Left(strPassword, 3)
            Case "$9$"
                strChars = Right(strPassword, Len(strPassword) - 3)
                vNibble = Nibble(strChars, 1)
                strFirst = vNibble(0)
                strChars = vNibble(1)
                vNibble = Nibble(strChars, objExtra(strFirst))
                strToss = vNibble(0)
                strChars = vNibble(1)
                strPrev = strFirst
                strDecrypt = vbNullString
                Do While Not strChars = vbNullString:
                    vDecode = objEncoding(Len(strDecrypt) Mod objEncoding.Count)
                    vNibble = Nibble(strChars, (UBound(vDecode) - LBound(vDecode) + 1))
                    strNibble = vNibble(0)
                    strChars = vNibble(1)
                    Set objGaps = CreateObject("Scripting.Dictionary")
                    For intCounter = 1 To Len(strNibble):
                        strChar1 = strPrev
                        strChar2 = Mid(strNibble, intCounter, 1)
                        intDiff = objAlpha_Num(strChar2) - objAlpha_Num(strChar1)
                        intLen = UBound(vNum_Alpha) - LBound(vNum_Alpha) + 1
                        intGap = (objAlpha_Num(strChar2) - objAlpha_Num(strChar1) Mod intLen) - 1
                        If intDiff < 0 Then
                            intGap = intGap + intLen
                        End If
                        strPrev = strChar2
                        objGaps.Add objGaps.Count, intGap
                    Next intCounter
                    strDecrypt = strDecrypt & Gap_Decode(objGaps, vDecode)
                Loop
                juniper_decrypt = strDecrypt
            Case Else
                juniper_decrypt = CVErr(xlErrValue)
        End Select
        Exit Function
    '
    juniper_decrypt_error_handler:
        MsgBox Err.Description
    End Function
    
    Function Nibble(strCref, intLength) As Variant
    Dim strNib$, strRest$
    '
        Select Case Len(strCref)
            Case Is < intLength
                Err.Raise Number:=1 + vbObjectError, Description:="Ran out of characters: hit " & strCref & ", expecting " & intLength & " chars."
            Case Else
                strNib = Left(strCref, intLength)
                strRest = Right(strCref, Len(strCref) - intLength)
                Nibble = Array(strNib, strRest)
        End Select
    End Function
    
    Function Gap_Decode$(objGaps As Object, vDec As Variant)
    Dim num%, intIndex%
    '
        Select Case objGaps.Count
            Case (UBound(vDec) - LBound(vDec) + 1)
                For intIndex = LBound(vDec) To UBound(vDec)
                    num = num + objGaps(intIndex) * vDec(intIndex)
                Next intIndex
                Gap_Decode = Chr(num Mod 256)
            Case Else
                Err.Raise Number:=2 + vbObjectError, Description:="Nibble and decode size not the same!"
        End Select
    End Function

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. python in excel
    By idandush in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-05-2015, 04:26 PM
  2. Can you use (or compile?) Python code in Excel
    By mrvp in forum The Water Cooler
    Replies: 7
    Last Post: 06-08-2014, 06:49 AM
  3. [SOLVED] Porting Data Using s Sumif
    By Aussie1 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-18-2013, 07:09 PM
  4. call a python script from excel vba
    By sheffieldlad in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-28-2013, 07:11 PM
  5. Python commands in Excel
    By ljoseph in forum Excel General
    Replies: 0
    Last Post: 09-18-2009, 07:02 AM
  6. Excel and Python
    By davidlawrence in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-24-2008, 08:42 AM
  7. [SOLVED] Porting Modules
    By Mel Monroe in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-18-2005, 02:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1