Technology and Security

Month: March 2022

Send email from Spring Boot using Microsoft 365

Send Email using Spring Boot

This article is documents how to send email using Spring Boot over Microsoft 365 (formerly Office 365). Spring provides an easy to use interface called JavaMailSender that wraps the native JavaMail API. This example demonstrates sending both plain and HTML messages, as well as adding attachments.

Microsoft 365

Microsoft 365 subscriptions come with Outlook email access via browser or by native Office client applications. The Microsoft email solution (Outlook and Exchange) is ubiquitous for business users; It also offers the ability to connect from external clients for both sending and receiving messages. We will leverage this capability to send messages from a custom Java application.

JavaMailSender

Spring Boot provides this extended interface for JavaMail that supports MIME messages and completely handles session management. Messages containing attachments typically use this interface in conjunction with the MimeMessageHelper class.

Spring Boot Properties

Spring Boot pulls the mail settings from your default application.properties file (or YAML depending on your local setup). First set the properties to reference a Microsoft 365 (formerly Office 365) SMTP email host and enable security. Our GitHub repository should not contain sensitive property values, so externalize those variables.

// application.properties

spring.mail.host=smtp.office365.com
spring.mail.port=587
spring.mail.properties.mail.smtp.port=587
spring.mail.properties.mail.transport.protocol=smtps
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.ssl.enable=false
spring.mail.properties.mail.smtp.timeout=15000
spring.mail.properties.mail.smtp.connectiontimeout=15000
spring.mail.properties.mail.smtp.socketFactory.fallback=true
spring.mail.properties.mail.mail.debug=true

spring.config.import=sensitive.properties

Next add a spring.config.import statement to import sensitive.properties file, then provide the alternate file.

// sensitive.properties

spring.mail.username=my-user@mydomain.com
spring.mail.password=my-secure-password
prototype.o365.email.from=webmaster@mydomain.com
prototype.o365.email.to=destination-user@your-domain.com

Finally, to prevent sensitive.properties from finding its way to GitHub, be sure to update the .gitignore file to exclude it.

# .gitignore

### SENSITIVE PROPERTIES ###
sensitive*.properties

Send Email using Spring Boot

The final step is to craft and send your email. Use a SimpleMailMessage to send a plaintext email:

SimpleMailMessage msg = new SimpleMailMessage();
msg.setTo(emailTo);
msg.setFrom(emailFrom);
msg.setSubject("plain email at " + new Date().toString());
msg.setText("Hello World\n" + new Date().toString());

try
{
    javaMailSender.send(msg);
}
catch(MailException e)
{
    log.error("error sending plaintext email", e);
}

Use a MIME message to send HTML email content or include attachments:

MimeMessage msg = javaMailSender.createMimeMessage();
try
{
    MimeMessageHelper helper = 
        new MimeMessageHelper(msg, true);

    helper.setTo(emailTo);
    helper.setFrom(emailFrom);
    helper.setSubject("html email at " + new Date().toString());
    helper.setText("<h1>check attachment for the logo</h1>", true);

    helper.addAttachment("logo.png", 
        new ClassPathResource("logo.png"));
}
catch(MessagingException e)
{
    log.error("error preparing email", e);
}

try
{
    javaMailSender.send(msg);
}
catch(MailException e)
{
    log.error("error sending mime email", e);
}

Source Code

This article’s full source code is available on GitHub.

Installing Ubuntu on Hyper-V

Installing Ubuntu on Hyper-V

This article will look at the steps required for installing Ubuntu on Hyper-V. We will focus on a vanilla build using Ubuntu LTS 20.04. This virtual machine could be used for a variety of purposes as-is. In later articles we will build on this simple foundation to create an open source intelligence (osint) platform for performing research on the internet with privacy. If you are interested in cybersecurity, you could follow this article to build a Kali Linux attack machine and use Ubuntu as the target.

Installation Overview

Installing Ubuntu on a Windows 10 workstation using Hyper-V involves the following steps:

  • Download the Ubuntu ISO image
  • Configure a new Hyper-V Virtual Machine
  • Install and Configure Ubuntu Operating System
  • Login to the Ubuntu Linux VM

Next we will download the operating system.

Downloading Ubuntu 20.04 LTS

Ubuntu can be downloaded from here. For this article we will be using the most recent long-term support (LTS) version available. After installation we will update and upgrade to apply the most recent security patches.

Ubuntu 20.04 LTS Requirements
Ubuntu 20.04 LTS Requirements

Configure a new Hyper-V Virtual Machine

If you have not done so already, please follow the Microsoft documentation for enabling Hyper-V. Run the Hyper-V application, then right-click your workstation and select “New | Virtual Machine.”

