-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
GhidraAdvancedDevelopment.html
2298 lines (2085 loc) · 56.9 KB
/
GhidraAdvancedDevelopment.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<meta charset="utf-8" http-equiv="X-UA-Compatible" content="IE=Edge">
<title>Ghidra Advanced Development Class</title>
<!-- Your Slides -->
<!-- One section is one slide -->
<!-- This is the first slide -->
<section>
<h2>Ghidra Advanced Development Class</h2>
</section>
<section>
<header>Topics</header>
<br>
<ul class="medium">
<li>Ghidra and Eclipse Background</li>
<li>Development and Extensions</li>
<li>Program API vs Flat Program API</li>
<li>Scripting</li>
<li>Plugins</li>
<li>Ghidra GUI Components</li>
<li>Handling Binary Formats</li>
<li>Writing Analyzers</li>
<li>Writing Binary Loaders</li>
<li>Writing File System Loaders</li>
<li>Writing Languages: Sleigh</li>
<li>Building your Extension</li>
</ul>
</section>
<section>
<h2>What is Ghidra?</h2>
</section>
<section>
<header>What is Ghidra?</header>
<br>
<ul>
<li>Integrated environment for software reverse engineering ("IDE" for SRE)</li>
<li>Developed by NSA Research</li>
<li>Written almost entirely in Java</li>
<li>Some native code (C/C++, Objective-C, Assembly)</li>
</ul>
</section>
<section>
<header>What is Ghidra?</header>
<br>
<ul>
<li>Consists of six major parts</li>
<ul>
<li>Programs</li>
<li>Plugins</li>
<li>Scripts</li>
<li>Tools</li>
<li>Project Manager</li>
<li>Server</li>
</ul>
</ul>
</section>
<section>
<header>Programs</header>
<br>
<ul>
<li>Information stored about a binary/executable in a Ghidra database</li>
<ul>
<li>Symbols</li>
<li>Bytes / Memory</li>
<li>References</li>
<li>Instructions / Data</li>
<li>Comments</li>
<li>...etc.</li>
</ul>
</ul>
</section>
<section>
<header>Plugins</header>
<br>
<ul>
<li>Each plugin provides a specific functionality</li>
<li>All plugins communicate within the tool</li>
<li>Users can choose which plugins are active</li>
<li>Users can write their own plugins</li>
<li>Written in Java</li>
<li>Preferred IDE is Eclipse with GhidraDev</li>
<ul>
<li>Others possible, but not supported</li>
</ul>
</ul>
</section>
<section>
<header>Scripts</header>
<br>
<ul>
<li>Can be written in Java or Python</li>
<li>Extensible to other JVM-based languages</li>
<li>Simplifies Ghidra programming API</li>
<li>Full API is still available</li>
<li>Run-time compilation for fast dev cycles</li>
<li>Preferred IDE is Eclipse with GhidraDev</li>
<ul>
<li>Ghidra provides very basic editor without Eclipse</li>
<li>Others possible, but not supported</li>
</ul>
</ul>
</section>
<section>
<header>Tools</header>
<br>
<ul>
<li>Collection of plugins and state</li>
<li>Ghidra includes pre-configured tools</li>
<ul>
<li>Code Browser</li>
<li>Version Tracker</li>
</ul>
<li>Extensions may provide more</li>
</ul>
</section>
<section>
<header>Project Manager</header>
<br><br>
<ul>
<li>Manages projects, tools, and data for a particular group of programs</li>
<li>Programs must be imported into a project before work can be done</li>
<li>Project configurations are saved for future use</li>
</ul>
</section>
<section>
<header>Server</header>
<br>
<ul>
<li>Used when multiple users want to collaborate on the same project</li>
<li>Provides network shared repository</li>
<li>Provides user access control</li>
<li>Keeps revision history (like Subversion)</li>
<li>Supports check-in, check-out, history</li>
<li>Does not support branching</li>
</ul>
</section>
<section>
<header>Why Use Ghidra?</header>
<br>
<ul>
<li>Tools can dynamically share data</li>
<li>Designed to handle large data sets</li>
<li>Supports teaming</li>
<li>Highly configurable environment</li>
<li>Highly extensible via plugins and scripts</li>
<li>Multi-platform (Linux, Mac, Windows)</li>
</ul>
</section>
<section>
<header>How to Install Ghidra</header>
<br>
<ul>
<li>Install the required version of Java</li>
<li>Extract the Ghidra distribution <file>.zip</file></li>
<li>For development, install a supported version of Eclipse</li>
<br>
<li>See the documentation for version requirements</li>
</ul>
</section>
<section>
<h2>What is Eclipse?</h2>
</section>
<section>
<header>Eclipse</header>
<br><br>
<ul>
<li>Integrated Development Environment (IDE)</li>
<ul>
<li>Java, C/C++, Python, and more!</li>
</ul>
<li>Can be used for script development</li>
<li>Can be used for extension development</li>
</ul>
</section>
<section>
<header>Eclipse</header>
<br><br>
<ul>
<li>To integrate with Ghidra:</li>
<ul>
<li>Install the GhidraDev plugin</li>
<li>Connect Ghidra to GhidraDev/Eclipse</li>
<br>
<li>GhidraDev is distributed with Ghidra</li>
<li>See <span style="font-family:monospace;">"GhidraDev_README.html"</font></li>
</ul>
</ul>
</section>
<section>
<header>Eclipse</header>
<ul>
<li>Eclipse Java Editor Tips and Tricks (1/3)</li>
<ul>
<li>Open Type: <keys><k>Ctrl</k> <k>Shift</k> <k>T</k></keys></li>
<ul>
<li>Allows you to find a class without knowing the package</li>
</ul>
<li>Organize Imports: <keys><k>Ctrl</k> <k>Shift</k> <k>O</k></keys></li>
<ul>
<li>Automatically fixes import statements</li>
</ul>
<li>Navigate to: <keys><k>F3</k></keys></li>
<ul>
<li>Navigate to the class, method, variable, etc., at the current cursor location</li>
</ul>
</ul>
</ul>
</section>
<section>
<header>Eclipse</header>
<ul>
<li>Eclipse Java Editor Tips and Tricks (2/3)</li>
<ul>
<li>Type Hierarchy: <keys><k>F4</k></keys></li>
<ul>
<li>Show inheritance hierarchy of current class/interface</li>
</ul>
<li>Find References to: <keys><k>Ctrl</k> <k>Shift</k> <k>G</k></keys></li>
<ul>
<li>Finds all references to the item at current cursor location</li>
</ul>
<li>Toggle Comments: <keys><k>Ctrl</k> <k>/</k></keys></li>
<ul>
<li>Comment or uncomment the selection</li>
</ul>
</ul>
</ul>
</section>
<section>
<header>Eclipse</header>
<ul>
<li>Eclipse Java Editor Tips and Tricks (3/3)</li>
<ul>
<li>Outline View: <keys><k>Ctrl</k> <k>O</k></keys></li>
<ul>
<li>Lists members of the class</li>
</ul>
<li>Code Completion: <keys><k>Ctrl</k> <k>Space</k></keys></li>
<ul>
<li>Displays a list of matches to complete the current expression</li>
</ul>
<li>Quick Fix: <keys><k>Ctrl</k> <k>1</k></keys></li>
<ul>
<li>Offer corrections to most problems (errors, warnings, etc.)</li>
</ul>
</ul>
</ul>
</section>
<section>
<header>Eclipse</header>
<br>
<ul>
<li>Provides templates and common tasks for Ghidra development</li>
<br>
<li><menus><m>GhidraDev</m><m>New</m><m>Ghidra Module Project</m></menus></li>
<li><menus><m>GhidraDev</m><m>New</m><m>Ghidra Script</m></menus></li>
<li><menus><m>GhidraDev</m><m>Export</m><m>Ghidra Module Extension</m></menus></li>
</ul>
</section>
<section>
<header>Lab 1</header>
<br><br>
<ul>
<li>Launch Ghidra through Eclipse in debug mode</li>
</ul>
</section>
<section>
<h2>Development</h2>
</section>
<section>
<header>Development</header>
<br>
<ul class="medium">
<li>Every piece of Ghidra is extensible</li>
<ul>
<li>Plugins, Scripts, Analyzers, Fields, Importers, Exporters, etc.</li>
</ul>
<li>Extensible components implement <cls>ExtensionPoint</cls></li>
<ul>
<li>List suffix in <file>ExtensionPoint.manifest</file></li>
</ul>
<br>
<li>Create a New Ghidra Module Project...</li>
<ul>
<li>You may be prompted to locate Ghidra</li>
</ul>
</ul>
</section>
<section>
<h2>Anatomy of an Extension Project</h2>
</section>
<section>
<header>Extension Project</header>
<br>
<ul class="bare medium">
<li><folder class="icon">MyExtension/</folder></li>
<ul class="bare">
<li><folder class="icon">src/main/java/</folder></li>
<li><folder class="icon">src/main/help/</folder></li>
<li><folder class="icon">src/main/resources/</folder></li>
<li><folder class="icon">ghidra_scripts/</folder></li>
<li><folder class="icon">data/</folder></li>
<li><folder class="icon">lib/</folder></li>
<li><folder class="icon">os/</folder></li>
<li><file class="icon">extension.properties</file></li>
<li><file class="icon">Module.manifest</file></li>
</ul>
</ul>
</section>
<section>
<header>Extension Project</header>
<br>
<br>
<folder class="icon">src/main/java/</folder>
<br>
<folder class="icon">src/main/resoures/</folder>
<ul>
<li>Hold the Java source for this extension</li>
<li>Packaged into a <file>.jar</file> file</li>
<li>Add <folder>src/test/java/</folder> manually to include unit tests</li>
<ul>
<li>Unit tests are not included in the <file>.jar</file> file</li>
</ul>
</ul>
</section>
<section>
<header>Extension Project</header>
<br><br>
<folder class="icon">src/main/help/</folder>
<ul>
<li>Holds online help for this extension</li>
<li>Contains the table of contents to append</li>
<li>Contains the CSS and HTML contents</li>
<li>Java help does not support the latest HTML/CSS. Please preview using the help viewer rather than your browser.</li>
</ul>
</section>
<section>
<header>Extension Project</header>
<br><br>
<folder class="icon">ghidra_scripts/</folder>
<ul>
<li>Holds scripts for this extension</li>
<li>Expect the user to copy and modify scripts</li>
<li>Unpacked as source to the file system on installation</li>
<li>May provide examples to use an extension's API</li>
</ul>
</section>
<section>
<header>Extension Project</header>
<br>
<folder class="icon">data/</folder>
<ul>
<li>Holds data files for this extension</li>
<li>Will not end up inside the <file>.jar</file> file</li>
<li>Will be present in the distribution <file>.zip</file> file</li>
<li>Unpacked to the file system on installation</li>
<li>Allows user to easily edit or append data compared to resources in the <file>.jar</file></li>
</ul>
</section>
<section>
<header>Extension Project</header>
<br>
<folder class="icon">lib/</folder>
<ul>
<li>Holds external Java dependencies for this extension</li>
<li>When working in Eclipse, the contents of this directory must be manually added to the class path of the Eclipse project</li>
<li>Optional</li>
<ul>
<li>This directory may be deleted if there are no external dependencies</li>
</ul>
</ul>
</section>
<section>
<header>Extension Project</header>
<br>
<folder class="icon">os/</folder>
<ul>
<li>Holds native components for this extension</li>
<li>Optional</li>
<ul>
<li>This directory may be deleted if there are no native components</li>
</ul>
<li>NEVER EVER USE JNI!</li>
<ul>
<li>Communicate with a native process using sockets, I/O stream, etc.</li>
</ul>
</ul>
</section>
<section>
<header>Extension Project</header>
<br>
<ul class="bare">
<li><folder class="icon">os/</folder>
<ul class="bare">
<li><folder class="icon">linux64/</folder></li>
<ul>
<li>Linux x86_64 natives</li>
</ul>
<li><folder class="icon">osx64/</folder></li>
<ul>
<li>Mac OSX x86_64 natives</li>
</ul>
<li><folder class="icon">win64/</folder></li>
<ul>
<li>Windows x64 natives</li>
</ul>
</ul>
</ul>
</section>
<section>
<header>Lab 2</header>
<br>
<ul>
<li>Create a new Ghidra module project</li>
<li>From the Eclipse menu bar, select:</li>
<ul>
<li><menus><m>GhidraDev</m><m>New</m><m>Ghidra Module Project</m></menus></li>
</ul>
<li>In the "Module name" field, enter "gadc"</li>
<li>Click the "Finish" button to complete</li>
</ul>
</section>
<section>
<h2>Program API</h2>
</section>
<section>
<header>High-Level Classes</header>
<br>
<img width="95%" src="Images/highLevelClasses.png" style="vertical-align:middle"></img>
<br>
<ul><ul>
<span style="font-size:25px"><i>Note: this diagram is not complete.</i></span>
</ul></ul>
</section>
<section>
<h2>Flat vs. Program API</h2>
</section>
<section>
<header>Program API</header>
<br>
<div style="width:55%;display:inline-block;vertical-align:middle;">
<ul class="medium">
<li>Ghidra Program API</li>
<ul>
<li>Object-Oriented</li>
<li>Very deep</li>
<li>Can change from version to version</li>
</ul>
</ul>
</div><div style="width:45%;display:inline-block;vertical-align:middle;">
<ul class="tiny">
<li><cls>Program</cls></li>
<ul>
<li><cls>Listing</span></li>
<ul>
<li><cls>Instructions</cls></li>
<li><cls>Data</cls></li>
<li><cls>Functions</cls></li>
<li><cls>Comments</cls></li>
</ul>
<li><cls>Memory</cls></li>
<ul>
<li><cls>Memory Blocks</cls></li>
<li><cls>Bytes</cls></li>
</ul>
<li><cls>Symbol Table</cls></li>
<ul>
<li><cls>Symbols</cls></li>
</ul>
<li><cls>Reference Manager</cls></li>
<ul>
<li><cls>References</cls></li>
<ul>
<li><cls>Memory</cls></li>
<li><cls>Stack</cls></li>
<li><cls>External</cls></li>
</ul>
</ul>
<li>...etc.</li>
</ul>
</ul>
</div>
</section>
<section>
<header>Flat Program API</header>
<br><br>
<ul>
<li>Flat</li>
<li>Provides access to most common features</li>
<li>Is not complete</li>
<li>Will not change on you*</li>
</ul>
</section>
<section>
<header>Flat Program API</header>
<codeblock class="smaller"><kw>public</kw> <kw>class</kw> FlatProgramAPI {
FlatProgramAPI(<if>Program</if>)
analyze()
clear...()
create...()
find...()
get...()
remove...()
save()
set...()
to...()
}</codeblock>
</section>
<section>
<h2>Scripting</h2>
</section>
<section>
<header>Scripting</header>
<br><br>
<ul class="medium">
<li>Script Manager</li>
<ul>
<li>Script Category Tree</li>
<ul>
<li>Displays dynamic tree of categories</li>
</ul>
<li>Script Table</li>
<ul>
<li>Displays name, description, key binding</li>
</ul>
<li>Filter</li>
<ul>
<li>Matches filter to name or description</li>
<li>Wildcard and case-insensitive</li>
</ul>
</ul>
</section>
<section>
<header>Scripting</header>
<br>
<ul>
<li>Script Directories</li>
<ul>
<li>Allows management of script directories</li>
<li>Default directories</li>
<ul class="bare small">
<li><folder class="icon"><vardir>HOME</vardir>/ghidra_scripts/</folder></li>
<li><folder class="icon"><vardir>INSTALL</vardir>/Features/Base/ghidra_scripts/</folder></li>
<li><folder class="icon"><vardir>Each other module</vardir>/ghidra_scripts/</folder></li>
</ul>
<li>Useful for sharing scripts in multi-user</li>
</ul>
</ul>
</section>
<section>
<header>Scripting</header>
<br>
<ul>
<li>GhidraScript API</li>
</ul>
<codeblock class="smaller"><kw>public</kw> <kw>abstract</kw> <kw>class</kw> GhidraScript
<kw>extends</kw> FlatProgramAPI {
ask...()
create...()
get...()
goto...()
print...()
run...()
to...()
}</codeblock>
</section>
<section>
<header>Scripting</header>
<ul>
<li>Sample script template in Java</li>
<ul>
<li>Must fill in the TODO areas</li>
<li>Create a description</li>
<li>Fill in the <tt>run()</tt> method</li>
</ul>
</ul>
<codeblock class="small"><com>//TODO add description here</com>
<com>//TODO add metadata here</com>
<kw>public</kw> <kw>class</kw> MyScript <kw>extends</kw> GhidraScript {
<kw>public</kw> <kw>void</kw> run() <kw>throws</kw> Exception {
<com>//TODO add code here</com>
}
}
</codeblock>
</section>
<section>
<header>Scripting</header>
<br>
<ul>
<li>Script Meta-data</li>
<ul>
<li>Special tags in header comment</li>
<li>Describe the script</li>
<li>All are optional</li>
<li>Handle all the messy Java GUI stuff (buttons, menus, etc.)</li>
<li>Do not insert blank lines between meta-data</li>
</ul>
</ul>
</section>
<section>
<header>Scripting</header>
<br><br>
<ul>
<li>Script Meta-data</li>
<ul>
<li><tt>@category</tt></li>
<ul>
<li>The category path for the script</li>
<li>Levels are separated by "."</li>
<li>Example</li>
<ul>
<li><tt>@category A.B.C</tt></li>
</ul>
</ul>
</ul>
</ul>
</section>
<section>
<header>Scripting</header>
<br>
<ul>
<li>Script Meta-data</li>
<ul class="medium">
<li><tt>@keybinding</tt></li>
<ul>
<li>Default key binding for a script</li>
<li>Format is <tt>[ctrl] [alt] [shift] [A-Z,0-9,F1-F12]</tt> and is case-sensitive</li>
<li>Examples</li>
<ul>
<li><tt>@keybinding L</tt></li>
<li><tt>@keybinding ctrl alt shift F1</tt></li>
<li><tt>@keybinding ctrl shift COMMA</tt></li>
</ul>
</ul>
</ul>
</ul>
</section>
<section>
<header>Scripting</header>
<br><br>
<ul>
<li>Script Meta-data</li>
<ul class="medium">
<li><tt>@menupath</tt></li>
<ul>
<li>Top-level menu path for a script</li>
<li>Use with caution!</li>
<ul>
<li>Overcrowded menu</li>
<li>Collide with system action</li>
</ul>
<li>Example</li>
<ul>
<li><tt>@menupath File.Run.My Script</tt></li>
</ul>
</ul>
</ul>
</ul>
</section>
<section>
<header>Scripting</header>
<br>
<ul class="medium">
<li>Script Meta-data</li>
<ul>
<li><tt>@toolbar</tt></li>
<ul>
<li>Image for top-level toolbar button to launch this script</li>
<li>Searches for image in script directories and then Ghidra installation directory</li>
<li>Also use with caution! (same issues as <tt>@menupath</tt>)</li>
<li>Example</li>
<ul>
<li><tt>@toolbar myScriptImage.gif</tt></li>
</ul>
</ul>
</ul>
</ul>
</section>
<section>
<header>Scripting</header>
<br>
<ul>
<li>Script State (1/3)</li>
<ul class="medium">
<li><tt>currentProgram</tt></li>
<ul>
<li>The current active open program</li>
</ul>
<li><tt>currentAddress</tt></li>
<ul>
<li>The current address of the location of the cursor</li>
</ul>
<li><tt>currentLocation</tt></li>
<ul>
<li>The program location of the cursor</li>
</ul>
</ul>
</ul>
</section>
<section>
<header>Scripting</header>
<br>
<ul>
<li>Script State (2/3)</li>
<ul class="medium">
<li><tt>currentSelection</tt></li>
<ul>
<li>The current <span style="color:#7CFC00">selection</span> or <tt>null</tt> if no selection exists</li>
</ul>
<li><tt>currentHighlight</tt></li>
<ul>
<li>The current <span style="color:#F0E68C">highlight</span> or <tt>null</tt> if no highlight exists</li>
</ul>
<li><tt>state</tt></li>
<ul>
<li>Provides place to store environment variables</li>
<li>Static and non-static</li>
</ul>
</ul>
</ul>
</section>
<section>
<header>Scripting</header>
<br>
<ul>
<li>Script State (3/3)</li>
<ul class="medium">
<li><tt>monitor</tt></li>
<ul>
<li>Allows script writer to inform user</li>
<ul>
<li>messages and progress</li>
</ul>
<li>Allows user to cancel script</li>
<li>Always use inside loops</li>
</ul>
</ul>
</ul>
<codeblock class="smaller"><kw>while</kw> (!<fld>monitor</fld>.isCancelled()) {
...
}</codeblock>
</section>
<section>
<header>Console</header>
<br><br>
<ul>
<li>Provides a place to dump information</li>
<li>Any text representing a symbol or address can be navigated by double-clicking</li>
<li>Example</li>
<ul class="small">
<li>Run <file>PropagateExternalParametersScript.java</file></li>
</ul>
</ul>
</section>
<section>
<h2>Scripting Lab</h2>
</section>
<section>
<header>Lab 3</header>
<br>
<ul class="medium">
<li>Create a new Java script: <file><im>Lab3Script.java</im></file></li>
<li>Set the description</li>
<li>Set category: <im>Category A.Category B</im></li>
<li>Set key binding: <im><keys><k>Alt</k> <k>Shift</k> <k>6</k></im></li>
<li>Set menu path: <im><menus><m>Script</m><m>My Class Script</m></menus></im></li>
<li>Set toolbar button: <im><file>Info.png</file></im></li>
<li>Change the body of the <tt>run()</tt> method to:
<codeblock class="small">println(<strlit>"Hello class"</strlit>);</codeblock></li>
<li>Save it </li>
<li>Run it </li>
</ul>
</section>
<section>
<header>Lab 3 Answer</header>
<codeblock class="small"><com>//It prints "Hello class" to the console.
//<annot>@category</annot> Category A.Category B
//<annot>@keybinding</annot> alt shift 6
//<annot>@menupath</annot> Script.My Class Script
//<annot>@toolbar</annot> Info.png</com>
<kw>import</kw> ghidra.app.script.GhidraScript;
<kw>public</kw> <kw>class</kw> Lab3Script <kw>extends</kw> GhidraScript {
<annot>@Override</annot>
<kw>public</kw> <kw>void</kw> run() <kw>throws</kw> Exception {
println(<strlit>"Hello class"</strlit>);
}
}</codeblock>
</section>
<section>
<header>Lab 4</header>
<br>
<ul>
<li>Create new script that will ask for an integer and print the current program's name that many times to the console</li>
</ul>
</section>
<section>
<header>Lab 4 Answer</header>
<codeblock class="reallysmall"><com>//Ask for an integer and print the current</com>
<com>//program’s name that many times to the console</com>
<com>//<annot>@category</annot> GADC</com>
<kw>import</kw> ghidra.app.script.GhidraScript;
<kw>public</kw> <kw>class</kw> Lab4Script <kw>extends</kw> GhidraScript {
<annot>@Override</annot>
<kw>public</kw> <kw>void</kw> run() <kw>throws</kw> Exception {
<kw>int</kw> <loc>n</loc> = askInt(<strlit>"How Many Times?"</strlit>, <strlit>"N"</strlit>);
<kw>for</kw> (<kw>int</kw> <loc>i</loc> = <lit>0</lit>; <loc>i</loc> < <loc>n</loc>; ++<loc>i</loc>) {
<kw>if</kw> (<fld>monitor</fld>.isCancelled()) {
<kw>break</kw>;
}
println(<loc>i</loc> + <strlit>". "</strlit> + <fld>currentProgram</fld>.getName());
Thread.<stat>sleep</stat>(<lit>1000</lit>);
}
}
}</codeblock>
</section>
<section>
<header>Lab 5</header>
<br>
<ul>
<li>Write a script that will search for instructions that move a scalar into a register</li>
<li>Pull the scalar from the instruction and create an EOL comment in the form:</li>
<ul class="bare">
<li><im>setting [register] = [value]</im></li>
</ul>
</ul>
</section>
<section>
<header>Lab 5 Answer</header>
<codeblock class="supersmall"><com>//This script searches through all instructions that are</com>
<com>//moving a scalar into a register</com>
<com>//and sets an EOL comment in the form "[register] = [value]"</com>
<com>//<annot>@category</annot> GADC</com>
<kw>import</kw> ghidra.app.script.GhidraScript;
<kw>import</kw> ghidra.program.model.lang.Register;
<kw>import</kw> ghidra.program.model.listing.Instruction;
<kw>import</kw> ghidra.program.model.scalar.Scalar;
<kw>public</kw> <kw>class</kw> Lab5Script <kw>extends</kw> GhidraScript {
<annot>@Override</annot>
<kw>public</kw> <kw>void</kw> run() <kw>throws</kw> Exception {
<kw>for</kw> (<if>Instruction</if> <loc>instruction</loc> = getFirstInstruction(); <loc>instruction</loc> != <kw>null</kw>; <loc>instruction</loc> = getInstructionAfter(<loc>instruction</loc>)) {
<kw>if</kw> ( <fld>monitor</fld>.isCancelled() ) {
<kw>break</kw>;
}
if (<loc>instruction</loc>.getNumOperands() != <lit>2</lit>) {
<kw>continue</kw>;
}
Object[] <loc>opObjects0</loc> = <loc>instruction</loc>.getOpObjects(<lit>0</lit>);
<kw>if</kw> (<loc>opObjects0</loc>.<fld>length</fld> != <lit>1</lit> || !(<loc>opObjects0</loc>[<lit>0</lit>] <kw>instanceof</kw> <if>Register</if>)) {
<kw>continue</kw>;
}
Object[] <loc>opObjects1</loc> = <loc>instruction</loc>.getOpObjects(<lit>1</lit>);
<kw>if</kw> (<loc>opObjects1</loc>.<fld>length</fld> != <lit>1</lit> || !(<loc>opObjects1</loc>[<lit>0</lit>] <kw>instanceof</kw> Scalar)) {
<kw>continue</kw>;
}
Register <loc>register</loc> = (Register) <loc>opObjects0</loc>[<lit>0</lit>];
Scalar <loc>scalar</loc> = (Scalar) <loc>opObjects1</loc>[<lit>0</lit>];
String <loc>comment</loc> = <strlit>"["</strlit> + <loc>register</loc>.getName() + <strlit>"]=["</strlit> + <loc>scalar</loc>.toString(<lit>16</lit>, <kw>false</kw>, <kw>false</kw>, <stlrit>""</strlit>, <strlit>""</strlit>) + <strlit>"]"</strlit>;
setEOLComment(<loc>instruction</loc>.getMinAddress(), <loc>comment</loc>);
}
}
}</codeblock>
</section>
<section>
<h2>Headless Scripting</h2>
</section>
<section>
<header>Headless Scripting</header>
<br>
<ul>
<li>Ghidra can be run from the command line without invoking the user interface</li>
<li>Can be run on one or more programs</li>
<li>Any script that does not invoke the GUI can be run in headless mode</li>
<li>See <file>analyzeHeadlessREADME.html</file></li>
</ul>
</section>
<section>
<h2>Plugins</h2>
</section>
<section>
<header>Plugins</header>
<br>
<ul>
<li>Must extend <cls>Plugin</cls> class</li>
<li>Can provide and consume services</li>
<li>Can provide actions</li>
<ul>
<li>Extend <cls>DockingAction</cls></li>
</ul>
<li>Can provide GUIs</li>
<ul>
<li>Extend <cls>ComponentProvider</cls></li>
</ul>
</ul>
</section>
<section>
<header>Plugins</header>
<ul>
<li>Must provide a plugin description</li>
<ul>
<li>Use the <cls>@PluginInfo</cls> annotation</li>
</ul>
<li>In constructor</li>
<ul>
<li>Register provided service implementations</li>
</ul>
<li>In <tt>init()</tt></li>
<ul>
<li>Retrieve services consumed</li>
<li>Create actions</li>
</ul>
<li>In <tt>dispose()</tt></li>
<ul>
<li>Release resources</li>
</ul>
</ul>
</section>
<section>
<header><cls>ProgramPlugin</cls></header>