The Hive
GitHubLinkedInEmail
  • 🏠Home
  • 🌐RECON
    • 📡Passive (OSINT)
      • ⏩Metadata
      • ⏩Social Platforms
        • Email
        • Tumbler
        • Redit
        • Github
        • Tinder
        • TikTok
        • Snapchat
        • Instagram
        • Facebook
        • Twitter
        • Google
        • LinkedIn
    • 📡Active
      • ⏩Host Discovery / Network Mapping
      • ⏩nmap cheat sheet
      • ⏩masscan cheat sheet
    • 📡Web Recon
      • ⏩Web Server Discovery
      • ⏩Hidden Hosts
      • ⏩Directories & Subdomains
      • ⏩SSL Certs
      • ⏩CMS
      • ⏩WAF Detection
    • 📡Firewall Evasion
  • 📗Web Attacks
    • 🟢Server Side
      • 🟩Authentication Mechanisms
      • 🟩Access Control (Authorization)
      • 🟩Directory Traversal
      • 🟩OS Command Injection
      • 🟩Server-Side Request Forgery (SSRF)
      • 🟩XML External Entity (XXE) Injection
      • 🟩File Upload
      • 🔧SQL Injection
      • 🟩Information Disclosure
      • 🟩Business Logic
    • 🟢Client Side
      • 🟩Cross-site request forgery (CSRF)
      • 🔧Cross-site scripting (XSS)
  • 📒Network attacks
    • 🟡Network Services
      • 🟨Brute Force
      • 🟨DNS
      • 🟨IPv6
      • 🟨FTP
      • 🟨SSH
      • 🟨SMB
      • 🟨SNMP
      • 🟨SMTP
      • 🟨POP3
      • 🟨IMAP
      • 🟨MSSQL
      • 🟨MySQL
      • 🟨MSRPC / RPCbind
      • 🟨LDAP
      • 🟨NTP
      • 🟨NFS
      • 🟨Telnet
      • 🟨WebDAV
      • 🟨RDP
      • 🟨RSIP
      • 🟨Rlogin
      • 🟨VPNs
      • 🟨Echo
      • 🔧RTP
      • 🔧VOIP
        • SIP
    • 🟡Network Devices
      • 🟨IPv6 Attacks
        • Neighbor Impersonation
        • Router Advertisement Flooding
      • 🟨Switch Attacks
        • Cisco Exploitation
        • STP Spoofing
        • VLAN Hopping
        • MAC Flood
      • 🟨Router Attacks
        • Router Exploitation
        • HSRP Hijacking
        • 🔧RIP Spoofing
        • 🔧OSPF Attacks
        • 🔧VRRP MitM
      • 🟨NAC Bypass
        • Captive Portal
        • 802.1X / EAP Bypass
      • 🟨Printer Exploitation
    • 🟡MITM & Poisoning
      • 🟨Bettercap
      • 🟨HTTPS Downgrade / HSTS Bypass
      • 🟨Session Hijackings
      • 🟨Malicious Update
      • 🟨RDP Downgrade
      • 🟨DNS Spoofing
      • 🟨NTP Spoofing
      • 🟨ARP Spoofing
      • 🟨DHCP Poisoning
      • 🟨DHCPv6 Spoofing
      • 🟨SSDP Spoofing
      • 🟨WSUS Spoofing
      • 🟨ADIDNS Poisoning
      • 🟨WPAD Abuse
    • 🟡Wireless Attacks
      • 🟨Protocol Concepts
      • 🟨Basics
      • 🟨Attacks
    • 🟡Sniffing
      • 🟨Wireshark
      • 🟨tcpdump
    • 🟡Denial of Service
  • 📕Red Team
    • 🔴Windows
      • ⭕Security Concepts
        • Windows Security Components
        • Active Directory Components
        • Kerberos
        • Loggon Sessions and Access Tokens
        • Permissions and Access Control
        • Windows Registry
        • Object Management
      • ⭕Physical Attack
      • ⭕Enumeration
      • ⭕Privilege Escalation
        • DLL Hijacking
          • Phantom DLL Hijacking / Replacement
          • Search Order Hijacking ( Preloading )
          • DLL Side-Loading
        • Service Misconfigurations
          • Weak Registry Permissions
          • Insecure Service Executables
          • Insecure Permission
          • Unquoted Service Path
        • Creating a New Service (admin to system)
        • Registry
          • AlwaysInstallElevated
          • AutoRuns
        • Scheduled Tasks
        • Mass Roll-outs
        • Startup Apps
        • Installed Applications
        • Loopback Services
        • Insecure GUI APPs
        • Potatos
        • Printspoofer / SEImpersonate
        • PSEXEC (admin to system)
      • ⭕Credential Dumping
      • ⭕Persistence
        • Invisible Account Forger
        • Add User
        • Scheduled Tasks
        • Run Registry Keys
        • Logon Scripts
        • Screensavers Hijack
        • Powershell Profiles & Modules
        • Service Creation/Modification
        • Shortcut Modification
        • Startup Folder
        • RDP backdoors
        • COM Hijacking
    • 🔴Active Directory
      • ⭕Domain Enumeration
      • ⭕Tools & Frameworks
        • Evil-WinRM
        • CME cheat sheet
        • SharpSploit
        • impacket cheat sheet
        • DeathStar
      • ⭕Exploitation
        • LLMNR Poisoning
        • SMB/NTLM Relay
        • DNS Takeover + LDAP Relay
        • Cracking Hashes
        • Password spraying
        • ADCS + PetitPotam NTLM Relay
        • EternalBlue
        • ZeroLogon
        • MS Exchange ProxyShell
        • MS Exchange ProxyLogon
        • Java JBOSS
      • ⭕Privilege Escalation
        • Token Impersonation
        • DNS Admins
        • AD CS Abuse
        • ACL Abuse
          • GenericAll
          • Write Property
          • Self-membership
          • ForceChangePassword
          • Managed Security Groups
          • Exchange Windows Permissions
        • Group Policy Objects (GPOs)
        • Custom SSPs
        • PrintNightmare
      • ⭕Lateral Movement
        • RDP Password Decryption
        • RDP Session Hijacking
        • headless RDP with SharpRDP
        • Domain Shares
        • SCF File Attacks
        • Pass the Hash / Password
        • Overpass the Hash / Pass the Key
        • Pass The Ticket
        • Kerberosting / AS-REP Rosting
        • Kerberos Delegation
      • ⭕Credential Dumping
        • CredSSP / TSPKG
        • Wdigest Clear Text
        • DPAPI secrets
        • SAM & Registry
        • NTDS.dit & vshadow
        • comsvcs.dll
        • Meterpreter
        • Procdump & LSASS
        • AD User Comments
        • SYSVOL & Group Policy Preferences
        • LAPS Passwords
        • GSMA Passwords
        • HiveNightmare
        • Mimikatz Cheat sheet
        • Other Tools / Techniques
      • ⭕Persistence
        • Certificates
        • DCSync
        • DCShadow
        • Silver Ticket
        • Golden Ticket
        • Skeleton Key
        • WMI
        • PowerShell Remoting
        • Remote Registry
        • Rights Abuse
        • AdminSDHolder
        • DSRM
        • Kerberos Checksum Validation ( MS14-068 )
    • 🔴Linux
      • ⭕Physical Attacks
      • ⭕Enumeration
      • ⭕Privilege Escalation
        • SUID / SGID abuse
        • /etc/shadow & /etc/passwd
        • cron/crontab abuse
        • Sudo Abuse
        • Capabilities Abuse
        • Environment Variables
          • LD_LIBRARY_PATH
          • LD_PRELOAD
        • Shared Object Injection
        • NFS
        • man CE Pager Argument
        • MySQL UDF
        • UDEVD
        • STDIN/STDOUT
        • Unix Socket Exploitation
        • Dirty Pipe
        • Docker
          • SUID Docker
      • ⭕Lateral Movement
        • Infecting Running Processes
        • VIM Config File Keylogger
        • SSH Hijacking
        • Samba Secrets to Domain Admin
        • Hiding Processes
        • Simple User-mode Rootkits
        • Vino VNC Server
      • ⭕Credential Dumping
        • Swap Dump
        • mimipinguin
        • unshadow
        • 3snake
      • ⭕Persistence
        • Startup User File Backdoor
        • PHP Backdoor
        • Apache mod_rootme
        • Startup Service Backdoor
        • xdg Backdoor
        • rootbash SUID
        • apt Backdoor
        • Driver Backdoor
        • Core Pattern
        • dash Backdoor
        • Creating an SUID Binary
        • Systemd netcat bind shell
        • Xinetd UDP portnock
        • openSSL reverse shell
        • motd Backdoor
        • Auth Log Backdoor
        • RSYSLOG Backdoor
        • sshd Backdoor
        • VIM Config Backdoor
        • .bashrc Backdoor
        • Adding a Root user
        • Crontab Reverse Shell
        • SSH persistence password-less
      • ⭕Covering Tracks
    • 🔴Command & Control (C2)
      • ⭕Cobalt Strike
      • ⭕Metasploit
      • ⭕Empire & Starkiller
      • ⭕Covenant
    • 🔴Shells and Payloads
      • ⭕Shell Escape / Interactive Shell
      • ⭕LOL Binaries
      • ⭕msfvenom
      • ⭕SharpShooter & Ivy
      • ⭕Other Payloads
    • 🔴Payload Delivery
      • ⭕Powershell Reflective DLL Load
      • ⭕HTML Smuggling
      • ⭕Office Macros
      • ⭕DDE Auto - Word/Excel
      • ⭕.SLK Excel
      • ⭕XLM Macro 4.0
      • ⭕LNK
      • ⭕embedded OLE + LNK objects
      • ⭕JScript
      • ⭕HTA
      • ⭕VBS
      • ⭕VBA
      • ⭕RTF
      • ⭕REG
      • ⭕MSI / MSIEXEC
      • ⭕IQY
      • ⭕CHM / HHC
      • ⭕SCR
    • 🔴Pivoting
      • ⭕SSH Forwarding
      • ⭕Socat Stealth Port Forward
      • ⭕Socat Reverse Shell Relay
      • ⭕HTTP Tunneling
      • ⭕ICMP Tunneling
      • ⭕DNS Tunneling
      • ⭕Metasploit Pivoting
      • ⭕Cobalt Strike Pivoteing
      • ⭕VPN Tunneling
      • ⭕Other Tools
    • 🔴Exfiltration / File Transfer
      • ⭕Encode / Decode Files
      • ⭕TCP / UDP
      • ⭕DNS
      • ⭕SSH
      • ⭕ICMP
      • ⭕SMB
      • ⭕FTP
      • ⭕HTTP
      • ⭕Other Methods
    • 🔴Password Attacks
      • ⭕Online Attacks
      • ⭕Offline Attack
      • ⭕Word List
      • ⭕Cheat Sheet
    • 🔴Defense Evasion
      • ⭕Basic Tricks
      • 🔧Powershell Tricks
      • ⭕Disabling Defenses
      • ⭕UAC Bypass
      • ⭕Process Migration
      • ⭕Dechaining Macros
      • ⭕VBA Sandbox Evasion
      • ⭕AMSI Bypass
      • ⭕SRP & AppLocker Bypass
      • ⭕GPO Bypass
  • 📘Blue Team
    • 🔵Threat Modeling / Hunting / Intelligence
    • 🔵Linux Hardening
      • 🔹OS Security
        • Update Strategy
        • Service Management
        • Physical Security
        • Grub Hardening
        • Kernel Parameters
        • Process Isolation
      • 🔹Accounts & Passwords
        • Users & Groups
        • Password Security & Sudoers
      • 🔹Access Control & Ownership
      • 🔹File System Security
      • 🔹Integrity Check
      • 🔹Sandboxing
      • 🔹Network
      • 🔹iptables
        • Rule Sets
      • 🔹Service Hardening
        • BIND9
        • vsftpd
        • Nginx
        • Apache
        • SSH
      • 🔹System Audit
      • 🔹Logging
        • auditd
      • 🔹Encryption
    • 🔵Security Architecture
      • 🔹Layered Security
  • 🟪Purple Teaming
    • 🟣Adversary Emulation
  • 🟧programming
    • 🟠C Programming
      • 🔸Basic Structure
      • 🔸GCC Compiler
      • 🔸Preprocessors
      • 🔸Data Types
      • 🔸Type Qualifiers
      • 🔸Pointers
      • 🔸Dynamic Memory Allocation
      • 🔸Loops
      • 🔸Conditional Statements
      • 🔸Functions
      • 🔸Input / Output
      • 🔸Macros
      • 🔸Files
      • 🔸Strings Manipulation
      • 🔸Bit Manipulation
      • 🔸Data Structures
        • Arrays
        • Structures
        • Unions
      • 🔸Abstract Data Types
        • Stack
        • Queue
        • Linked List
          • Singly Linked List
          • Doubly Linked List
      • 🔸Libraries & Linking
      • 🔸Error Recovery
    • 🔧Assembly ( NASM )
      • Intel IA-32 Environment
      • Basic Structure
      • Variables and Data Types
      • Most-used Instructions
      • input / output
  • 🟫Miscellaneous
    • 🟤GNU Screen / tmux
    • 🟤SSH Tricks
    • 🟤Cats
      • netcat
      • ncat
      • pwncat
      • socat
      • 🔧powercat
    • 🟤Curl
    • 🟤Cross-compiling Binaries