create new hyper-v vm
Creating a new Hyper-V VM

You must complete several screens of options in order to configure the VM. Several steps worth noting include:

  • Specify Name and Location
  • Specify Generation: 2
  • Assign Memory: 4096MB
  • Configure Networking: Default Switch
  • Connect Virtual Hard Disk: 25GB
  • Installation Options: configure to use the downloaded ISO image

Finally press “Finish” on the summary page to create the virtual machine.

Disable Secure Boot Option

Since we selected a second generation vm in the last section, there is one more step to complete. Open the vm settings, and uncheck the “Enable Secure Boot” option under Security. Without completing this step, the Linux OS will not boot from the ISO.

Virtual Machine Settings
Virtual Machine Settings
Virtual Machine Disable Secure Boot
Virtual Machine Disable Secure Boot

Secure Boot is a feature available with generation 2 virtual machines that helps prevent unauthorized firmware, operating systems, or Unified Extensible Firmware Interface (UEFI) drivers (also known as option ROMs) from running at boot time. Secure Boot is enabled by default. 

microsoft

Install and Configure Ubuntu on Hyper-V

Right-click on the newly created VM and select Connect followed by Start. Select the option for “Install Ubuntu“.

Install Ubuntu
Install Ubuntu

There are several configuration screens that follow. Fortunately most steps can use default values on your own preferences. We will show the non-default values below.

For updates and other software, choose either a normal or minimal installation. Either will work, but a minimum will allow you to install only the features you need. Also be sure to check the box for installing third-party software.

Updates and Other Software
Updates and Other Software

For the “Who are you” page, be sure to select a username and strong password. We entered “osint” across the board because our vm was disposable. This simple scheme is not recommended for a long-living virtual machine.

Who are you
Who are you

After completing the setup pages, you will be prompted to restart the machine.

Reboot to Complete Installation
Reboot to Complete Installation

Configure Personal Settings

Previously you rebooted the vm and are back at a login screen. Finish out your personal settings to complete your installation of ubuntu on Hyper-V. Your settings will depend entirely on how you intend to use the vm. If you are using this as a personal workstation, feel free to configure the settings for your accounts. Since we will be updating this virtual machine in future blog articles, we will not be including any personal information.

Connect Online Accounts
Connect Online Accounts
Skip Live Patch Setup
Skip Live Patch Setup
Help Improve Ubuntu
Help Improve Ubuntu
Privacy
Privacy
Ready To Go
Ready To Go
building a windows 10 vm in hyper-v

Building a Windows 10 VM on Hyper-V

This article will look at the steps required for building a Windows 10 VM on Hyper-V. Love it or hate it, at some point you will find yourself needing a Windows 10 workstation. Maybe to use as a cybersecurity target, testing a new software install, or for installing business software that will not run on your favorite Linux distro. If this VM will used as a target, see our instructions on building a Kali Linux attack machine in this article on our blog.

Downloading a Windows ISO

Microsoft publishes free Windows virtual machines for testing browser compatibility. Where the intent is to offer browsers for testing, the reality is that they can be repurposed to any need. The caveat is that they are 90 day OS licenses, so don’t plan on maintaining a long-term project on these ISOs. The variants can be downloaded directly from Microsoft here.

Windows VM Downloads
Windows VM Downloads

Installing a Windows 10 VM on Hyper-V

After downloading your preferred distro in the last section, you are now ready to build the virtual machine. First start by opening the Hyper-V Manager and choosing the option to import a virtual machine.

Import a VM in Hyper-V
Import a VM in Hyper-V

Use the following settings to start the wizard:

  • Locate Folder: this will usually be your downloads folder
  • Select Virtual Machine: this will only have one option

The next few steps are important to making a functional vm. For the “Import Type” you should select the “Copy” option.

Copy the Virtual Machine
Copy the Virtual Machine

The next two pages of options deal with the vm destination. Mileage may vary based on your host, but most users tend to install their virtual machines on a larger secondary drive.

Windows VM Destination
Windows VM Destination
Virtual Hard Disk Location
Virtual Hard Disk Location

Finally click “Finish” on the Summary tab to kick-off the installation.

Logging In

After the virtual machine has completed installation, you can select the “Connect To” and “Start” options from the Hyper-V Manager to launch Windows. This will bring you to the login screen which requires a password.

Windows 10 Login
Windows 10 Login

The password is predefined in the ISO and displayed on the original download page from Microsoft. At this time of writing this blog article, the default password to your new virtual machine is “Passw0rd!”

As a final step, is always good to make sure networking is functioning properly. After a quick ping test, you are good to go…

Windows 10 Ping Test
Windows 10 Ping Test

Powered by WordPress & Theme by Anders Norén