Making a DNS query in Ruby from scratch
jvns.ca
jvns.ca
https://github.com/jaysoffian/eap_proxy/blob/78a058ffe67c253...
The dnspython package is pure python and it's a lot of code, but it supports pretty much everything related to DNS:
Just blindly copying flags off of a packet isn't really a good practice for software development. What if the packet was not recursive? I'm sure 99.99% of the people reading that have no idea that non-recursive packets were possible, or even what it means.
Instead, read RFC 4.1.1 and figure out what the flags mean and why you'd use one or the other.
https://www.rfc-editor.org/rfc/rfc1035
And by reading the actual format document you can start looking for vulnerabilities. For example, what happens when the various COUNT numbers aren't correct in a response? What if you get an error response code but there is response data? What if the implementor used signed instead of unsigned numbers for COUNT?
This is a bit better than SO, but not by much. You're not writing a DNS query, you're hacking someone else's DNS query and sending it over the wire.
compared to Python, Ruby has:
- first-class symbols (yes python has sys.intern but it would take a PEP giving them a pithy syntax to make them usable, plus python has 25 years of stdlib and libraries using "strings" or enums for constants instead of :symbols)
- procs/blocks and better-than-python lambdas
- "open classes" / monkey-patching of builtins (for better or for worse)
- trivial metaprogramming with method_missing (for better or for worse)
some of these make fun one-off projects easier or faster, some of them would be less welcome in large, mature codebases.But just to be a bit contrary:
- I don't see a huge value in symbols. In Ruby they are literally just static strings which means they use memory you'll never get back – potentially important if you're e.g. parsing something large into a hash and symbolizing the keys. If you have to put a non-alphanumeric character in a symbol you still need to use quotes.
- Procs, blocks, and lambdas – yes.
- Metaprogramming and monkey patching? dfjasdjldfjkdfjlkfdjldfoh4houfhufl. A double edged sword at best and 100% not something I'd want to see in a larger codebase. Javascript folks largely learned this lesson with the shift from Prototype to jQuery. You can do some really neat-o things but they're almost always unintuitive to the uninitiated.
One important advantage of symbols versus strings is that Symbol#== is constant time.
At that point though why not use integer constants?the real footgun is dependencies that monkey patch things you don't know about / don't expect, and then the order you require those dependencies becomes important. augughghhhhh I hate that part.
I get it that the hype around Ruby and Rails has—thankfully—subsided but Ruby is even better today than it was then.
Sure when it was hype a decade ago you'd get lots of flashy tools and library every other days, but a large part of it was really wonky.
Now that the dust settled, the tools that remain largely used are much higher quality.
Ruby gets results.
This is not the case today. And most of those startups also had to then migrate to Go/Java/etc. to scale.
You can scale anything if architected right.
You can't make languages fast and fun to write in if they aren't that to begin with. There's a lot to be said for dev morale and productivity.
I.e https://news.ycombinator.com/item?id=20349004
"I worked at Twitch from 2014 to 2018... No more Ruby on Rails because no good way was found to scale it organizationally; almost everything is now Go"
I love Elixir as a language but I still find myself coming back to Ruby frequently.
Python exists, but there’s nothing about the language that makes me want to use it. Quite the opposite. I find myself avoiding it whenever possible.
As a prominent Python dev told me, “It’s the okayest language out there.”
Beyond that Python is opinionated. In a lot of ways this is an improvement over e.g. Perl. However enjoyment is largely predicated on liking the opinions, if you don't it's not fun. For instance I wanted to write a multi-line lambda recently (mostly to make it easier to read). With Ruby and Rust I can do this pretty easily. With Python? No dice. Sure, there are good reasons to not make a lambda a multi-line ordeal but sometimes I just want to.
1. “One way of doing everything”
2. Had a “Rails like” framework with Django
3. If I wanted to dip my toes in an AI library it would be a good choice
4. Access to a lot of system level libraries.
These all fell apart for me after getting into it.
1. As the other commenter suggested, there’s not even one clear way to handle package management. It’s a mess. In other languages, I’ve never had a good development experience with Docker because native is usually so much smoother, but with Python native is a bit of a mess with everyone recommending different tooling. This was compounded because I got into it during the last year of Python 2 so library segmentation was a bit of an added problem.
2. I’m hugely opposed to the way Django handles database changes compared to standard Rails migrations. This is personal preference.
3. I never had time to get into it, but figured if I ever did I’d setup something more dedicated for it. Plus Elixir released so much tooling around this space that I’d probably just go that route anyway.
4. I haven’t found anything system level that I need that I can’t get from Ruby or just calling directly from the command line.
Those were the biggest items.
It’s just not for me. I say that knowing full well that there are plenty of people who don’t like Ruby.
A bit of rant - what annoys me on similar "examples" like articles - those have none to 0 error handling and not even assuming things may go bad. Doing bind() with hope it will always succeed, connect without timeout - for me as a newbie such things are important to be provided in examples as well.
-Carl Sagan
With that said, it was cool to see a lower-level explanation than just calling a library.
DOH_SERVER=eu1.lavate.ch
(echo example.net;echo example.org;echo example.com) > 1.txt
yy048 < 1.txt|yy049|yy050|sed "s,.*,https://$DOH_SERVER?dns-query?dns=&," \
|yy025|openssl s_client -connect $DOH_SERVER:443 -ign_eof
This uses HTTP/1.1 pipelining and only a single TCP connection. Good netiquette.Instead of openssl one can use, e.g., stunnel, or an https proxy, plus netcat.
The sources for yy025, yy048, yy049 and yy050 are just short text files compiled with flex. If preferred, yy025 can be replaced a little more shell script and tr(1).
No Ruby, no Python, no Go, or other large languages. Just shell utilities.
Parsing catenated responses can be done with ldns, e.g., drill.
and the HTML source also includes a machine-readable element:
<p class="meta">
<time datetime="2022-11-06T08:31:53" pubdate="" data-updated="true"></time>
</p>
For my own journal, I tuck human-readable metadata inside a <details> block (which defaults to hidden), with the title in the nested <summary> (which defaults to visible). Thus, it's available, if visitors activate the title to reveal it.The world may be a better place without SEO.
Further, this code implements a stub resolver querying 8.8.8.8 --- in that scenario, there is no DNSSEC verification, as you know. For stub resolvers, the kind your browser or OS uses, DNSSEC condenses down to a single bit in the header that the server uses to say "trust me, I did DNSSEC".
> Further, this code implements a stub resolver
Fair enough, but…
> DNSSEC condenses down to a single bit in the header that the server uses to say "trust me, I did DNSSEC".
…they did not ask (in the query) for DNSSEC verification, nor did they check the bit in the response.
It's possible that the reason I said "less than 4% of North American domains" is that I simply made a mistake, and should instead have said "less than 4% of all domains". Again: .COM has a 1.6% uptake. There are years in the last ~4 where DNSSEC uptake fell in .COM.
DNSSEC is moribund.
For how many years have you been saying that? Meanwhile, from what I can tell, DNSSEC usage keeps going up.
All I can say is that from personal experience when working at a registrar and DNS service provider, the number of people asking about and requesting DNSSEC is increasing all the time, and show no signs of decreasing. Also, all registries (i.e. TLDs) are also all pushing for registrars and DNS service providers to provide DNSSEC, so there is demand from both sides. Note: I do not have any financial incentive to push DNSSEC; in fact, strictly speaking, DNSSEC makes my job harder.
Also, as I have mentioned before, I have never seen anyone argue against DNSSEC with any persistence (in industry interest groups, at conferences, etc). Except you, here on HN. And you really seem to have it in for DNSSEC, even going so far as to keep making arguments against the crypto, not only while it was obvious that it could (and would) be fixed, but even making the same argument after it was actually fixed. You keep shifting your arguments, but keep arguing against DNSSEC with whatever you can find. This does not make you look credible. And your sole remaining argument, that DNSSEC has low usage, is not a very good one, if it is in fact the case that the usage is actually (on the whole) increasing.
The number of people not requesting it is increasing all the time too.
But HTTP without HTTPS is useful if you need to go really fast, same goes for taking DNS out of the loop and going straight to the IP.
The way I see it, it's all about your threat model. Who/what are you defending against.