Hi,
You can execute scripts on your Virtual Machine even if you cannot login to it, since the Azure add an agent in the VM by default. Using the script you can enable ssh, uninstall the ufw or do whatever you want.
Any script you execute from the portal (as the following explanation) is executed as root/administrator.
How to execute script on the VM when you cannot login :
If you are using windows VM then:
Logon to the Azure portal-> select the VM -> on the left menu select Run command -> select RunPowerShellScript
If you this is Ubuntu VM then:
Logon to the Azure portal -> VM Name -> Extensions -> Add -> Select "Custom Script for Linux" > Create
Uncomplicated Firewall (UFW) uses a command-line interface which mean that using the above option you can run and control UFW using the script you execute even if you cannot login the VM