asp .net core: preventing attacks 2 - contentful request contains onlythe headers set automatically...

82
ASP .NET Core: Preventing attacks 2.0 Mikhail Shcherbakov Independent Developer and Consultant

Upload: doanhuong

Post on 10-Apr-2018

245 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

ASP.NETCore:Preventingattacks2.0

MikhailShcherbakovIndependentDeveloperandConsultant

Page 2: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

WhoamI

§ IndependentDeveloperandConsultant§ Co-organizerof.NETmeetupshttp://dotnet.ru§ PublicSpeakeratDotNext,DotNetConf,ITGM, .NETmeetups§ FormerProductManageratCezurity,R&DDeveloperatPositiveTechnologiesandTeamLeadatAcronis,Luxoft,Boeing

#2

Page 3: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Whatyoucanexpect

§ PreventingOpenRedirect§ DataProtection§ PreventingXSS§ SettingupCSP§ Anti-RequestForgery§ SettingupCORS§UsingCookies

#3

Page 4: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

MicrosoftBugBountyProgram

https://aka.ms/corebounty#4

Page 5: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

PreventingOpenRedirect

Page 6: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values
Page 7: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Howtoprevent

public async Task<IActionResult> Login(LoginViewModel model, string returnUrl)

{// ...return LocalRedirect(returnUrl);

}

#7

Page 8: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Howtoprevent

private IActionResult RedirectToLocal(string returnUrl){

if (Url.IsLocalUrl(returnUrl))return Redirect(returnUrl);

elsereturn RedirectToAction(

nameof(HomeController.Index), "Home");}

#8

Page 9: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

OpenRedirectAttack

https://github.com/OWASP/Top10/tree/master/2017/datacall#9

Page 10: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

https://technet.microsoft.com/library/security/4021279#10

Page 11: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

DataProtection

Page 12: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Overview

§Nomachinekeys§Highlevelcryptographyout-of-the-box§ Keystoresout-of-the-box§ Abilitytoextendkeystores§ Supportskeyrotationautomatically§ Providesisolationautomatically§ Customalgorithmssupported

#12

Page 13: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Protect/Unprotect

public class HomeController : Controller{

private readonly IDataProtector protector;

public HomeController(IDataProtectionProvider provider){

protector = provider.CreateProtector("isolation-purpose");}

public IActionResult Index(string input){

var protectedPayload = protector.Protect(input);var unprotectedPayload = protector.Unprotect(protectedPayload);return View();

}}

#13

Page 14: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Protect/Unprotect

public IActionResult Index(string input){

var timeLimitedProtector = protector.ToTimeLimitedDataProtector();

var protectedData = timeLimitedProtector.Protect(input,lifetime: TimeSpan.FromMinutes(30));

return View();}

#14

Page 15: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

