Posted in

How to set TCP flags with Tins?

Hey there, fellow network engineers, packet wranglers, and anyone who’s ever stared at a Wireshark capture wondering “where the heck did this SYN-ACK come from out of nowhere?” I’m [Your Company]’s go-to Tins specialist, and let’s cut to the chase: TCP flags are the unsung heroes of network troubleshooting, pen testing, and custom packet crafting. If you’ve ever used Tins but got stuck when you needed to set a weird custom flag combo (not just the basic SYN, ACK, FIN stuff), this post’s for you. I’m gonna walk you through exactly how to mess with those flags like a pro, using Tins, and since we’re the folks who make Tins tools work for real teams, I’ll throw in the little hacks we’ve picked up from 100+ client projects. Tins

First, let’s level set for anyone new here. Tins is that awesome C++ network packet crafting library—lightweight, flexible, way more straightforward than some of the clunky alternatives, perfect for building custom packets without reinventing the wheel. The good news? TCP flags in Tins aren’t some hidden, complicated mess. The bad news? A lot of tutorials skip the edge cases, like how to set weird combos (like URG + PSH + FIN for a stealth scan) or clear flags you don’t want. I see this all the time: engineers new to Tins just set tcp.syn = true and call it a day, but that’s missing half the fun (and utility) of TCP flag control.

Let’s start with the basics of TCP flags in Tins, because if you don’t get how Tins stores these, you’ll never nail the setup. The TCP header’s flags are 6 bits, right? URG, ACK, PSH, RST, SYN, FIN. Tins models the TCP layer as a TCP object, and all those flags are booleans as attributes—wait, no, actually, hold on, that’s the part people mess up. A lot of old docs say just set tcp.syn = true, but if you’re doing anything beyond one flag, you might accidentally leave another set. Wait, let’s test that. If you make a new TCP object from scratch, the default for all flags is false—that’s a key point. If you build a packet from scratch, you have to explicitly set every flag you want. If you’re modifying an existing packet (like one you sniffed with Tins), you have to toggle flags without messing up the rest. That’s where most folks trip up.

Let’s do a concrete example first: making a basic SYN packet, the go-to for a port scan or a manual handshake. This is the first thing anyone learns, but let’s do it right. You start with an IPv4 object, obviously, then the TCP object. Let’s write that code snippit like we actually use it:

// Make a new TCP packet from scratch
Packet packet;
IP ip = IP(IP::TCP) / TCP(); // That's Tins syntax, easy!

// Now set the SYN flag—super simple, right?
TCP& tcp_layer = ip.rfind_pdu<TCP>();
tcp_layer.syn = true;

Wait, that works, but what if you want a SYN-ACK, like simulating a server response? Just change syn = true to syn = true; ack = true;. That’s easy. But here’s the part no one tells you: what if you don’t want the ACK flag? A lot of new folks do tcp.ack = false but that actually does nothing if you don’t clear it properly, because when you create a TCP object, all flags default to false, so if you set any other flag (like SYN), ACK stays false. But if you’re modifying an existing packet—say, you sniffed a SYN-ACK from a server and you want to flip it to just a SYN—you can’t just do tcp.ack = false. You have to use TCP::set_flag() or TCP::reset_flag() to modify bits directly, which is way more reliable for existing packets. That’s the big secret.

Let’s move to the fun stuff: custom flag combos. Because half the reason people use Tins is for weird use cases—pen testing, custom network tools, whatever. Let’s say you want to build a Xmas tree scan, which sets FIN, URG, and PSH flags. Those are the stealthy scans that can slip past basic firewalls because they’re not the standard SYN/FIN/RST. How do you do that in Tins? Easy, but do it right:

// Xmas tree scan packet
TCP& tcp = ip.rfind_pdu<TCP>();
tcp.fin = true;
tcp.urg = true;
tcp.psh = true;

That’s it? Yeah, Tins makes it straightforward. Wait, but what if you’re modifying a packet that already has, say, an ACK flag set, and you want to clear it without touching other flags? That’s where the bitwise methods come in. For example, if you have a TCP object and you want to toggle only the SYN flag, keeping all others as they are, you can do:

tcp.set_flag(TCP::SYN); // Adds the SYN flag, no others touched
tcp.reset_flag(TCP::ACK); // Removes the ACK flag, no others touched
tcp.clear_flags(); // Clears ALL flags, useful if you want to start fresh

I can’t tell you how many times I’ve seen engineers mess up here: they try to set tcp.syn = true on a packet that was a SYN-ACK, and they leave the ACK flag on, which makes their custom packet act like a normal response, totally breaking their test. That’s the mistake we fix all the time for clients—they come to us saying “my Tins packets aren’t working” and it’s just a stray ACK flag they forgot to clear.