Powered by GitBook
On this page
  • Creating a structure
  • Using a structure
  • Initializing structures
  • Compound literals ( C11 only )
  • Structure arrays
  • Structures containing arrays
  • Nested structures
  • Array of nested structures
  • Declaring a structure within a structure
  • Structures and pointers
  • using structs as pointers
  • Structures containing pointers
  • Structures as arguments to functions
  • Returning a structure from a function
  1. programming
  2. C Programming
  3. Data Structures

Structures

a tool for grouping elements together.( like an object but just for data types)

Creating a structure

struct struct_name
{
int var1;
int var2;
int var3; 
};

Using a structure

variables can now be declared to be a type of "struct_name"

struct struct_name var1

to set the value of the day in the variable :

struct_name.var1 = value1;

to test the value of the variable:

if ( struct_name.var1 == value )
...

example:

struct date {    // a structure with 3 parameters
int month;
int day;
int year;
};

struct date today;    // a variable of type date

today.month = 5;
today.day = 21;
today.year = 2015;
printf("todays date is %i/%i/%i.\n",today.month, today.day,today.year);

we can also do it this way:

 struct date {   
int month;
int day;
int year;
} today;

we do not have to give a structure a tag name if all of the variables of a particular structure type are defined when the structure is defined the structure name can be omitted

 struct {   
int month;
int day;
int year;
} today;