PasswordHashingpublic void StorePassword(SecureString password){

var salt = new byte[128 / 8];using (var rng = RandomNumberGenerator.Create()){

rng.GetBytes(salt);}

var hash = Convert.ToBase64String(KeyDerivation.Pbkdf2(password: password,salt: salt,prf: KeyDerivationPrf.HMACSHA512,iterationCount: 10000,numBytesRequested: 256 / 8));

// store salt and hash to DB...}

#15

Page 16: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Configuration

public void ConfigureServices(IServiceCollection services){

services.AddDataProtection().SetApplicationName("isolation-name").SetDefaultKeyLifetime(TimeSpan.FromDays(14)).PersistKeysToFileSystem(new DirectoryInfo(@"\\server\share\")).ProtectKeysWithDpapi().UseCryptographicAlgorithms(new AuthenticatedEncryptionSettings{

EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC,ValidationAlgorithm = ValidationAlgorithm.HMACSHA256

});}

#16

Page 17: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Configuration

§HKLM\SOFTWARE\Microsoft\DotNetPackages\Microsoft.AspNetCore.DataProtection§ EncryptionType§ DefaultKeyLifetime§ KeyEscrowSinks

Page 18: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Details

§ ThedefaultpayloadprotectionalgorithmusedisAES-256-CBC forconfidentialityandHMACSHA256 forauthenticity.A512-bitmasterkey,rolledevery90days

§ Protectedpayloadformat§ 32-bitmagicheader§ 128-bitkeyid§ thepartofspecifictotheencryptor

#17

Page 19: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

PreventingXSS

Page 20: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Same-originpolicy(SOP)

#19

var invocation = new XMLHttpRequest();var url ='http://bar.other/resources/public-data/';

function callOtherDomain() {if(invocation) {invocation.open('GET', url, true);invocation.onreadystatechange =

handler;invocation.send();

}}

https://foo.example

http://bar.other

{"id": 2,"name": " Rubber duck","price": 12.50

},{"id": 3,"name": "Giant Unicorn","price": 25.50

Page 21: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Same-originpolicy(SOP)

#20

var invocation = new XMLHttpRequest();var url ='https://mail.google.com/mail/u/0/';

function callOtherDomain() {if(invocation) {invocation.open('GET', url, true);invocation.onreadystatechange =

handler;invocation.send();

}}

https://evil.com

https://mail.google.com/

Page 22: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Same-originpolicy(SOP)

Thesame-originpolicy(SOP)restricts howadocumentorscriptloadedfromoneorigin caninteract witharesourcefromanotherorigin.Itisacriticalsecuritymechanismforisolatingpotentiallymaliciousdocuments.

#21

https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy

Page 23: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Definitionofanorigin

§URIscheme(http)§Hostname(my-domain.com)§ Portnumber(80)

#22

Page 24: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Cross-SiteScripting(XSS)

Cross-SiteScripting(XSS)attacksareatypeofinjection,inwhichmaliciousscripts areinjectedintootherwisebenignandtrustedwebsites.

https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)#23

Page 25: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Cross-SiteScripting(XSS)

§ TheXSSisthemostpopularattacktowebapplications§ https://github.com/OWASP/Top10/tree/master/2017/datacall

§ Thisisagoodentrypointforotherattackswithmoreimpact§Onlysomepeopleknowandcorrectlyusebuilt-inXSSpreventionmechanisms

#24

Page 26: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

PotentialXSS

<script src="@ViewData["UntrustedInput"]"></script>

#25

Page 27: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

PotentialXSS

<script>@ViewData["UntrustedInput"];

</script>

...

<img src="@ViewData["UntrustedInput"];">

#26

Page 28: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Howtoprevent

§ Inputdatavalidationandfiltering§OutputsanitizationforHTML,JavaScript,XML,SVGusingsanitizationlibs

#27

Page 29: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Encodingpoints

§HTMLtags§HTMLattributes§ JavaScript§URL§ XML§ SVG

Page 30: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Encodingpoints

§HtmlEncoder (@<member> in.cshtml)§ JavaScriptEncoder§UrlEncoder§ Anysanitizer (e.g.,https://github.com/mganss/HtmlSanitizer orhttps://github.com/LibProtection/libprotection-dotnet)

#28

Page 31: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

PotentialXSS

<script src="@ViewData["UntrustedInput"]"></script>

<script>@ViewData["UntrustedInput"];

</script>

#29

Page 32: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

ContentSecurityPolicy(CSP)

app.UseMvc();

app.Use(async (context, next) =>{

context.Response.Headers.Add("Content-Security-Policy","default-src 'self'; report-uri /cspreport");

await next();});

#30

Page 33: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

CSPReportRequest

public class CspReportRequest{

[JsonProperty(PropertyName = "csp-report")]public CspReport CspReport { get; set; }

}

public class CspReport{

[JsonProperty(PropertyName = "document-uri")] public string DocumentUri { get; set; }[JsonProperty(PropertyName = "referrer")] public string Referrer { get; set; }[JsonProperty(PropertyName = "violated-directive")] public string ViolatedDirective { get; set; }[JsonProperty(PropertyName = "effective-directive")] public string EffectiveDirective {get; set;}[JsonProperty(PropertyName = "original-policy")] public string OriginalPolicy { get; set; }[JsonProperty(PropertyName = "blocked-uri")] public string BlockedUri { get; set; }[JsonProperty(PropertyName = "status-code")] public int StatusCode { get; set; }

}

#31

Page 34: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

CSPReportEndpoint

[HttpPost("~/cspreport")]public IActionResult CspReport([FromBody] CspReportRequest request){

// log and analyze the report...return Ok();

}

#32

Page 35: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

https://cspvalidator.org/

#33

Page 36: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Bypass

<form method="post" class="form-horizontal"action="/Account/Login"><h4>Use a local account to log in.</h4><input type="email" id="Email" name="Email" value="" /><input type="password" id="Password" name="Password" /><button type="submit">Log in</button><input name="__RequestVerificationToken" type="hidden"

value="CfDJ8MNYtGIQJ0NKvmDVDgK_YQ1W616zRzP4KVx5Pud1Z0kwNgsP4fkkzW3kapQF2iSQMQHIbq0jZIEO8deGn5YA41WC7</form>

#34

<base href='http://evil.com/'>

Page 37: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

x-xss-protection

app.UseMvc();

app.Use(async (context, next) =>{

context.Response.Headers.Add("x-xss-protection", "1");await next();

});

#35

Page 38: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Howtoprevent

§ Inputdatavalidationandfiltering§OutputsanitizationforHTML,JavaScript,XML,SVG§Goodpractice:usingContentSecurityPolicy(CSP)§Goodpractice:usingx-xss-protection

Page 39: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Anti-RequestForgery

Page 40: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Cross-SiteRequestForgery

#37

Page 41: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Double-SubmitCookiePattern

#38

https://www.some-bank.com

https://www.evil.com

Page 42: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Double-SubmitCookiePattern

<form method="post" action="/Home/MyAction"><div>...</div><input name="__RequestVerificationToken" type="hidden"

value="CfDJ8MNYtGIQJ0NKvmDVDgK_YQ2alNtW7VHnQAVGEoKzZhHQgrj0A0o8L8s9049_Z1ELltvpTkCt978aCpj1q9DQyb_XIsQ4s9URgHVFedXMkGQuf4kmXpaokoB</form>

https://github.com/aspnet/Antiforgery

#39

Page 43: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

NewToken

private static readonly RandomNumberGeneratorrandomNumberGenerator = RandomNumberGenerator.Create();

private static byte[] GenerateNewToken(int bitLength){

var data = new byte[bitLength / 8];randomNumberGenerator.GetBytes(data);return data;

}

#40

Page 44: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

NewToken

cryptoSystem = provider.CreateProtector("Microsoft.AspNetCore.Antiforgery.AntiforgeryToken.v1");

// ...

var bytes = cryptoSystem.Protect(stream.ToArray());

#41

Page 45: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

AntiForgery Token<form asp-controller="Account" asp-action="Login“ method="post"

asp-route-returnurl="@ViewData["ReturnUrl"]" class="form-horizontal"><h4>Use a local account to log in.</h4><div asp-validation-summary="All" class="text-danger"></div><div class="form-group">...</div>

</form>

<form method="post" class="form-horizontal" action="/Account/Login"><h4>Use a local account to log in.</h4><div class="text-danger validation-summary-valid" data-valmsg-summary="true">

<ul><li style="display: none"></li></ul></div><div class="form-group">...</div><input name="__RequestVerificationToken" type="hidden"

value="CfDJ8MNYtGIQJ0NKvmDVDgK_YQ2alNtW7VHnQAVGEoKzZhHQgrj0A0o8L8s9049_Z1ELltvpTkCt978aCpj1q9DQyb_XIsQ4s9URgHVFedXMkGQuf4kmXpaokoB</form>

#42

Page 46: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

EnableAutoValidation

public class Startup{

public void ConfigureServices(IServiceCollection services)

{services.AddMvc(options =>options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()));

}}

#44

Page 47: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Attributes

[AutoValidateAntiForgeryToken]public class CustomController : Controller{

// some methods…}

#45

Anantiforgery tokenisrequiredforHTTPmethodsotherthan:• GET• HEAD• OPTIONS• TRACE

Page 48: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Attributes

[ValidateAntiForgeryToken]public class CustomController : Controller{

[HttpPost][IgnoreAntiforgeryToken]public IActionResult DoSomething(SomeViewModel model){

// no antiforgery token required}

}

#46

Page 49: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

AJAX,WebAPI,SPAs…

§ Doyouuseauthenticationcookies?§ Nocookies,noproblems…exceptforstealingatokenbyXSSJ Forexample,youmayuseJSONWebToken(JWT)

§ Yes,Ido…Gotonextpage

#47

Page 50: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

AJAX,WebAPI,SPAs…@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf@functions{

public string GetAntiXsrfRequestToken(){

return Xsrf.GetAndStoreTokens(Context).RequestToken;}

}

<input type="button" id="antiforgery" value="Antiforgery" /><script>

$("#antiforgery").click(function () {$.ajax({

type: "post",dataType: "html",headers:{

"RequestVerificationToken": '@GetAntiXsrfRequestToken()'},url: '@Url.Action("Antiforgery", "Home")',

});});

#48

Page 51: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

AngularJS

HttpContext.Response.Cookies.Append("XSRF-TOKEN",antiforgery.GetAndStoreTokens(HttpContext).RequestToken,new Microsoft.AspNetCore.Http.CookieOptions{

HttpOnly = false});

services.AddAntiforgery(options =>options.HeaderName = "X-XSRF-TOKEN");

#29

Page 52: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Cross-OriginRequests

Page 53: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Cross-OriginRequests

#50

var invocation = new XMLHttpRequest();var url ='https://bar.other/resources/public-data/';

function callOtherDomain() {if(invocation) {invocation.open('GET', url, true);invocation.onreadystatechange =

handler;invocation.send();

}}

https://foo.example

https://bar.other

{"id": 2,"name": " Rubber duck","price": 12.50

},{"id": 3,"name": "Giant Unicorn","price": 25.50

Page 54: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Cross-OriginResourceSharing(CORS)

#51

https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

Page 55: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Simplerequests

§ Therequesthasamethod:GET,HEAD,POST§ Therequestcontainsonly theheaderssetautomaticallybytheuseragentandCORS-safelisted request-headers.

§ TheContent-Typeheadervaluesare:application/x-www-form-urlencoded,multipart/form-data,text/plain

§NoeventlistenersareregisteredonanyXMLHttpRequestUpload object§NoReadableStream objectisusedintherequest

#52

Page 56: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Preflighted requests

#53

https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

Page 57: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Preflighted requests

#54

https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

Page 58: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

SettingupCORS

#55

public void ConfigureServices(IServiceCollection services){

services.AddCors();}

public void Configure(IApplicationBuilder app, IHostingEnvironment env){

app.UseCors(builder => builder.AllowAnyOrigin());

// ...

Page 59: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

SettingupCORS

#56

public void ConfigureServices(IServiceCollection services){

services.AddCors(options =>{

options.AddPolicy("AllowSpecificOrigin",builder => builder.WithOrigins("http://example.com"));

});}

[Route("api/[controller]")][EnableCors("AllowSpecificOrigin")]public class ValuesController : Controller

Page 60: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Requestswithcredentials

#57

var invocation = new XMLHttpRequest();var url ='https://example.com/resources/';

function callOtherDomain() {if(invocation) {invocation.open('GET', url, true);invocation.withCredentials = true;invocation.onreadystatechange =

handler;invocation.send();

}}

https://ex.com

https://example.com

Page 61: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Requestswithcredentials

https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

#58

Page 62: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Requestswithcredentials

public void Configure(IApplicationBuilder app, IHostingEnvironment env){

app.UseCors(builder => builder.WithOrigins("https://ex.com").AllowCredentials());

// ...

#59

Page 63: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

OtherCross-OriginRequests

CORS

postMessage()

checktheORIGINusetokens#60

Page 64: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

SessionManagement

Page 65: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Overview

private string GetMessageFromCacheOrDb(){

// check session cachebyte[] value;if (HttpContext.Session.TryGetValue("msg", out value)){

return System.Text.Encoding.UTF8.GetString(value);}

var valueMessage = GetMessageFromDb(HttpContext.User.Identity.Name);

HttpContext.Session.SetString("msg", valueMessage);return valueMessage;

}

Page 66: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values
Page 67: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Overview

Page 68: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

ASP.NETCoreSessionFixation

§ Don’tstoresecuritysensitivedatainasession!..§ Fixitinyourproject.

Page 69: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

FixSessionFixation

TheNWebsec.SessionSecurity libraryimprovesASP.NETsessionsecuritybyenforcingastrongbindingbetweenanauthenticateduser’sidentity andtheuser’ssessionidentifier.

https://docs.nwebsec.com/projects/SessionSecurity/en/latest/https://github.com/NWebsec/NWebsec

Page 70: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Cookies

Page 71: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Cookies

An HTTPcookie (webcookie,browsercookie)isasmallpieceofdatathataserversendstotheuser'swebbrowser.Thebrowsermaystoreitandsenditbackwiththenextrequesttothesameserver.

#62

Cookiesaremainlyusedfor:

• Client-sidestorage• Sessionmanagement• Personalization• Tracking

Page 72: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Cookies

#63

Response.Cookies.Append("cookie-name", "secret-value", new CookieOptions{

Domain = ".my-domain.com",Path = "/docs",Secure = true,HttpOnly = true,Expires = DateTimeOffset.Now.AddHours(1),SameSite = SameSiteMode.Lax

});

var value = Request.Cookies["cookie-name"];

Page 73: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

new CookieOptions{

Domain = "my-domain.com",Path = "/docs",Secure = true,HttpOnly = true,Expires = DateTimeOffset.Now.AddHours(1),SameSite = SameSiteMode.Lax

};

DomainandPath

#64

Page 74: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

DomainandPath

public class ResponseCookies : IResponseCookies{

public void Append(string key, string value, CookieOptions options)

{var cookieHeaderValue = new SetCookieHeaderValue(

Uri.EscapeDataString(key),Uri.EscapeDataString(value));

cookieHeaderValue.Domain = options.Domain;cookieHeaderValue.Path = options.Path;

#65

Page 75: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

new CookieOptions{

Domain = ".my-domain.com",Path = "/docs",Secure = true,HttpOnly = true,Expires = DateTimeOffset.Now.AddHours(1),SameSite = SameSiteMode.Lax

};

HttpOnly andSecure

#66

Page 76: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

UsingHttpOnly isenough?

cryptoSystem = provider.CreateProtector(“MyCompany.MyService.v1");

// ...

var bytes = cryptoSystem.Protect(stream.ToArray());

#67

Page 77: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Whyisn'titenoughagain?

/* The serialized format of the anti-XSRF token is as follows:* Version: 1 byte integer* SecurityToken: 16 byte binary blob* IsCookieToken: 1 byte Boolean* [if IsCookieToken != true]* +- IsClaimsBased: 1 byte Boolean* | [if IsClaimsBased = true]* | `- ClaimUid: 32 byte binary blob* | [if IsClaimsBased = false]* | `- Username: UTF-8 string with 7-bit integer length prefix* `- AdditionalData: UTF-8 string with 7-bit integer length prefix*/

#68

Page 78: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Hosting

Page 79: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Hostingoptions

§ KestrelandIIS§ KestrelandNginx§ Kestrelonly(notrecommendedway)

Page 80: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Kestreloptions

§ HTTPSsupport§ Maximumclientconnections§ Maximumrequestbodysize§ Minimumrequestbodydatarate§ Keep-alivetimeouts§ Request/Responsebuffersizes§ Requestheaderlimits

https://github.com/aspnet/KestrelHttpServer/blob/rel/2.0.0/src/Microsoft.AspNetCore.Server.Kestrel.Core/KestrelServerLimits.cs

Page 81: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Summary

§MichalZalewski “TangledWeb.AGuidetoSecuringModernWebApplications”

§ StanDrapkin “SecurityDriven.NET”§OWASPDeveloperGuidehttp://bit.ly/1JcQLoh§OWASPTestingGuidev4

#69

Page 82: ASP .NET Core: Preventing attacks 2 - Contentful request contains onlythe headers set automatically by the user agent and CORS-safelistedrequest-headers. The Content-Type header values

Thanks!Questions?!

MikhailShcherbakov

@yu5k3https://www.linkedin.com/in/mikhailshcherbakov

IndependentDeveloperandConsultant

Thepresentationcontainsfootagefrom“GhostintheShell”andKowlooncityphoto