determining country or language using url - vb.net

I have written simple code to determine country location using the suffix on the url eg. .cn, .se, .br etc.
Does anyone have any ideas or even if its possible to determine country or location using urls that end in .com or .net?
I was just reading up on something and found online apps that can determine location/country from the ip so how would I determine the ip or look it up using the url? can I do this in .net?
OK
So I have the following code and I get an exception when the url is invalid or faulty, can anyone help me to catch the error and add the url to another list and continue with my loop.
Public Sub getIpAddress(ByVal querylist As ArrayList)
Dim IPList As New ArrayList
Dim badList As New ArrayList
Dim badHost As String
Try
For Each prod In querylist
Dim ipEntry As IPHostEntry = Dns.GetHostEntry(prod)
Dim IPAdd As IPAddress() = ipEntry.AddressList
IPList.Add(IPAdd.ToString)
Next
Catch ex As Exception
If ex.Message.Contains("No such host is known?") Then
End If
End Try
End Sub

You could easily use an IP-to-country mapping file such as this to build a little tool to infer location from the IP. I would also supplement this with the TLD of country specific domains such as .co.uk and .co.nz.
You can get the IP from the host name in java using something like:
InetAddress addr = InetAddress.getByName("www.bbc.co.uk");
Long ipNum = ipToInt(addr.getHostAddress());
You'll have to map to the equivalent in your language.

If it is .net or .com you cannot read the location from the url.
You can read the language of the browser, see http://www.west-wind.com/weblog/posts/334.aspx
You could also read the IP address of request, and then use a location service to find where the request came from.

Not from the URL, but from the IP, you can do this. There are publicly available files which map an IP range to a country.
Note that the location within a country is quite hard to get, as you can only know the address of an ISP, not of a Web site.

You can determine the country where the servers are located.
You can find out the location of the domain registrar.
But then, the company or a person behind the site may live in some third place.
Whose geolocation are you trying to find out?

Related

DKIM Signing an email using MimeKit in a vb.net project

I'm using MimeKit to send emails and the use of DKIM to sign them has been broached. I've looked at the example on the MimKit site, and googled the terms but found no answers.
Public Shared Sub DkimSign(ByVal message As MimeMessage)
Dim headers = New HeaderId() {HeaderId.From, HeaderId.Subject, HeaderId.Date}
Dim headerAlgorithm = DkimCanonicalizationAlgorithm.Simple
Dim bodyAlgorithm = DkimCanonicalizationAlgorithm.Simple
Dim signer = New DkimSigner("filename", "domain", "selector") With {.SignatureAlgorithm = DkimSignatureAlgorithm.RsaSha1, .AgentOrUserIdentifier = "#eng.example.com"}
message.Prepare(EncodingConstraint.SevenBit)
message.Sign(signer, headers, headerAlgorithm, bodyAlgorithm)
End Sub
When instantiating the signer it requires a filename, domain and selector. If I'm sending an email from "bob#website.com" I would assume that the physical file would be placed on the root of the site and the instantiation would look something like this:
Dim signer = New DkimSigner("dkim.txt", "website.com", "") With {.SignatureAlgorithm = DkimSignatureAlgorithm.RsaSha1, .AgentOrUserIdentifier = "???"}
But not sure the format/reason for the AgentOrUserIdentifier ... can anyone edify me or correct me if my assumptions are wrong?
The fileName parameter is the path to the private key that should be used for signing the message. I'm not sure why you expect it to be at the root of the site or why it would be called dkim.txt, but I can almost guarantee that both assumptions are wrong.
A selector is used by the receiving client to locate the correct public key in the DNS records because it's possible for the same domain to have multiple keys that it uses for signing.
The AgentOrUserIdentifier corresponds to the i= parameter that you find in the DKIM-Signature header and specifies the user or agent that is taking responsibility for the signature.

WinNT ADSI provider - cross domain user lookup

