In light of recent management console attacks (ie: codespaces) I wanted to detail out some steps to enable two factor authentication on the azure management portal.
First off all, there are 2 types of accounts @ Microsoft. Organizational ones, and personal ones. For example, my MSDN account is registered under an email address that actually belongs to the "personal" side, even though it is a corporate account.
In order to enable two factor authentication on a personal account, visit this link and sign in. Under the Security & Password tab, you will see the following section:
As you can tell from the screenshot above, I have already enabled two factor authentication. You will see a message in that space that asks you to enable two factor authentication. Follow the steps. On Andriod devices (or "other" in Microsoft vernacular) it ties in with the authenticator app which is the same app used for google services.
Organizational accounts are different beasts, and tied to the Windows Azure Active Directory (WAAD). Inside my MSDN account, I have setup a default WAAD. I have also tied that to my personal domain (shamirc.com). I can now create users inside that WAAD instance and assign them different roles for the AD. I can also add them as an administrator on my subscription. Pretty neat!
The first thing you need to do for organizational accounts such as the one described above is setup a multi-factor authentication provider. Details can be found here. After this is complete, you need to enable multi-factor authentication for a particular user inside the directory.
As you can see on the waaaay bottom of this screenshot, after I've entered my directory, there is a "manage multi-factor authentication" button. Click on that an you will be taken to a screen where you can enable MFA for selected users. Find the user(s) that you want, and click enable.
The next time the user logs into any Microsoft site, they will see the following after they type in their password.
Follow the instructions and you are now all setup!
For reference, the official MS docs can be found here.
Monday, June 23, 2014
Thursday, May 8, 2014
Hacky In Memory User Store for ASP.NET Identity 2.0
There are cases when you want to build out a sample ASP.NET Web project (MVC in this case) just to test something out. I personally hate the defaults that come with most .net mvc projects. It forces you to create a database, initialize that database, and other useless steps. Furthermore, I hate working from the predefined templates that come with MVC. I like that you can get a working app quickly, but it really doesn't help you understand exactly what is going on.
In previous versions of MVC, you would just create a custom membership provider, override a few methods, register it in the web.config and you were off. Identity 2.0 changed all of that.
In this post I will try and outline what steps I took to create an in memory user store with very limited functionality. Bear in mind the goal of this article is to get you going in the right direction, and is probably not a complete solution.
The first thing you are going to want to do is read a couple of MS articles to get a good base line.
Adding Identity 2.0 to an empty project
Overview of Custom Storage Providers for ASP.NET Identity
There are a couple of key components here. The first in managers. There are built in managers, which are easy enough to use. The problem is that there is no source code released for the Microsoft.AspNet.Identity, so it is hard to see exactly what the built in user manager does. It obviously has to interface with the authorization/authentication framework in MVC, but little detail on that has been released.
The second component is the "stores". Instead of having one big store (or two as in MVC4) they have broken up the stores into various interfaces. Each store adds a certain amount of functionality to the user store. It is understood that the manager knows what type of "store" it has received and which interfaces are implemented. Based on these interfaces, different methods are called and different functionality is available.
The first step in this process is to create an user object. At a minimum, this user object must implement IUser which only requires a string id and string password.
In the implementation above, I am adding hashedPassword to the object. You can add any fields you like. This is probably a hack and probably not the right place to add it, but I am just trying to get something up quickly.
After the user object is created, you need to create a store. Depending on the features you want to implement, you will implement different interfaces. See the links above for more.
As you can see from above, I only implemented a few of the methods. I'm not worried about registration, or edit functionality. I just need to create an account to login with that plugs in with the built in authentication and authorization frameworks. As you can see from above, I initialize the user in the constructor.
After this is done, you can just initialize the user manager and pass it the store that you have created.
After this, you have to actually log the user in using the OWIN providers. You can see samples of this on the internet or if you create a MVC app template with authentication enabled.
Hope that helps!
In previous versions of MVC, you would just create a custom membership provider, override a few methods, register it in the web.config and you were off. Identity 2.0 changed all of that.
In this post I will try and outline what steps I took to create an in memory user store with very limited functionality. Bear in mind the goal of this article is to get you going in the right direction, and is probably not a complete solution.
The first thing you are going to want to do is read a couple of MS articles to get a good base line.
Adding Identity 2.0 to an empty project
Overview of Custom Storage Providers for ASP.NET Identity
There are a couple of key components here. The first in managers. There are built in managers, which are easy enough to use. The problem is that there is no source code released for the Microsoft.AspNet.Identity, so it is hard to see exactly what the built in user manager does. It obviously has to interface with the authorization/authentication framework in MVC, but little detail on that has been released.
The second component is the "stores". Instead of having one big store (or two as in MVC4) they have broken up the stores into various interfaces. Each store adds a certain amount of functionality to the user store. It is understood that the manager knows what type of "store" it has received and which interfaces are implemented. Based on these interfaces, different methods are called and different functionality is available.
The first step in this process is to create an user object. At a minimum, this user object must implement IUser which only requires a string id and string password.
public class IdentityUser : IUser
{
public IdentityUser(string id, string userName, string hashedPassword)
{
Id = id;
UserName = userName;
this.hashedPassword = hashedPassword;
}
public IdentityUser(string userName)
{
UserName = userName;
}
public string Id { get; private set; }
public string UserName { get; set; }
public string hashedPassword { get; set; }
}
In the implementation above, I am adding hashedPassword to the object. You can add any fields you like. This is probably a hack and probably not the right place to add it, but I am just trying to get something up quickly.
After the user object is created, you need to create a store. Depending on the features you want to implement, you will implement different interfaces. See the links above for more.
public class InMemoryUserStore : IUserStore<identityuser>, IUserPasswordStore<identityuser>
{
private IList<identityuser> userList = new List<identityuser>();
public InMemoryUserStore()
{
userList.Add(new IdentityUser("1", "admin", Crypto.HashPassword("admin")));
}
public void Dispose()
{
}
public Task CreateAsync(IdentityUser user)
{
throw new NotImplementedException();
}
public Task UpdateAsync(IdentityUser user)
{
throw new NotImplementedException();
}
public Task DeleteAsync(IdentityUser user)
{
throw new NotImplementedException();
}
public Task<identityuser> FindByIdAsync(string userId)
{
return Task.FromResult(userList.FirstOrDefault(x => x.Id.Equals(userId)));
}
public Task<identityuser> FindByNameAsync(string userName)
{
return Task.FromResult(userList.FirstOrDefault(x => x.UserName.Equals(userName)));
}
public Task SetPasswordHashAsync(IdentityUser user, string passwordHash)
{
throw new NotImplementedException();
}
public Task<string> GetPasswordHashAsync(IdentityUser user)
{
return Task.FromResult(userList.First(x => x.UserName.Equals(user.UserName)).hashedPassword);
}
public Task<bool> HasPasswordAsync(IdentityUser user)
{
var appUser = userList.FirstOrDefault(x => x.UserName.Equals(user.UserName));
if (appUser == null)
{
return Task.FromResult(false);
}
return Task.FromResult(!String.IsNullOrEmpty(appUser.hashedPassword));
}
}
As you can see from above, I only implemented a few of the methods. I'm not worried about registration, or edit functionality. I just need to create an account to login with that plugs in with the built in authentication and authorization frameworks. As you can see from above, I initialize the user in the constructor.
After this is done, you can just initialize the user manager and pass it the store that you have created.
[Authorize]
public class BaseController: Controller
{
public BaseController() : this(new UserManager<identityuser>(new InMemoryUserStore()))
{
}
private BaseController(UserManager<identityuser> userManager)
{
UserManager = userManager;
}
public UserManager<identityuser> UserManager { get; private set; }
protected IAuthenticationManager AuthenticationManager
{
get { return HttpContext.GetOwinContext().Authentication; }
}
}
After this, you have to actually log the user in using the OWIN providers. You can see samples of this on the internet or if you create a MVC app template with authentication enabled.
Hope that helps!
Tuesday, April 22, 2014
AppSensor and MVC: Unexpected HTTP Commands
So, I have decided to try and implement the some of the OWASP AppSensor controls inside an MVC project. I will go into details about the end goal of this project in another post, but for now I am going to talk about the Unexpected HTTP Commands detection point.
This is a first stab. Currently the only response is to log the request in the logs.
In MVC, the current set of http method controls are implemented as ActionMethodSelectorAttribute. What this means is that you apply the control on a method by method basis. This approach will probably be good when implementing other controls, but for this one, we need something more global.
This article has a really good explanation of action filters and how to use them. The filter itself is quite simple, since all we are doing is comparing the reported http verb to a list of expected ones.
A couple of notes.
1) filterContext.HttpContext.Request.GetHttpMethodOverride() returns the http method as listed in the request. Remember that this is untrusted code and should be sanitized before writing to the logs.
2) Verbs can actually be restricted earlier in the pipeline. Firstly, you can use Request Filtering to limit the accepted verbs. This method works, but doesn't allow the application to handle the request and thus limit our ability to build in the response mechanisms. Secondly, in the handler definition you can define the list of verbs to respond. If you try to use a verb that isn't on this list, you will get an HTTP 405 error. Both these mechanism are great (and log to the IIS logs) but they don't allow our application to handle and keep track of violations. To bypass the handler definition, you can use * in the accepted verbs list.
The code is located in github.
This is a first stab. Currently the only response is to log the request in the logs.
In MVC, the current set of http method controls are implemented as ActionMethodSelectorAttribute. What this means is that you apply the control on a method by method basis. This approach will probably be good when implementing other controls, but for this one, we need something more global.
This article has a really good explanation of action filters and how to use them. The filter itself is quite simple, since all we are doing is comparing the reported http verb to a list of expected ones.
public void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!Enabled)
{
return;
}
var httpMethod = filterContext.HttpContext.Request.GetHttpMethodOverride();
if (
acceptedVerbs.Any(
x =>
x.Equals(httpMethod,
StringComparison.InvariantCultureIgnoreCase)))
{
return;
}
httpMethod = SanitizeHttpMethod(httpMethod);
Logger.LogRequestException(filterContext.HttpContext.Request.Url.AbsolutePath,
SecurityConstants.UnexpectedHttpCommands,
httpMethod,
filterContext.HttpContext.Request.UserHostAddress);
filterContext.Result = new HttpNotFoundResult();
}
A couple of notes.
1) filterContext.HttpContext.Request.GetHttpMethodOverride() returns the http method as listed in the request. Remember that this is untrusted code and should be sanitized before writing to the logs.
2) Verbs can actually be restricted earlier in the pipeline. Firstly, you can use Request Filtering to limit the accepted verbs. This method works, but doesn't allow the application to handle the request and thus limit our ability to build in the response mechanisms. Secondly, in the handler definition you can define the list of verbs to respond. If you try to use a verb that isn't on this list, you will get an HTTP 405 error. Both these mechanism are great (and log to the IIS logs) but they don't allow our application to handle and keep track of violations. To bypass the handler definition, you can use * in the accepted verbs list.
The code is located in github.
Subscribe to:
Posts (Atom)