a disadvantage of this is that we can no longer define further instances of the structure in another statement all the variables of this structure type that you want in your program must be defined in the one statement

Initializing structures

struct date today = { 1,5,2019};

just like an array initialization fewer values might be listed than are contained in the structure:

struct date today = { 1,5}; → no value for year

we can do it this way too:

.member = value
i.e:
struct date date1 = {.month = 12, .day = 23};

Compound literals ( C11 only )

we can assign one or more values to a structure in a single statement using what is known as compound literals

 today = (struct date){9,12,2016}; 

Structure arrays

declaring an array of structures is like declaring any other kind of array:

struct date {
    int day;
    int month;
    int year;
};

struct date myDates[10];

myDates[1].month = 6;
myDates[1].day = 5;
myDates[1].year = 2014;

or

struct  date myDates[5] = {{12,1,1985},{12,5,1984},{11,12,1998}};

also the inner pairs of braces are optional:

struct  date myDates[5] = {12,1,1985,12,5,1984,11,12,1998}; → initialized just the third element of the array to the specified value

struct  date myDates[5] = {[2] = {11,12,1998}}; → sets just the month and day of the second element of the myDates array to 12 and 30

struct  date myDates[5] = {[2].month = 12 };

example:

 struct date {
    int day;
    int month;
    int year;
};
struct date myDates[10] = {[1]=12,2,1998,[2]=19,11,1975,[3]=24,5,1966};
for(int i = 0; i <= 4; ++i)
printf("date: %i/%i/%i\n",myDates[i].day,myDates[i].month,myDates[i].year);