''I am using the WinNT ADSI provider in a bit of authentication code, which looks up the user from Active Directory and checks its group membership.
We have run in to an issue getting this working for cross domain access. We followed the steps outlined here (https://support.microsoft.com/kb/241737?wa=wsignin1.0) to set up a Cross-Reference to an External Domain in Active Directory. This should be all that is required to allow WinNT to find the users.
When the code is run we only ever find one object under the domain (in the loop below) - "Schema". This is not correct - there are MANY child objects.
This problem seems to be intermittent - the same system did not have this issue a month ago. I realise this will be hard to investigate but someone who has a better understanding of ADSI may know better.
The below code illustrates the problem:
Dim objUser
Dim sUserName
Set ns = GetObject("WinNT://DOMAINNAME")
msgbox "Found " & ns.AdsPath & " (" & ns.Class &")" ' Shown
'ns.Filter = Array("User") ' Commented to show ALL objects
For Each UserObj in ns
Dim UserName
UserName = UserObj.Name & " " & UserObj.Class ' Returns "Schema Schema"
msgbox UserName
Next
This solution works but I'd like to point out why. After hours of trying to determine what the importance of the dns suffix on NetBIOS resolution for the WinNT provider, I found that the client makes a call to the local domain controller first to do an LSA_LookupNames call for the NetBIOS name and it gets back a domain controller in the remote domain/forest to go to which is responsible for that NetBIOS name. Subsequent to that lookup, it attempts to connect to the domain controller that was returned - but the name of the server is the simple host or NetBIOS name! So, it has to look that up and it uses DNS for that, trying suffixes in the order prescribed in the network config of the client. So, the domain is being translated properly by the local AD domain as part of the lookup but the client can't figure out how to get to that DC because LSA_LookupNames doesn't return an FQDN, just a hostname.
Hopefully this will save others the time I burned searching - sometimes it pays to just break down and open wireshark.
The solution was to ensure that the local computer, that the query was being run from, had a DNS suffix for the remote domain

How to validate the active directory domain my app is running in?

I have a VB.Net Application that authenticates using the current Windows User without asking for the password. It checks that this user is a member of "MYDOMAIN\ApplicationUsers" before it starts up.
How to check if this is the real domain and not a different one using the same name? Are there any certs or public keys to validate locally? I'd prefer to check this offline, without a third party machine or database etc.
In the System.DirectoryServices.ActiveDirectory Namespace are some Trust an Validate methods but they only seem to check inter domain trust and using a domain name only.
Your problem is that you are using strings and strings like mydomain/application users are not unique across domains. One possibility is to use the SID of the application users group in your expected domain instead of the name. Then you can check the SID of the group to make sure it matches the sid for the expected application users group at run time before checking membership. It would be much harder for a malicious user to spoof domain and group parts of the Sid then the domain and group name.
Ultimately if you are running code on a machine that is owned by the malicious user then this just raises the bar and they could still circumvent this check.
I made some example code which checks the group's SID as Mike suggested. You just need to put your group's SID in the constructor of the SecurityIdentifier class to make the check work against the currently logged on user.
Private Sub DoCheck()
Dim sid As New Security.Principal.SecurityIdentifier("S-0-0-00-0000000000-0000000000-0000000000-000"),
result As Boolean
result = IsUserInGroup(sid)
End Sub
Public Shared Function IsUserInGroup(sid As Security.Principal.SecurityIdentifier) As Boolean
Dim user As UserPrincipal
user = UserPrincipal.Current
For Each group As Principal In user.GetGroups()
If group.Sid.Equals(sid) Then Return True
Next
Return False
End Function
To make the code work you need to import System.DirectoryServices.AccountManagement:
Imports System.DirectoryServices.AccountManagement
This namespace is located in Microsoft's System.DirectoryServices.AccountManagement.dll which is available since .Net 4.0 I believe.

Get client IP in vb.net

I am trying to get client ID with vb.net. My code is as under :
Partial Class foo
Inherits System.Web.UI.Page
Private ipv4 As New clsIPv4
Dim ref As String = 0
Dim client_ip As String
client_ip = Request.UserHostAddress()
Dim myHost As String = System.Net.Dns.GetHostName()
client_ip = ipv4.ResolveAddress(myHost)
This code is returning me list of ip adresses (i.e 182.50.130.143;118.139.172.1;118.139.172.2;118.139.172.3;118.139.172.4;118.139.172.5;118.139.172.6;118.139.172.7;118.139.172.8;118.139.172.9;118.139.172.10;118.139.172.11;118.139.172.12;118.139.172.13;118.139.172.14;118.139.172.15;118.139.172.16;118.139.172.17;118.139.172.18;118.139.172.19;118.139.172.20;118.139.172.21;118.139.172.22;118.139.172.23;118.139.172.24;118.139.172.25;118.139.172.26;118).
Please guide where I am wrong
If you are using ASP.NET, you can use: Request.UserHostAddress
to get your client IP address.
But if you are using a windows application to get local IP address, in fact you may receive multiple IP addresses! In this case, if you want to get a specific address which you know the netid part of the IP address you can compare all IP addresses with a corresponding netid address.
This worked perfectly for me
System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName()).GetValue(0).ToString()

Getting IP address of my computer

How can I get the IP address of my computer (on which my application is running)
in vb.net
Thanks
Furqan
See System.Net.DNS.
Something like this should work:
Dim ips As IPAddress() = Dns.GetHostAddresses(Dns.GetHostName())
Dim index As Integer
For Each ip in ips
Console.WriteLine(ip)
Next ip
One way is to use System.Net.Dns.GetHostAddresses, passing it the empty string. Note that it will give you an array of addresses, this is because a host can have multiple addresses, one for each interface. A common example would be the loopback address (127.0.0.1) and one or more public facing IP addresses (eg 10.10.1.1). If your machine has a specific host name you can use that instead of the empty string.