# Write Your Own Simpson

{% hint style="info" %}
The script randomly chooses characters from the show and assigns them common sitcom roles. But, there's a catch... D'oh! For some reason Homer's name never gets chosen. Figure out why.
{% endhint %}

What we get. oooooooooffffff!!!!

```c
# lliliiLlls

module 		lllililli;
	export 




{  global llil:
		set



		[


count




]={

0x00	,




}

					;
		type 


illlllliiili:




enum
		{




illlllliiiliii,
illlllliiiliiii,
illlllliiiliiiiiiii,
illlllliiiliiiiiiiil,




	};

  global 
illill:		table




[
string
			] 
                 	of 
 string
			=
			{
    			[
"Main"


			] 

= 

	"The majority of the episode is based on what this character does",


	[


"Comic Relief"

  ]

= 


"The major of bad fortune happens to this character through the episode",
    [
		"Build Up"
	] 		
	= 

																			"The first 1/3 of the episode is about this character and sets the stage for the last 2/3",
  }	
;

			# Oh boy, here we go!
		  	global
 llllii:
  vector 


	of 

string					=


{"Homer"
	,"Marge"
		,"Lisa"
			,"Bart"
				,"Maggie"
					,"Grandpa"
						,"Ned"
							,"Krusty"
								,"Dr. Hibbert"
									,"Moe"};

	  global
 lllli:



function(
		)	;	}

function lllililli::lllli(
	) 
{
  for (
lliliiLll 			in 
lllililli::illill

	) 
	{
    local 
i 
	 			 



				=0x0		
			    	;while 
	(
i
 				in 

lllililli::llil
	) 
{		srand

	(		double_to_count

		(		time_to_double

			(		current_time

				(


))))

;


      i 
= 
rand(
	|	
lllililli::llllii
	|
)	;
    	}
    
 add 
	
		lllililli::llil[
i

				]


;

    local        

		  iillliliiililil 


= 		lllililli::llllii[i];
    local         ililllllllllilii 

= 		lliliiLll;
    local    

		  liiiiliiiiiiiiii 

= lllililli::illill[lliliiLll]

  ;

    print 			fmt
	  (


"%s %s  %s%s   %s"




		
,    iillliliiililil   , " " 
,    ililllllllllilii  , " " , liiiiliiiiiiiiii


  )
	;
 
  }
  }

event				zeek_init
  (
  ) 	&priority


= 0x00

 {
		  lllililli::lllli




(							);

							}

```

What!!! Lets clean that up. Quite a bit going on with the naming of variables and modules. So lets refactor that a little.

```c
module simpson;

export {

    global randSet: set [count] = {
        0x00,
    };

    global characterType: table [string] of string = {
        ["Main"] = "The majority of the episode is based on what this character does",
        ["Comic Relief"] = "The major of bad fortune happens to this character through the episode",
        ["Build Up"] = "The first 1/3 of the episode is about this character and sets the stage for the last 2/3",
    };

    global characterNames: vector of string = {
        "Homer",
        "Marge",
        "Lisa",
        "Bart",
        "Maggie",
        "Grandpa",
        "Ned",
        "Krusty",
        "Dr. Hibbert",
        "Moe"
    };
    
    global goRun: function();

}

function simpson::goRun() {
    for (el in simpson::characterType) {
        local i=0x0;
        while (i in simpson::randSet ) {
            srand(double_to_count(time_to_double(current_time())));
            i = rand(|simpson::characterNames|);
        }

        add simpson::randSet[i];

        local name = simpson::characterNames[i];
        local usertype = el;
        local explanation = simpson::characterType[el];

        print fmt ("%s %s %s", name, usertype, explanation);
    }
}

event zeek_init ()   &priority = 0x00 {
    simpson::goRun();
}
```

So why would homer never be selected. Sounds like an off by one error to me. So focusing on the select function and associated variables.

```c
    global randSet: set [count] = {
        0x00,
    };
    
    for (el in simpson::characterType) {
        local i=0x0;
        while (i in simpson::randSet ) {
            print i, i in simpson::randSet;
            srand(double_to_count(time_to_double(current_time())));
            i = rand(|simpson::characterNames|);
        }

        add simpson::randSet[i];
        print simpson::randSet;

        local name = simpson::characterNames[i];
        local usertype = el;
        local explanation = simpson::characterType[el];

        print fmt ("%s %s %s", name, usertype, explanation);
    }
```

The way the logic is written it won't exit from the while loop until it has chosen a unique name from the set. The value choosen should not be in the set.  However since you have an initialized that set with a 0 which corrisponds with "Homer" that value will never be returned.

{% hint style="success" %}
My Solution

```
The global set of count "llil", is initialized with 0x00.
The local variable i is set to 0 initially and checks membership with that set.
Since the initial value of 0 will always be "in" the global set, a different
character name from "llllii" vector of string will always be selected.
```

{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://eephillip.gitbook.io/zw2020-ctf-writeup/writeup/write-your-own-simpson.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