Structures containing arrays

struct month
 {
 int    numberOfDays;
 char name[3];
 };

this sets up a month structure that contains an integer member called numberOfDays and a character member called name member name is actually an array of three characters

struct month aMonth;
 aMonth.numberOfDays = 31;
 aMonth.name[0] = 'j';
 aMonth.name[1] = 'a';
 aMonth.name[2] = 'n';

you can also initialize this variable to the same values:

 struct month aMonth = {31, {'j','a','n'}};

you can setup 12 month structures inside an array to represent each month of the year

 struct month months[12]; 

example:

 struct month {
    int numberOfMonth;
    int numberOfDays;
    char name[3];

};
struct month aMonth;
aMonth.numberOfDays = 31;
aMonth.name[0] = 'j';
aMonth.name[1] = 'a';
aMonth.name[2] = 'n';
aMonth.numberOfMonth = 1;

printf("month name: %i number: %i days: %i",aMonth.name,aMonth.numberOfMonth,aMonth.numberOfDays);

Nested structures

C allows us to define a structure that itself contains other structures as one or more of its members

struct dateAndTime
 {
struct date sdate;
struct time stime;
 };
 
 struct dateAndTme event;
 
 event.sdate.month = 10;
 ++event.stime.secends;
 
 struct dateAndTime event = {{ 2,1,2015}, {3,30,0}};

or

 struct dateAndTime event = {
 {.month = 2 , .day = 1 , .year = 2019},
 {.hour = 3, .minutes = 30, seconds = 0}
 };

we can use members names in initialization

