You have two possibilities in this case that doesn't require code.
1) Change the permission level of anonymous users
When you change the permission level from "Read" to "Restricted Read" your anonymous user won't see the site contents anymore.
Example - You can add anonymous users to the Restricted Readers group. Or Create a custom group and assign "Restricted read" permission to it.
2) Alternate read permission level of anonymous users
You can simply remove the base permission "View Application Pages" from the "Read" Permission level or create a custom permission level and remove "View Application pages" permission from it by unchecking it as below for anonymous users:
The "site contents" is not the only place where user will see this navigation option. It is also available through the gear icon on the suite bar. By changing the permissions or permission level it will be hidden from there too.
In the masterpage , you try below code in masterpage:
Find the SharePoint:SiteActions tag, inside it you will find
<SharePoint:MenuItemTemplate runat="server" id="MenuItem_ViewAllSiteContents",
PermissionsString="" property to:
Reference - Hide site content for visitors
Security trimming publishing site
3) Activate the limited access user permission lockdown mode feature
This feature will block users to view list view pages. Users can able to access all other web pages.
In 2010, we had the FormpagesLockDown feature, however, in 2013 we need to activate this feature "limited access user permission lockdown mode".