44from pathlib import Path
55from hashlib import sha512
66
7+ from pysqlrecon .lib .exceptions import DuplicateAssemblyError
78from pysqlrecon .logger import logger
89from pysqlrecon .lib import PySqlRecon
910
1718@app .callback (invoke_without_command = True )
1819def main (
1920 ctx : typer .Context ,
20- dll : Path = typer .Option (None , "--dll" , dir_okay = False , readable = True , help = ".NET DLL to load into stored procedure" ),
21- function : str = typer .Option (None , "--function" , help = "Function within .NET DLL to execute" )):
21+ dll : Path = typer .Option (... , "--dll" , dir_okay = False , readable = True , help = ".NET DLL to load into stored procedure" ),
22+ function : str = typer .Option (... , "--function" , help = "Function within .NET DLL to execute" )):
2223
2324 pysqlrecon : PySqlRecon = ctx .obj ['pysqlrecon' ]
2425
@@ -86,7 +87,8 @@ def main(
8687
8788 #####
8889 # Create the custom assembly
89- pysqlrecon .create_asm (asm_name , dll_bytes )
90+ create_assembly (pysqlrecon , asm_name , dll_bytes , function )
91+
9092 if not pysqlrecon .check_assembly (asm_name ):
9193 logger .error ("Failed to create custom assembly" )
9294 logger .info ("Cleaning up..." )
@@ -122,4 +124,21 @@ def main(
122124 pysqlrecon .delete_tasm_resources (asm_name , function )
123125
124126 pysqlrecon .disconnect ()
125-
127+
128+
129+ # recursive func to create assembly and handle duplicate assmebly error by deleting and re-creating
130+ # fix for https://github.com/Tw1sm/PySQLRecon/issues/1
131+ def create_assembly (pysqlrecon , asm_name , dll_bytes , function ):
132+ try :
133+ pysqlrecon .create_asm (asm_name , dll_bytes )
134+
135+ except DuplicateAssemblyError as e :
136+ logger .warning ("Duplicate assembly detected - will try to delete and re-create" )
137+
138+ pysqlrecon .delete_tasm_resources (e .assembly_name , function )
139+ logger .info (f"Deleted the offending duplicate assembly '{ e .assembly_name } '" )
140+
141+ logger .info (f"Attempting to re-create assembly with name '{ asm_name } '" )
142+ pysqlrecon .create_asm (asm_name , dll_bytes )
143+
144+
0 commit comments