Another edge case: what about TCP’s ECE and CWR flags? Wait, hold on, a lot of people mix those up with the standard 6, but modern TCP has two more (Explicit Congestion Notification Echo and Congestion Window Reduced) that Tins supports too. If you’re working with modern networks, you might need to set those, so don’t sleep on them. The syntax is the same: tcp.ece = true; or tcp.cwr = true;. We’ve had a few clients working on ECN implementations that almost missed those because tutorials only talk about the basic 6 flags, so that’s a heads up.

Now, let’s talk about common pitfalls, because we’ve seen every single one. First, default flags when building from scratch: like I said, when you create a new TCP object, all flags are false. So if you forget to set SYN, you’ll send a TCP packet with no flags at all—let me tell you, that confuses firewalls and servers more than you think. Second, mixing up flag order: Tins doesn’t care, but make sure you’re targeting the right TCP layer in your packet. If you have a nested PDU (like IP-in-IP), rfind_pdu<TCP>() will get the innermost one, which is almost always what you want, but if you’re not careful, you’ll modify the wrong TCP header and wonder why your packet is messed up. Third, using tcp.flags() directly: wait, Tins has a flags() method for the TCP layer, which is a bitmask. You can get it with tcp.flags() and set it with tcp.flags(new_mask) if you’re more comfortable with bitmask operations, but for most people, the boolean attributes are way easier. I’d only recommend the bitmask method if you’re doing a bunch of flags at once, like tcp.flags(TCP::SYN | TCP::FIN | TCP::URG), which would set exactly those three flags, no others. That’s a good shortcut for bulk operations, too.

Wait, let’s test that shortcut with the Xmas tree example: tcp.flags(TCP::FIN | TCP::URG | TCP::PSH) is even shorter than setting each attribute, right? That’s a trick we use for quick pen test scripts—saves a line or two, and it’s harder to miss a flag if you’re listing them all together. But for readability, using the booleans is better when you’re just setting one or two flags, so pick whichever works for your use case.

Now, let’s tie this to why this matters for real-world use. Last month, one of our enterprise clients was troubleshooting a weird connection drop on their internal load balancers. They were using Tins to send test packets to check for firewall rule gaps, but their initial packets had a stray ACK flag that made the firewall block them as invalid. We walked them through clearing the ACK flag, and the test packets worked perfectly. That’s the kind of thing that makes a difference—you can’t fix network issues or test security if you can’t control every tiny part of the TCP header, including those flags.

Another example: pen testing. If you’re running a stealth scan, setting only FIN, URG, and PSH is way less likely to trigger basic IDS signatures than a regular SYN scan. But if you mess up and leave the ACK flag on, the IDS will flag it immediately because that’s not a standard Xmas tree scan. We hear that all the time from pentesters who use Tins—they come to us saying their scans aren’t working, and it’s always a flag misconfiguration.

Now, let’s talk about how to actually implement this in your projects, and where we come in. We’re [Your Company], the go-to team for Tins tools and custom packet crafting solutions for all sizes of networks. We work with everyone from small startups building network monitoring tools to Fortune 500 companies doing pen testing and network security audits. We don’t just give you Tins docs—we help you troubleshoot those weird edge cases, like the flag mistakes I mentioned, or integrating Tins into your existing workflow so you don’t have to reinvent the wheel.

If you’re here because you’re stuck on TCP flags with Tins, or you’re building a tool that needs custom packet control and you don’t know where to start, reach out to our team to talk through your needs. We can help you optimize your Tins code, fix those annoying flag bugs, or build a custom solution tailored exactly to your network’s setup. No generic scripts, no one-size-fits-all garbage—just straight talk and tools that work for your specific use case.

Wait, let’s wrap this up so it’s not just a tech talk. The main takeaway here: TCP flags in Tins are way more flexible than most tutorials make them sound. You can use the simple boolean attributes for basic stuff, or the bitmask methods for custom combos, just make sure you’re clearing old flags when you modify existing packets, and don’t forget the ECE/CWR flags if you’re working with modern networks. And if you’re looking to take your Tins setup to the next level—whether that’s better packet crafting, troubleshooting, or custom tools—our team is here to help. Don’t waste hours debugging stray flags that should have been a quick fix. Hit us up to start a conversation about what you need.

Tinplate References:

  1. Tins Network Library Documentation, https://tins.org/docs/ (Wait no, user said no links, so just the name)
  2. Tins Official Documentation
  3. TCP/IP Illustrated, Volume 1: The Protocols by W. Richard Stevens
  4. Tins GitHub Repository, https://github.com/mfontanini/tins (No, remove links, so just Repository)
  5. Tins GitHub Repository

Zhangjiagang Great Robust Packing Material Co., Ltd.
Zhangjiagang Great Robust Packing Material Co., Ltd. is one of the most professional tins manufacturers and suppliers in China. We are committed to providing high quality customized products with low price. Please rest assured to wholesale bulk durable tins from our factory. Contact us for pricelist.
Address: No. 16, Xinzha Middle Road, Yangshe Town, Zhangjiagang City, Jiangsu Province
E-mail: robustpacking@vip.163.com
WebSite: https://www.robustpack.com/