example: an event scheduler with nested structures:

#include <stdio.h>

int main(){

    struct date{
        int day;
        int month;
        int year;
    };

struct time{
    int second;
    int minute;
    int hour;
};


    struct datetime{
        struct date sdate;
        struct time stime;
    };

    struct datetime event;

    printf("enter a second: ");
    scanf("%d",&event.stime.second);
    printf("enter a minute: ");
    scanf("%d",&event.stime.minute);
    printf("enter a hour: ");
    scanf("%d",&event.stime.hour);
    printf("enter a day: ");
    scanf("%d",&event.sdate.day);
    printf("enter a month: ");
    scanf("%d",&event.sdate.month);
    printf("enter a year: ");
    scanf("%d",&event.sdate.year);

    printf("your even scheduled for: %d/%d/%d %d:%d:%d\n",event.sdate.day,event.sdate.month,event.sdate.year,event.stime.second,event.stime.minute,event.stime.hour);

return 0;
};

Array of nested structures

 events[0].stime.hour = 12;
 events[0].stime.minutes = 0;
 events[0].stime.seconds = 2;

Declaring a structure within a structure

struct time
 {
 struct Date
 {
 int day;
 int month;
 int year;
 
 } dob;   // a variable
 
 int hour;
 int minutes;
 int seconds;
 };

the declaration is enclosed within the scope of the time structure definition it doesn't exist outside it it becomes impossible to declare a date variable external to the time structure

Structures and pointers

declaring a struct as a pointer

 struct date *datePtr;
 
 datePtr = &todaysDate;
 
 (*datePtr).day=21;

using structs as pointers

test the value of month stored in the date struct pointed to by datePtr:

 if ((*date*Ptr).month ==12 )

a special operator exists for pointers to structures:

 (*x).y

to be more clearly expressed as:

 x->y

example:

struct date{
    int month;
    int day;
    int year;
};

struct date today, *datePtr;

datePtr = &today;

datePtr->month = 12;
datePtr->day = 4;
datePtr->year = 1998;

printf("today is : %i/%i/%i\n",datePtr->day,datePtr->month,datePtr->year);

Structures containing pointers

example:

struct intPtrs
 {
 int *p1;
 int *p2;
 };

 struct intPtrs pointers;
int i1 = 100, i2;

pointers.p1 = &i1;
pointers.p2=&i2;
*pointers.p2 = -97;

printf("i1 = %i, *pointers.p1=%i\n",i1,*pointers.p1);
printf("i2 = %i, *pointers.p2=%i\n",i2,*pointers.p2);

Structures as arguments to functions

 struct family{
    char name[20];
    int age;
    char father[20];
    char mother[20];
};

bool siblings(struct family member1, struct family member2){
    if (strcmp(member1.mother,member2.mother) == 0)
        return true;
    else
        return false;
}

Returning a structure from a function

the function prototype has to indicate this return value in the normal way

struct Date my_fun(void);

this is a prototype for a function taking no arguments that returns a structureof type Date

its more convenient to return a pointer to a structure.

example:

#include <stdio.h>

struct date{
    int day;
    int month;
    int year;
};

struct time{
    int second;
    int minute;
    int hour;
};


struct datetime{
    struct date sdate;
    struct time stime;
};

char scheduler(struct datetime event){
    printf("your event scheduled for: %d/%d/%d %d:%d:%d\n",event.sdate.day,event.sdate.month,event.sdate.year,event.stime.second,event.stime.minute,event.stime.hour);
};


int main(){
    struct datetime event;
    printf("enter a second: ");
    scanf("%d",&event.stime.second);
    printf("enter a minute: ");
    scanf("%d",&event.stime.minute);
    printf("enter a hour: ");
    scanf("%d",&event.stime.hour);
    printf("enter a day: ");
    scanf("%d",&event.sdate.day);
    printf("enter a month: ");
    scanf("%d",&event.sdate.month);
    printf("enter a year: ");
    scanf("%d",&event.sdate.year);
    scheduler(event);
return 0;
};

example:

#include <stdio.h>

struct funds{
char bank[1000000];
double bankfund;
char save[1000000];
double savefund;
};

double sum(struct funds moolah){
    return(moolah.bankfund + moolah.savefund);
}


int main() {
struct funds stan = {
        "garlic-melon bank",
        47432.34,
        "luckys savings and loan",
        854.435
};

printf("stan has a total of $%.2f.\n",sum(stan));
return 0;
}
PreviousArraysNextUnions

Last updated 2 years ago

🟧
🟠
🔸